Skip to content

Commit 62ac12b

Browse files
committed
Merge pull request #8372 from jreback/resample
BUG: bug in non-evently divisible offsets when resampling (e.g. '7s') (GH8371)
2 parents 68e5c59 + a638a4f commit 62ac12b

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

doc/source/v0.15.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ Bug Fixes
964964
- Bug with kde plot and NaNs (:issue:`8182`)
965965
- Bug in ``GroupBy.count`` with float32 data type were nan values were not excluded (:issue:`8169`).
966966
- Bug with stacked barplots and NaNs (:issue:`8175`).
967-
967+
- Bug in resample with non evenly divisible offsets (e.g. '7s') (:issue:`8371`)
968968

969969
- Bug in interpolation methods with the ``limit`` keyword when no values needed interpolating (:issue:`7173`).
970970
- Bug where ``col_space`` was ignored in ``DataFrame.to_string()`` when ``header=False`` (:issue:`8230`).

pandas/tseries/resample.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pandas.tseries.frequencies import to_offset, is_subperiod, is_superperiod
77
from pandas.tseries.index import DatetimeIndex, date_range
88
from pandas.tseries.tdi import TimedeltaIndex
9-
from pandas.tseries.offsets import DateOffset, Tick, _delta_to_nanoseconds
9+
from pandas.tseries.offsets import DateOffset, Tick, Day, _delta_to_nanoseconds
1010
from pandas.tseries.period import PeriodIndex, period_range
1111
import pandas.tseries.tools as tools
1212
import pandas.core.common as com
@@ -385,9 +385,11 @@ def _get_range_edges(first, last, offset, closed='left', base=0):
385385
offset = to_offset(offset)
386386

387387
if isinstance(offset, Tick):
388+
is_day = isinstance(offset, Day)
388389
day_nanos = _delta_to_nanoseconds(timedelta(1))
390+
389391
# #1165
390-
if (day_nanos % offset.nanos) == 0:
392+
if (is_day and day_nanos % offset.nanos == 0) or not is_day:
391393
return _adjust_dates_anchored(first, last, offset,
392394
closed=closed, base=base)
393395

pandas/tseries/tests/test_resample.py

+48
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,54 @@ def test_resample_with_timedeltas(self):
176176

177177
assert_frame_equal(result, expected)
178178

179+
def test_resample_rounding(self):
180+
# GH 8371
181+
# odd results when rounding is needed
182+
183+
data = """date,time,value
184+
11-08-2014,00:00:01.093,1
185+
11-08-2014,00:00:02.159,1
186+
11-08-2014,00:00:02.667,1
187+
11-08-2014,00:00:03.175,1
188+
11-08-2014,00:00:07.058,1
189+
11-08-2014,00:00:07.362,1
190+
11-08-2014,00:00:08.324,1
191+
11-08-2014,00:00:08.830,1
192+
11-08-2014,00:00:08.982,1
193+
11-08-2014,00:00:09.815,1
194+
11-08-2014,00:00:10.540,1
195+
11-08-2014,00:00:11.061,1
196+
11-08-2014,00:00:11.617,1
197+
11-08-2014,00:00:13.607,1
198+
11-08-2014,00:00:14.535,1
199+
11-08-2014,00:00:15.525,1
200+
11-08-2014,00:00:17.960,1
201+
11-08-2014,00:00:20.674,1
202+
11-08-2014,00:00:21.191,1"""
203+
204+
from pandas.compat import StringIO
205+
df = pd.read_csv(StringIO(data), parse_dates={'timestamp': ['date', 'time']}, index_col='timestamp')
206+
df.index.name = None
207+
result = df.resample('6s', how='sum')
208+
expected = DataFrame({'value' : [4,9,4,2]},index=date_range('2014-11-08',freq='6s',periods=4))
209+
assert_frame_equal(result,expected)
210+
211+
result = df.resample('7s', how='sum')
212+
expected = DataFrame({'value' : [4,10,4,1]},index=date_range('2014-11-08',freq='7s',periods=4))
213+
assert_frame_equal(result,expected)
214+
215+
result = df.resample('11s', how='sum')
216+
expected = DataFrame({'value' : [11,8]},index=date_range('2014-11-08',freq='11s',periods=2))
217+
assert_frame_equal(result,expected)
218+
219+
result = df.resample('13s', how='sum')
220+
expected = DataFrame({'value' : [13,6]},index=date_range('2014-11-08',freq='13s',periods=2))
221+
assert_frame_equal(result,expected)
222+
223+
result = df.resample('17s', how='sum')
224+
expected = DataFrame({'value' : [16,3]},index=date_range('2014-11-08',freq='17s',periods=2))
225+
assert_frame_equal(result,expected)
226+
179227
def test_resample_basic_from_daily(self):
180228
# from daily
181229
dti = DatetimeIndex(

0 commit comments

Comments
 (0)