diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 3a360b09ae789..1979bde796452 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -709,6 +709,7 @@ Groupby/Resample/Rolling datetime-like index leading to incorrect results and also segfault. (:issue:`21704`) - Bug in :meth:`Resampler.apply` when passing postiional arguments to applied func (:issue:`14615`). - Bug in :meth:`Series.resample` when passing ``numpy.timedelta64`` to `loffset` kwarg (:issue:`7687`). +- Bug in :meth:`Resampler.asfreq` when frequency of ``TimedeltaIndex`` is a subperiod of a new frequency (:issue:`13022`). Sparse ^^^^^^ diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 2ada4d758d463..1ef8a0854887b 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -963,7 +963,10 @@ def _downsample(self, how, **kwargs): return self._wrap_result(result) def _adjust_binner_for_upsample(self, binner): - """ adjust our binner when upsampling """ + """ + Adjust our binner when upsampling. + The range of a new index should not be outside specified range + """ if self.closed == 'right': binner = binner[1:] else: @@ -1156,17 +1159,11 @@ def _get_binner_for_time(self): return self.groupby._get_time_delta_bins(self.ax) def _adjust_binner_for_upsample(self, binner): - """ adjust our binner when upsampling """ - ax = self.ax - - if is_subperiod(ax.freq, self.freq): - # We are actually downsampling - # but are in the asfreq path - # GH 12926 - if self.closed == 'right': - binner = binner[1:] - else: - binner = binner[:-1] + """ + Adjust our binner when upsampling. + The range of a new index is allowed to be greater than original range + so we don't need to change the length of a binner, GH 13022 + """ return binner diff --git a/pandas/tests/test_resample.py b/pandas/tests/test_resample.py index b60fd10d745c1..530a683c02f9d 100644 --- a/pandas/tests/test_resample.py +++ b/pandas/tests/test_resample.py @@ -26,7 +26,6 @@ from pandas.compat import range, lrange, zip, OrderedDict from pandas.errors import UnsupportedFunctionCall import pandas.tseries.offsets as offsets -from pandas.tseries.frequencies import to_offset from pandas.tseries.offsets import Minute, BDay from pandas.core.groupby.groupby import DataError @@ -626,12 +625,7 @@ def test_asfreq(self, series_and_frame, freq): obj = series_and_frame result = obj.resample(freq).asfreq() - if freq == '2D': - new_index = obj.index.take(np.arange(0, len(obj.index), 2)) - new_index.freq = to_offset('2D') - else: - new_index = self.create_index(obj.index[0], obj.index[-1], - freq=freq) + new_index = self.create_index(obj.index[0], obj.index[-1], freq=freq) expected = obj.reindex(new_index) assert_almost_equal(result, expected) @@ -2932,6 +2926,17 @@ def test_resample_with_nat(self): freq='1S')) assert_frame_equal(result, expected) + def test_resample_as_freq_with_subperiod(self): + # GH 13022 + index = timedelta_range('00:00:00', '00:10:00', freq='5T') + df = DataFrame(data={'value': [1, 5, 10]}, index=index) + result = df.resample('2T').asfreq() + expected_data = {'value': [1, np.nan, np.nan, np.nan, np.nan, 10]} + expected = DataFrame(data=expected_data, + index=timedelta_range('00:00:00', + '00:10:00', freq='2T')) + tm.assert_frame_equal(result, expected) + class TestResamplerGrouper(object):