Skip to content

Commit 7b17a7e

Browse files
committed
name retained in PeriodIndex resample
1 parent 101d81d commit 7b17a7e

File tree

3 files changed

+43
-32
lines changed

3 files changed

+43
-32
lines changed

doc/source/whatsnew/v0.18.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ Bug Fixes
192192

193193

194194
- Bug in ``CategoricalIndex.get_loc`` returns different result from regular ``Index`` (:issue:`12531`)
195-
195+
- Bug in ``PeriodIndex.resample`` where name not propagated (:issue:`12769`)
196196

197197

198198
- Bug in ``SparseSeries.shape`` ignores ``fill_value`` (:issue:`10452`)

pandas/tseries/resample.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -672,16 +672,18 @@ def aggregate(self, arg, *args, **kwargs):
672672
def _get_new_index(self):
673673
""" return our new index """
674674
ax = self.ax
675+
ax_attrs = ax._get_attributes_dict()
676+
ax_attrs['freq'] = self.freq
675677
obj = self._selected_obj
676678

677679
if len(ax) == 0:
678-
new_index = PeriodIndex(data=[], freq=self.freq)
680+
new_index = PeriodIndex(data=[], **ax_attrs)
679681
return obj.reindex(new_index)
680682

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

684-
return period_range(start, end, freq=self.freq)
686+
return period_range(start, end, **ax_attrs)
685687

686688
def _downsample(self, how, **kwargs):
687689
"""

pandas/tseries/tests/test_timeseries.py

+38-29
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,38 @@
11
# pylint: disable-msg=E1101,W0612
22
import calendar
3-
from datetime import datetime, time, timedelta
4-
import sys
53
import operator
4+
import sys
65
import warnings
6+
from datetime import datetime, time, timedelta
7+
from numpy.random import rand
8+
from numpy.testing.decorators import slow
9+
710
import nose
811
import numpy as np
9-
import pandas.tseries.frequencies as frequencies
12+
import pandas.index as _index
1013
import pandas.lib as lib
1114
import pandas.tslib as tslib
12-
import pandas.index as _index
13-
import pandas as pd
14-
from pandas import (Index, Series, DataFrame, isnull, date_range, Timestamp,
15-
Period, DatetimeIndex, Int64Index, to_datetime,
16-
bdate_range, Float64Index, NaT, timedelta_range, Timedelta)
1715

18-
from pandas.compat.numpy_compat import np_datetime64_compat
16+
import pandas as pd
17+
import pandas.compat as compat
18+
import pandas.core.common as com
1919
import pandas.core.datetools as datetools
20+
import pandas.tseries.frequencies as frequencies
2021
import pandas.tseries.offsets as offsets
2122
import pandas.tseries.tools as tools
22-
23-
24-
from pandas.util.testing import assert_series_equal, assert_almost_equal,\
25-
_skip_if_has_locale
2623
import pandas.util.testing as tm
27-
28-
from pandas.tslib import iNaT
29-
24+
from pandas import (
25+
Index, Series, DataFrame, isnull, date_range, Timestamp, Period,
26+
DatetimeIndex, Int64Index, to_datetime, bdate_range, Float64Index,
27+
NaT, timedelta_range, Timedelta, _np_version_under1p8, concat,
28+
PeriodIndex)
3029
from pandas.compat import range, long, StringIO, lrange, lmap, zip, product
31-
from numpy.random import rand
32-
from pandas.util.testing import assert_frame_equal
30+
from pandas.compat.numpy_compat import np_datetime64_compat
3331
from pandas.core.common import PerformanceWarning
34-
import pandas.compat as compat
35-
import pandas.core.common as com
36-
from pandas import concat
37-
from pandas import _np_version_under1p8
38-
39-
from numpy.testing.decorators import slow
32+
from pandas.tslib import iNaT
33+
from pandas.util.testing import (
34+
assert_frame_equal, assert_series_equal, assert_almost_equal,
35+
_skip_if_has_locale)
4036

4137
randn = np.random.randn
4238

@@ -2249,15 +2245,27 @@ def test_concat_datetime_datetime64_frame(self):
22492245
def test_period_resample(self):
22502246
# GH3609
22512247
s = Series(range(100), index=date_range(
2252-
'20130101', freq='s', periods=100), dtype='float')
2248+
'20130101', freq='s', periods=100, name='idx'), dtype='float')
22532249
s[10:30] = np.nan
2254-
expected = Series([34.5, 79.5], index=[Period(
2255-
'2013-01-01 00:00', 'T'), Period('2013-01-01 00:01', 'T')])
2250+
index = PeriodIndex([
2251+
Period('2013-01-01 00:00', 'T'),
2252+
Period('2013-01-01 00:01', 'T')], name='idx')
2253+
expected = Series([34.5, 79.5], index=index)
22562254
result = s.to_period().resample('T', kind='period').mean()
22572255
assert_series_equal(result, expected)
22582256
result2 = s.resample('T', kind='period').mean()
22592257
assert_series_equal(result2, expected)
22602258

2259+
def test_empty_period_index_resample(self):
2260+
# GH12771
2261+
index = PeriodIndex(start='2000', periods=0, freq='D', name='idx')
2262+
s = Series(index=index)
2263+
result = s.resample('M').sum()
2264+
# after GH12774 is resolved, this should be a PeriodIndex
2265+
expected_index = DatetimeIndex([], name='idx')
2266+
expected = Series(index=expected_index)
2267+
assert_series_equal(result, expected)
2268+
22612269
def test_period_resample_with_local_timezone_pytz(self):
22622270
# GH5430
22632271
tm._skip_if_no_pytz()
@@ -2297,7 +2305,7 @@ def test_period_resample_with_local_timezone_dateutil(self):
22972305
end = datetime(year=2013, month=11, day=2, hour=0, minute=0,
22982306
tzinfo=dateutil.tz.tzutc())
22992307

2300-
index = pd.date_range(start, end, freq='H')
2308+
index = pd.date_range(start, end, freq='H', name='idx')
23012309

23022310
series = pd.Series(1, index=index)
23032311
series = series.tz_convert(local_timezone)
@@ -2306,7 +2314,8 @@ def test_period_resample_with_local_timezone_dateutil(self):
23062314
# Create the expected series
23072315
# Index is moved back a day with the timezone conversion from UTC to
23082316
# Pacific
2309-
expected_index = (pd.period_range(start=start, end=end, freq='D') - 1)
2317+
expected_index = (pd.period_range(start=start, end=end, freq='D',
2318+
name='idx') - 1)
23102319
expected = pd.Series(1, index=expected_index)
23112320
assert_series_equal(result, expected)
23122321

0 commit comments

Comments
 (0)