From ba1716930bcf956990b3545c1339fb64be77c2c4 Mon Sep 17 00:00:00 2001 From: discort Date: Thu, 23 Aug 2018 22:58:31 +0300 Subject: [PATCH 1/2] fixed asfreq in Resampler.asfreq using TDI --- doc/source/whatsnew/v0.24.0.txt | 1 + pandas/core/resample.py | 10 ---------- pandas/tests/test_resample.py | 18 ++++++++++++------ 3 files changed, 13 insertions(+), 16 deletions(-) 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..ef515ef33d5d3 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -1157,16 +1157,6 @@ def _get_binner_for_time(self): 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] return binner diff --git a/pandas/tests/test_resample.py b/pandas/tests/test_resample.py index b60fd10d745c1..245a6b13e5ee7 100644 --- a/pandas/tests/test_resample.py +++ b/pandas/tests/test_resample.py @@ -626,12 +626,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 +2927,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): From bd8f31a92b1e03d1fd6141fd837969caafdf1a8f Mon Sep 17 00:00:00 2001 From: discort Date: Wed, 29 Aug 2018 17:01:05 +0300 Subject: [PATCH 2/2] update comments for _adjust_binner_for_upsample --- pandas/core/resample.py | 11 +++++++++-- pandas/tests/test_resample.py | 1 - 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index ef515ef33d5d3..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,7 +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 """ + """ + 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 245a6b13e5ee7..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