From 1a7d3dc4ef868f7fb51ebf51421476adcb4d48e8 Mon Sep 17 00:00:00 2001 From: discort Date: Wed, 8 Aug 2018 22:01:26 +0300 Subject: [PATCH] Fixed MemoryError in resampling with NaT in TimedeltaIndex --- doc/source/whatsnew/v0.23.5.txt | 2 +- pandas/core/resample.py | 3 +-- pandas/tests/test_resample.py | 10 ++++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.23.5.txt b/doc/source/whatsnew/v0.23.5.txt index 916a246355b5f..f9247bc04a96e 100644 --- a/doc/source/whatsnew/v0.23.5.txt +++ b/doc/source/whatsnew/v0.23.5.txt @@ -33,7 +33,7 @@ Bug Fixes **Groupby/Resample/Rolling** -- +- Bug in :meth:`DataFrame.resample` when resampling ``NaT`` in ``TimeDeltaIndex`` (:issue:`13223`). - **Missing** diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 08bfec89a22a8..4a3625b2d7a2d 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -1383,8 +1383,7 @@ def _get_time_delta_bins(self, ax): data=[], freq=self.freq, name=ax.name) return binner, [], labels - start = ax[0] - end = ax[-1] + start, end = ax.min(), ax.max() labels = binner = TimedeltaIndex(start=start, end=end, freq=self.freq, diff --git a/pandas/tests/test_resample.py b/pandas/tests/test_resample.py index de4dc2bcf25a4..d11077668359b 100644 --- a/pandas/tests/test_resample.py +++ b/pandas/tests/test_resample.py @@ -2887,6 +2887,16 @@ def test_asfreq_bug(self): freq='1T')) assert_frame_equal(result, expected) + def test_resample_with_nat(self): + # GH 13223 + index = pd.to_timedelta(['0s', pd.NaT, '2s']) + result = DataFrame({'value': [2, 3, 5]}, index).resample('1s').mean() + expected = DataFrame({'value': [2.5, np.nan, 5.0]}, + index=timedelta_range('0 day', + periods=3, + freq='1S')) + assert_frame_equal(result, expected) + class TestResamplerGrouper(object):