Skip to content

Commit f82c10a

Browse files
discortjreback
authored andcommitted
BUG: resample with TimedeltaIndex, fenceposts are off (#22488)
1 parent 9df21d8 commit f82c10a

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ Groupby/Resample/Rolling
709709
datetime-like index leading to incorrect results and also segfault. (:issue:`21704`)
710710
- Bug in :meth:`Resampler.apply` when passing postiional arguments to applied func (:issue:`14615`).
711711
- Bug in :meth:`Series.resample` when passing ``numpy.timedelta64`` to `loffset` kwarg (:issue:`7687`).
712+
- Bug in :meth:`Resampler.asfreq` when frequency of ``TimedeltaIndex`` is a subperiod of a new frequency (:issue:`13022`).
712713

713714
Sparse
714715
^^^^^^

pandas/core/resample.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,10 @@ def _downsample(self, how, **kwargs):
963963
return self._wrap_result(result)
964964

965965
def _adjust_binner_for_upsample(self, binner):
966-
""" adjust our binner when upsampling """
966+
"""
967+
Adjust our binner when upsampling.
968+
The range of a new index should not be outside specified range
969+
"""
967970
if self.closed == 'right':
968971
binner = binner[1:]
969972
else:
@@ -1156,17 +1159,11 @@ def _get_binner_for_time(self):
11561159
return self.groupby._get_time_delta_bins(self.ax)
11571160

11581161
def _adjust_binner_for_upsample(self, binner):
1159-
""" adjust our binner when upsampling """
1160-
ax = self.ax
1161-
1162-
if is_subperiod(ax.freq, self.freq):
1163-
# We are actually downsampling
1164-
# but are in the asfreq path
1165-
# GH 12926
1166-
if self.closed == 'right':
1167-
binner = binner[1:]
1168-
else:
1169-
binner = binner[:-1]
1162+
"""
1163+
Adjust our binner when upsampling.
1164+
The range of a new index is allowed to be greater than original range
1165+
so we don't need to change the length of a binner, GH 13022
1166+
"""
11701167
return binner
11711168

11721169

pandas/tests/test_resample.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from pandas.compat import range, lrange, zip, OrderedDict
2727
from pandas.errors import UnsupportedFunctionCall
2828
import pandas.tseries.offsets as offsets
29-
from pandas.tseries.frequencies import to_offset
3029
from pandas.tseries.offsets import Minute, BDay
3130

3231
from pandas.core.groupby.groupby import DataError
@@ -626,12 +625,7 @@ def test_asfreq(self, series_and_frame, freq):
626625
obj = series_and_frame
627626

628627
result = obj.resample(freq).asfreq()
629-
if freq == '2D':
630-
new_index = obj.index.take(np.arange(0, len(obj.index), 2))
631-
new_index.freq = to_offset('2D')
632-
else:
633-
new_index = self.create_index(obj.index[0], obj.index[-1],
634-
freq=freq)
628+
new_index = self.create_index(obj.index[0], obj.index[-1], freq=freq)
635629
expected = obj.reindex(new_index)
636630
assert_almost_equal(result, expected)
637631

@@ -2932,6 +2926,17 @@ def test_resample_with_nat(self):
29322926
freq='1S'))
29332927
assert_frame_equal(result, expected)
29342928

2929+
def test_resample_as_freq_with_subperiod(self):
2930+
# GH 13022
2931+
index = timedelta_range('00:00:00', '00:10:00', freq='5T')
2932+
df = DataFrame(data={'value': [1, 5, 10]}, index=index)
2933+
result = df.resample('2T').asfreq()
2934+
expected_data = {'value': [1, np.nan, np.nan, np.nan, np.nan, 10]}
2935+
expected = DataFrame(data=expected_data,
2936+
index=timedelta_range('00:00:00',
2937+
'00:10:00', freq='2T'))
2938+
tm.assert_frame_equal(result, expected)
2939+
29352940

29362941
class TestResamplerGrouper(object):
29372942

0 commit comments

Comments
 (0)