Skip to content

BUG: Bug in resample when how=None resample freq is the same as the axis freq (GH5955) #6731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ Bug Fixes
- Bug in ``DataFrame.to_stata`` which incorrectly handles nan values and ignores 'with_index' keyword argument (:issue:`6685`)
- Bug in resample with extra bins when using an evenly divisible frequency (:issue:`4076`)
- Bug in consistency of groupby aggregation when passing a custom function (:issue:`6715`)
- Bug in resample when ``how=None`` resample freq is the same as the axis frequency (:issue:`5955`)

pandas 0.13.1
-------------
Expand Down
13 changes: 11 additions & 2 deletions pandas/tseries/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ def _resample_timestamps(self):

# Determine if we're downsampling
if axlabels.freq is not None or axlabels.inferred_freq is not None:

if len(grouper.binlabels) < len(axlabels) or self.how is not None:
# downsample
grouped = obj.groupby(grouper, axis=self.axis)
result = grouped.aggregate(self._agg_method)
else:
Expand All @@ -259,8 +261,15 @@ def _resample_timestamps(self):
else:
res_index = binner[:-1]

result = obj.reindex(res_index, method=self.fill_method,
limit=self.limit)
# if we have the same frequency as our axis, then we are equal sampling
# even if how is None
if self.fill_method is None and self.limit is None and to_offset(
axlabels.inferred_freq) == self.freq:
result = obj.copy()
result.index = res_index
else:
result = obj.reindex(res_index, method=self.fill_method,
limit=self.limit)
else:
# Irregular data, have to use groupby
grouped = obj.groupby(grouper, axis=self.axis)
Expand Down
35 changes: 26 additions & 9 deletions pandas/tseries/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,32 @@ def test_resample_basic_from_daily(self):
self.assertEquals(result.irow(5), s['1/9/2005'])
self.assertEqual(result.index.name, 'index')

def test_resample_upsampling_picked_but_not_correct(self):

# Test for issue #3020
dates = date_range('01-Jan-2014','05-Jan-2014', freq='D')
series = Series(1, index=dates)

result = series.resample('D')
self.assertEquals(result.index[0], dates[0])

# GH 5955
# incorrect deciding to upsample when the axis frequency matches the resample frequency

import datetime
s = Series(np.arange(1.,6),index=[datetime.datetime(1975, 1, i, 12, 0) for i in range(1, 6)])
expected = Series(np.arange(1.,6),index=date_range('19750101',periods=5,freq='D'))

result = s.resample('D',how='count')
assert_series_equal(result,Series(1,index=expected.index))

result1 = s.resample('D',how='sum')
result2 = s.resample('D',how='mean')
result3 = s.resample('D')
assert_series_equal(result1,expected)
assert_series_equal(result2,expected)
assert_series_equal(result3,expected)

def test_resample_frame_basic(self):
df = tm.makeTimeDataFrame()

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

def test_resample_doesnt_truncate(self):
# Test for issue #3020
import pandas as pd
dates = pd.date_range('01-Jan-2014','05-Jan-2014', freq='D')
series = Series(1, index=dates)

result = series.resample('D')
self.assertEquals(result.index[0], dates[0])

def test_evenly_divisible_with_no_extra_bins(self):
# 4076
# when the frequency is evenly divisible, sometimes extra bins
Expand Down