Skip to content

Retain name in PeriodIndex resample #12771

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

Closed
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.18.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ Bug Fixes


- Bug in ``CategoricalIndex.get_loc`` returns different result from regular ``Index`` (:issue:`12531`)

- Bug in ``PeriodIndex.resample`` where name not propagated (:issue:`12769`)


- Bug in ``SparseSeries.shape`` ignores ``fill_value`` (:issue:`10452`)
Expand Down
6 changes: 4 additions & 2 deletions pandas/tseries/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,16 +672,18 @@ def aggregate(self, arg, *args, **kwargs):
def _get_new_index(self):
""" return our new index """
ax = self.ax
ax_attrs = ax._get_attributes_dict()
ax_attrs['freq'] = self.freq
obj = self._selected_obj

if len(ax) == 0:
new_index = PeriodIndex(data=[], freq=self.freq)
new_index = PeriodIndex(data=[], **ax_attrs)
return obj.reindex(new_index)

start = ax[0].asfreq(self.freq, how=self.convention)
end = ax[-1].asfreq(self.freq, how='end')

return period_range(start, end, freq=self.freq)
return period_range(start, end, **ax_attrs)

def _downsample(self, how, **kwargs):
"""
Expand Down
67 changes: 38 additions & 29 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
# pylint: disable-msg=E1101,W0612
import calendar
from datetime import datetime, time, timedelta
import sys
import operator
import sys
import warnings
from datetime import datetime, time, timedelta
from numpy.random import rand
from numpy.testing.decorators import slow

import nose
import numpy as np
import pandas.tseries.frequencies as frequencies
import pandas.index as _index
import pandas.lib as lib
import pandas.tslib as tslib
import pandas.index as _index
import pandas as pd
from pandas import (Index, Series, DataFrame, isnull, date_range, Timestamp,
Period, DatetimeIndex, Int64Index, to_datetime,
bdate_range, Float64Index, NaT, timedelta_range, Timedelta)

from pandas.compat.numpy_compat import np_datetime64_compat
import pandas as pd
import pandas.compat as compat
import pandas.core.common as com
import pandas.core.datetools as datetools
import pandas.tseries.frequencies as frequencies
import pandas.tseries.offsets as offsets
import pandas.tseries.tools as tools


from pandas.util.testing import assert_series_equal, assert_almost_equal,\
_skip_if_has_locale
import pandas.util.testing as tm

from pandas.tslib import iNaT

from pandas import (
Index, Series, DataFrame, isnull, date_range, Timestamp, Period,
DatetimeIndex, Int64Index, to_datetime, bdate_range, Float64Index,
NaT, timedelta_range, Timedelta, _np_version_under1p8, concat,
PeriodIndex)
from pandas.compat import range, long, StringIO, lrange, lmap, zip, product
from numpy.random import rand
from pandas.util.testing import assert_frame_equal
from pandas.compat.numpy_compat import np_datetime64_compat
from pandas.core.common import PerformanceWarning
import pandas.compat as compat
import pandas.core.common as com
from pandas import concat
from pandas import _np_version_under1p8

from numpy.testing.decorators import slow
from pandas.tslib import iNaT
from pandas.util.testing import (
assert_frame_equal, assert_series_equal, assert_almost_equal,
_skip_if_has_locale)

randn = np.random.randn

Expand Down Expand Up @@ -2249,15 +2245,27 @@ def test_concat_datetime_datetime64_frame(self):
def test_period_resample(self):
# GH3609
s = Series(range(100), index=date_range(
'20130101', freq='s', periods=100), dtype='float')
'20130101', freq='s', periods=100, name='idx'), 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')])
index = PeriodIndex([
Period('2013-01-01 00:00', 'T'),
Period('2013-01-01 00:01', 'T')], name='idx')
expected = Series([34.5, 79.5], index=index)
result = s.to_period().resample('T', kind='period').mean()
assert_series_equal(result, expected)
result2 = s.resample('T', kind='period').mean()
assert_series_equal(result2, expected)

def test_empty_period_index_resample(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests this, which was untested

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the issue number (or in this case the PR number as you are fixing something unrealted to the original issue)

# GH12771
index = PeriodIndex(start='2000', periods=0, freq='D', name='idx')
s = Series(index=index)
result = s.resample('M').sum()
# after GH12774 is resolved, this should be a PeriodIndex
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great. put a pointer to this PR in that issue (and maybe a checkbox), so we don't forget.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

expected_index = DatetimeIndex([], name='idx')
expected = Series(index=expected_index)
assert_series_equal(result, expected)

def test_period_resample_with_local_timezone_pytz(self):
# GH5430
tm._skip_if_no_pytz()
Expand Down Expand Up @@ -2297,7 +2305,7 @@ def test_period_resample_with_local_timezone_dateutil(self):
end = datetime(year=2013, month=11, day=2, hour=0, minute=0,
tzinfo=dateutil.tz.tzutc())

index = pd.date_range(start, end, freq='H')
index = pd.date_range(start, end, freq='H', name='idx')

series = pd.Series(1, index=index)
series = series.tz_convert(local_timezone)
Expand All @@ -2306,7 +2314,8 @@ def test_period_resample_with_local_timezone_dateutil(self):
# Create the expected series
# Index is moved back a day with the timezone conversion from UTC to
# Pacific
expected_index = (pd.period_range(start=start, end=end, freq='D') - 1)
expected_index = (pd.period_range(start=start, end=end, freq='D',
name='idx') - 1)
expected = pd.Series(1, index=expected_index)
assert_series_equal(result, expected)

Expand Down