Skip to content

Commit bbab950

Browse files
committed
Merge pull request #6731 from jreback/resample
BUG: Bug in resample when how=None resample freq is the same as the axis freq (GH5955)
2 parents bc9f380 + 32b3a06 commit bbab950

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ Bug Fixes
291291
- Bug in ``DataFrame.to_stata`` which incorrectly handles nan values and ignores 'with_index' keyword argument (:issue:`6685`)
292292
- Bug in resample with extra bins when using an evenly divisible frequency (:issue:`4076`)
293293
- Bug in consistency of groupby aggregation when passing a custom function (:issue:`6715`)
294+
- Bug in resample when ``how=None`` resample freq is the same as the axis frequency (:issue:`5955`)
294295

295296
pandas 0.13.1
296297
-------------

pandas/tseries/resample.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@ def _resample_timestamps(self):
246246

247247
# Determine if we're downsampling
248248
if axlabels.freq is not None or axlabels.inferred_freq is not None:
249+
249250
if len(grouper.binlabels) < len(axlabels) or self.how is not None:
251+
# downsample
250252
grouped = obj.groupby(grouper, axis=self.axis)
251253
result = grouped.aggregate(self._agg_method)
252254
else:
@@ -259,8 +261,15 @@ def _resample_timestamps(self):
259261
else:
260262
res_index = binner[:-1]
261263

262-
result = obj.reindex(res_index, method=self.fill_method,
263-
limit=self.limit)
264+
# if we have the same frequency as our axis, then we are equal sampling
265+
# even if how is None
266+
if self.fill_method is None and self.limit is None and to_offset(
267+
axlabels.inferred_freq) == self.freq:
268+
result = obj.copy()
269+
result.index = res_index
270+
else:
271+
result = obj.reindex(res_index, method=self.fill_method,
272+
limit=self.limit)
264273
else:
265274
# Irregular data, have to use groupby
266275
grouped = obj.groupby(grouper, axis=self.axis)

pandas/tseries/tests/test_resample.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,32 @@ def test_resample_basic_from_daily(self):
160160
self.assertEquals(result.irow(5), s['1/9/2005'])
161161
self.assertEqual(result.index.name, 'index')
162162

163+
def test_resample_upsampling_picked_but_not_correct(self):
164+
165+
# Test for issue #3020
166+
dates = date_range('01-Jan-2014','05-Jan-2014', freq='D')
167+
series = Series(1, index=dates)
168+
169+
result = series.resample('D')
170+
self.assertEquals(result.index[0], dates[0])
171+
172+
# GH 5955
173+
# incorrect deciding to upsample when the axis frequency matches the resample frequency
174+
175+
import datetime
176+
s = Series(np.arange(1.,6),index=[datetime.datetime(1975, 1, i, 12, 0) for i in range(1, 6)])
177+
expected = Series(np.arange(1.,6),index=date_range('19750101',periods=5,freq='D'))
178+
179+
result = s.resample('D',how='count')
180+
assert_series_equal(result,Series(1,index=expected.index))
181+
182+
result1 = s.resample('D',how='sum')
183+
result2 = s.resample('D',how='mean')
184+
result3 = s.resample('D')
185+
assert_series_equal(result1,expected)
186+
assert_series_equal(result2,expected)
187+
assert_series_equal(result3,expected)
188+
163189
def test_resample_frame_basic(self):
164190
df = tm.makeTimeDataFrame()
165191

@@ -1078,15 +1104,6 @@ def test_all_values_single_bin(self):
10781104
result = s.resample("A", how='mean')
10791105
tm.assert_almost_equal(result[0], s.mean())
10801106

1081-
def test_resample_doesnt_truncate(self):
1082-
# Test for issue #3020
1083-
import pandas as pd
1084-
dates = pd.date_range('01-Jan-2014','05-Jan-2014', freq='D')
1085-
series = Series(1, index=dates)
1086-
1087-
result = series.resample('D')
1088-
self.assertEquals(result.index[0], dates[0])
1089-
10901107
def test_evenly_divisible_with_no_extra_bins(self):
10911108
# 4076
10921109
# when the frequency is evenly divisible, sometimes extra bins

0 commit comments

Comments
 (0)