Skip to content

Commit 2302973

Browse files
committed
Added failing test case for resampling TimeSeries by period with a local timezone.
Related to #5340. Signed-off-by: Kevin Stone <[email protected]> Added Test Case for #3609. Signed-off-by: Kevin Stone <[email protected]> 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 <[email protected]>
1 parent c435e72 commit 2302973

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

pandas/tseries/resample.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ def _get_time_period_bins(self, axis):
192192
labels = binner = PeriodIndex(start=axis[0], end=axis[-1],
193193
freq=self.freq)
194194

195-
end_stamps = (labels + 1).asfreq('D', 's').to_timestamp()
195+
end_stamps = (labels + 1).asfreq(self.freq, 's').to_timestamp()
196+
if axis.tzinfo:
197+
end_stamps = end_stamps.tz_localize(axis.tzinfo)
196198
bins = axis.searchsorted(end_stamps, side='left')
197199

198200
return binner, bins, labels

pandas/tseries/tests/test_timeseries.py

+31
Original file line numberDiff line numberDiff line change
@@ -1836,6 +1836,37 @@ def test_concat_datetime_datetime64_frame(self):
18361836
# it works!
18371837
pd.concat([df1, df2_obj])
18381838

1839+
def test_period_resample(self):
1840+
# GH3609
1841+
s = Series(range(100),index=date_range('20130101', freq='s', periods=100), dtype='float')
1842+
s[10:30] = np.nan
1843+
expected = Series([34.5, 79.5], index=[Period('2013-01-01 00:00', 'T'), Period('2013-01-01 00:01', 'T')])
1844+
result = s.to_period().resample('T', kind='period')
1845+
assert_series_equal(result, expected)
1846+
result2 = s.resample('T', kind='period')
1847+
assert_series_equal(result2, expected)
1848+
1849+
def test_period_resample_with_local_timezone(self):
1850+
# GH5430
1851+
_skip_if_no_pytz()
1852+
import pytz
1853+
1854+
local_timezone = pytz.timezone('America/Los_Angeles')
1855+
1856+
start = datetime(year=2013, month=11, day=1, hour=0, minute=0, tzinfo=pytz.utc)
1857+
# 1 day later
1858+
end = datetime(year=2013, month=11, day=2, hour=0, minute=0, tzinfo=pytz.utc)
1859+
1860+
index = pd.date_range(start, end, freq='H')
1861+
1862+
series = pd.Series(1, index=index)
1863+
series = series.tz_convert(local_timezone)
1864+
result = series.resample('D', kind='period')
1865+
# Create the expected series
1866+
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
1867+
expected = pd.Series(1, index=expected_index)
1868+
assert_series_equal(result, expected)
1869+
18391870

18401871
def _simple_ts(start, end, freq='D'):
18411872
rng = date_range(start, end, freq=freq)

0 commit comments

Comments
 (0)