Skip to content

BUG: fix Resampling a Series with a timezone using kind='period' (GH5430) #5432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pandas/tseries/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down