From 83a15afce495e9bbd5b2103127f309e518c81ae0 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Fri, 10 Jul 2015 21:38:33 -0700 Subject: [PATCH] ENH: Added functionality in resample to resolve #10530 Added tests for updated resample function Changed if-statement to be lower-bound inclusive Undid previous change to if statement Fixed typo in resample.py Fixed typo in _get_time_bins Updated _resample_timestamp function Updated condition in if-statement Updated exceptions raised in resample Moved test case into proper file Fixed typo in test case Updated tests for resampling fix ENH: Updated code for fixing #10530 Removed extraneous print statements from tests Moved code for fix to _get_time_delta_bins function Updated tests for resample TimeDeltaIndex with base Updated code for resample TimeDeltaIndex with base Removed print statements from test case Removed print statement in tests Added note to what's new Removed extra whitespace Removed addtional whitespace Removed whitespace Removed whitespace in resample.py Removed more whitespace in resample.py Removed more whitespace Fixed syntax in what's new --- doc/source/whatsnew/v0.17.0.txt | 2 ++ pandas/tseries/resample.py | 4 ++++ pandas/tseries/tests/test_resample.py | 14 ++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index 70d616ca72c1b..643226b84baff 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -94,6 +94,8 @@ Other enhancements - Enable `read_hdf` to be used without specifying a key when the HDF file contains a single dataset (:issue:`10443`) +- Added functionality to use ``base`` when resampling a ``TimeDeltaIndex`` (:issue:`10530`) + - ``DatetimeIndex`` can be instantiated using strings contains ``NaT`` (:issue:`7599`) - The string parsing of ``to_datetime``, ``Timestamp`` and ``DatetimeIndex`` has been made consistent. (:issue:`7599`) diff --git a/pandas/tseries/resample.py b/pandas/tseries/resample.py index 53c1292204f71..0ecdb43895f07 100644 --- a/pandas/tseries/resample.py +++ b/pandas/tseries/resample.py @@ -238,6 +238,10 @@ def _get_time_delta_bins(self, ax): end_stamps = labels + 1 bins = ax.searchsorted(end_stamps, side='left') + # Addresses GH #10530 + if self.base > 0: + labels += type(self.freq)(self.base) + return binner, bins, labels def _get_time_period_bins(self, ax): diff --git a/pandas/tseries/tests/test_resample.py b/pandas/tseries/tests/test_resample.py index 9ec336466266f..3a533b5e0b298 100644 --- a/pandas/tseries/tests/test_resample.py +++ b/pandas/tseries/tests/test_resample.py @@ -11,6 +11,7 @@ from pandas.core.groupby import DataError from pandas.tseries.index import date_range +from pandas.tseries.tdi import timedelta_range from pandas.tseries.offsets import Minute, BDay from pandas.tseries.period import period_range, PeriodIndex, Period from pandas.tseries.resample import DatetimeIndex, TimeGrouper @@ -627,6 +628,19 @@ def test_resample_base(self): freq='5min') self.assertTrue(resampled.index.equals(exp_rng)) + def test_resample_base_with_timedeltaindex(self): + rng = timedelta_range(start = '0s', periods = 25, freq = 's') + ts = Series(np.random.randn(len(rng)), index = rng) + + with_base = ts.resample('2s', base = 5) + without_base = ts.resample('2s') + + exp_without_base = timedelta_range(start = '0s', end = '25s', freq = '2s') + exp_with_base = timedelta_range(start = '5s', end = '29s', freq = '2s') + + self.assertTrue(without_base.index.equals(exp_without_base)) + self.assertTrue(with_base.index.equals(exp_with_base)) + def test_resample_daily_anchored(self): rng = date_range('1/1/2000 0:00:00', periods=10000, freq='T') ts = Series(np.random.randn(len(rng)), index=rng)