From 2302973d01fb0231455838edc709dd84bb2160aa Mon Sep 17 00:00:00 2001 From: Kevin Stone Date: Mon, 4 Nov 2013 10:54:24 -0800 Subject: [PATCH] Added failing test case for resampling TimeSeries by period with a local timezone. Related to #5340. Signed-off-by: Kevin Stone Added Test Case for #3609. Signed-off-by: Kevin Stone Fixes Grouping by Period with Timezones The timestamp generated to partition the data frame doesn't include timezone information, so it was creating the wrong groups. It also had the frequency ('D') hard coded. Fixes #5340 and #3609. Signed-off-by: Kevin Stone --- pandas/tseries/resample.py | 4 +++- pandas/tseries/tests/test_timeseries.py | 31 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/pandas/tseries/resample.py b/pandas/tseries/resample.py index 96ff8c47abc1e..5377543ac8c54 100644 --- a/pandas/tseries/resample.py +++ b/pandas/tseries/resample.py @@ -192,7 +192,9 @@ def _get_time_period_bins(self, axis): labels = binner = PeriodIndex(start=axis[0], end=axis[-1], freq=self.freq) - end_stamps = (labels + 1).asfreq('D', 's').to_timestamp() + end_stamps = (labels + 1).asfreq(self.freq, 's').to_timestamp() + if axis.tzinfo: + end_stamps = end_stamps.tz_localize(axis.tzinfo) bins = axis.searchsorted(end_stamps, side='left') return binner, bins, labels diff --git a/pandas/tseries/tests/test_timeseries.py b/pandas/tseries/tests/test_timeseries.py index b8144c2b5eab9..51939ac22e956 100644 --- a/pandas/tseries/tests/test_timeseries.py +++ b/pandas/tseries/tests/test_timeseries.py @@ -1836,6 +1836,37 @@ def test_concat_datetime_datetime64_frame(self): # it works! pd.concat([df1, df2_obj]) + def test_period_resample(self): + # GH3609 + s = Series(range(100),index=date_range('20130101', freq='s', periods=100), dtype='float') + s[10:30] = np.nan + expected = Series([34.5, 79.5], index=[Period('2013-01-01 00:00', 'T'), Period('2013-01-01 00:01', 'T')]) + result = s.to_period().resample('T', kind='period') + assert_series_equal(result, expected) + result2 = s.resample('T', kind='period') + assert_series_equal(result2, expected) + + def test_period_resample_with_local_timezone(self): + # GH5430 + _skip_if_no_pytz() + import pytz + + local_timezone = pytz.timezone('America/Los_Angeles') + + start = datetime(year=2013, month=11, day=1, hour=0, minute=0, tzinfo=pytz.utc) + # 1 day later + end = datetime(year=2013, month=11, day=2, hour=0, minute=0, tzinfo=pytz.utc) + + index = pd.date_range(start, end, freq='H') + + series = pd.Series(1, index=index) + series = series.tz_convert(local_timezone) + result = series.resample('D', kind='period') + # Create the expected series + expected_index = (pd.period_range(start=start, end=end, freq='D') - 1) # Index is moved back a day with the timezone conversion from UTC to Pacific + expected = pd.Series(1, index=expected_index) + assert_series_equal(result, expected) + def _simple_ts(start, end, freq='D'): rng = date_range(start, end, freq=freq)