Skip to content

Commit 05b2189

Browse files
max-sixtyjreback
authored andcommitted
BUG: Retain name in PeriodIndex resample
closes #12769 Author: Maximilian Roos <[email protected]> Closes #12771 from MaximilianR/retain-period-index-name-on-resample and squashes the following commits: 7b17a7e [Maximilian Roos] name retained in PeriodIndex resample
1 parent ee4c2c7 commit 05b2189

File tree

4 files changed

+117
-110
lines changed

4 files changed

+117
-110
lines changed

doc/source/whatsnew/v0.18.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ Bug Fixes
220220

221221

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

225225

226226

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_resample.py

+79-6
Original file line numberDiff line numberDiff line change
@@ -1891,6 +1891,85 @@ def test_monthly_upsample(self):
18911891
expected = expected.asfreq(targ, 'ffill').to_period()
18921892
assert_series_equal(result, expected)
18931893

1894+
def test_resample_basic(self):
1895+
# GH3609
1896+
s = Series(range(100), index=date_range(
1897+
'20130101', freq='s', periods=100, name='idx'), dtype='float')
1898+
s[10:30] = np.nan
1899+
index = PeriodIndex([
1900+
Period('2013-01-01 00:00', 'T'),
1901+
Period('2013-01-01 00:01', 'T')], name='idx')
1902+
expected = Series([34.5, 79.5], index=index)
1903+
result = s.to_period().resample('T', kind='period').mean()
1904+
assert_series_equal(result, expected)
1905+
result2 = s.resample('T', kind='period').mean()
1906+
assert_series_equal(result2, expected)
1907+
1908+
def test_resample_empty(self):
1909+
1910+
# GH12771
1911+
index = PeriodIndex(start='2000', periods=0, freq='D', name='idx')
1912+
s = Series(index=index)
1913+
result = s.resample('M').sum()
1914+
1915+
# after GH12774 is resolved, this should be a PeriodIndex
1916+
expected_index = DatetimeIndex([], name='idx')
1917+
expected = Series(index=expected_index)
1918+
assert_series_equal(result, expected)
1919+
1920+
def test_with_local_timezone_pytz(self):
1921+
# GH5430
1922+
tm._skip_if_no_pytz()
1923+
import pytz
1924+
1925+
local_timezone = pytz.timezone('America/Los_Angeles')
1926+
1927+
start = datetime(year=2013, month=11, day=1, hour=0, minute=0,
1928+
tzinfo=pytz.utc)
1929+
# 1 day later
1930+
end = datetime(year=2013, month=11, day=2, hour=0, minute=0,
1931+
tzinfo=pytz.utc)
1932+
1933+
index = pd.date_range(start, end, freq='H')
1934+
1935+
series = pd.Series(1, index=index)
1936+
series = series.tz_convert(local_timezone)
1937+
result = series.resample('D', kind='period').mean()
1938+
1939+
# Create the expected series
1940+
# Index is moved back a day with the timezone conversion from UTC to
1941+
# Pacific
1942+
expected_index = (pd.period_range(start=start, end=end, freq='D') - 1)
1943+
expected = pd.Series(1, index=expected_index)
1944+
assert_series_equal(result, expected)
1945+
1946+
def test_with_local_timezone_dateutil(self):
1947+
# GH5430
1948+
tm._skip_if_no_dateutil()
1949+
import dateutil
1950+
1951+
local_timezone = 'dateutil/America/Los_Angeles'
1952+
1953+
start = datetime(year=2013, month=11, day=1, hour=0, minute=0,
1954+
tzinfo=dateutil.tz.tzutc())
1955+
# 1 day later
1956+
end = datetime(year=2013, month=11, day=2, hour=0, minute=0,
1957+
tzinfo=dateutil.tz.tzutc())
1958+
1959+
index = pd.date_range(start, end, freq='H', name='idx')
1960+
1961+
series = pd.Series(1, index=index)
1962+
series = series.tz_convert(local_timezone)
1963+
result = series.resample('D', kind='period').mean()
1964+
1965+
# Create the expected series
1966+
# Index is moved back a day with the timezone conversion from UTC to
1967+
# Pacific
1968+
expected_index = (pd.period_range(start=start, end=end, freq='D',
1969+
name='idx') - 1)
1970+
expected = pd.Series(1, index=expected_index)
1971+
assert_series_equal(result, expected)
1972+
18941973
def test_fill_method_and_how_upsample(self):
18951974
# GH2073
18961975
s = Series(np.arange(9, dtype='int64'),
@@ -1983,12 +2062,6 @@ def test_upsample_daily_business_daily(self):
19832062
expected = ts.asfreq('H', how='s').reindex(exp_rng)
19842063
assert_series_equal(result, expected)
19852064

1986-
def test_resample_empty(self):
1987-
ts = _simple_pts('1/1/2000', '2/1/2000')[:0]
1988-
1989-
result = ts.resample('A').asfreq()
1990-
self.assertEqual(len(result), 0)
1991-
19922065
def test_resample_irregular_sparse(self):
19932066
dr = date_range(start='1/1/2012', freq='5min', periods=1000)
19942067
s = Series(np.array(100), index=dr)

pandas/tseries/tests/test_timeseries.py

+33-101
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

@@ -1247,23 +1243,6 @@ def test_asfreq_keep_index_name(self):
12471243
tm.assert_equal(index_name, df.index.name)
12481244
tm.assert_equal(index_name, df.asfreq('10D').index.name)
12491245

1250-
def test_asfreq_resample_set_correct_freq(self):
1251-
# GH5613
1252-
# we test if .asfreq() and .resample() set the correct value for .freq
1253-
df = pd.DataFrame({'date': ["2012-01-01", "2012-01-02", "2012-01-03"],
1254-
'col': [1, 2, 3]})
1255-
df = df.set_index(pd.to_datetime(df.date))
1256-
1257-
# testing the settings before calling .asfreq() and .resample()
1258-
self.assertEqual(df.index.freq, None)
1259-
self.assertEqual(df.index.inferred_freq, 'D')
1260-
1261-
# does .asfreq() set .freq correctly?
1262-
self.assertEqual(df.asfreq('D').index.freq, 'D')
1263-
1264-
# does .resample() set .freq correctly?
1265-
self.assertEqual(df.resample('D').asfreq().index.freq, 'D')
1266-
12671246
def test_promote_datetime_date(self):
12681247
rng = date_range('1/1/2000', periods=20)
12691248
ts = Series(np.random.randn(20), index=rng)
@@ -2246,69 +2225,22 @@ def test_concat_datetime_datetime64_frame(self):
22462225
# it works!
22472226
pd.concat([df1, df2_obj])
22482227

2249-
def test_period_resample(self):
2250-
# GH3609
2251-
s = Series(range(100), index=date_range(
2252-
'20130101', freq='s', periods=100), dtype='float')
2253-
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')])
2256-
result = s.to_period().resample('T', kind='period').mean()
2257-
assert_series_equal(result, expected)
2258-
result2 = s.resample('T', kind='period').mean()
2259-
assert_series_equal(result2, expected)
2260-
2261-
def test_period_resample_with_local_timezone_pytz(self):
2262-
# GH5430
2263-
tm._skip_if_no_pytz()
2264-
import pytz
2265-
2266-
local_timezone = pytz.timezone('America/Los_Angeles')
2267-
2268-
start = datetime(year=2013, month=11, day=1, hour=0, minute=0,
2269-
tzinfo=pytz.utc)
2270-
# 1 day later
2271-
end = datetime(year=2013, month=11, day=2, hour=0, minute=0,
2272-
tzinfo=pytz.utc)
2273-
2274-
index = pd.date_range(start, end, freq='H')
2275-
2276-
series = pd.Series(1, index=index)
2277-
series = series.tz_convert(local_timezone)
2278-
result = series.resample('D', kind='period').mean()
2279-
2280-
# Create the expected series
2281-
# Index is moved back a day with the timezone conversion from UTC to
2282-
# Pacific
2283-
expected_index = (pd.period_range(start=start, end=end, freq='D') - 1)
2284-
expected = pd.Series(1, index=expected_index)
2285-
assert_series_equal(result, expected)
2286-
2287-
def test_period_resample_with_local_timezone_dateutil(self):
2288-
# GH5430
2289-
tm._skip_if_no_dateutil()
2290-
import dateutil
2291-
2292-
local_timezone = 'dateutil/America/Los_Angeles'
2293-
2294-
start = datetime(year=2013, month=11, day=1, hour=0, minute=0,
2295-
tzinfo=dateutil.tz.tzutc())
2296-
# 1 day later
2297-
end = datetime(year=2013, month=11, day=2, hour=0, minute=0,
2298-
tzinfo=dateutil.tz.tzutc())
2228+
def test_asfreq_resample_set_correct_freq(self):
2229+
# GH5613
2230+
# we test if .asfreq() and .resample() set the correct value for .freq
2231+
df = pd.DataFrame({'date': ["2012-01-01", "2012-01-02", "2012-01-03"],
2232+
'col': [1, 2, 3]})
2233+
df = df.set_index(pd.to_datetime(df.date))
22992234

2300-
index = pd.date_range(start, end, freq='H')
2235+
# testing the settings before calling .asfreq() and .resample()
2236+
self.assertEqual(df.index.freq, None)
2237+
self.assertEqual(df.index.inferred_freq, 'D')
23012238

2302-
series = pd.Series(1, index=index)
2303-
series = series.tz_convert(local_timezone)
2304-
result = series.resample('D', kind='period').mean()
2239+
# does .asfreq() set .freq correctly?
2240+
self.assertEqual(df.asfreq('D').index.freq, 'D')
23052241

2306-
# Create the expected series
2307-
# Index is moved back a day with the timezone conversion from UTC to
2308-
# Pacific
2309-
expected_index = (pd.period_range(start=start, end=end, freq='D') - 1)
2310-
expected = pd.Series(1, index=expected_index)
2311-
assert_series_equal(result, expected)
2242+
# does .resample() set .freq correctly?
2243+
self.assertEqual(df.resample('D').asfreq().index.freq, 'D')
23122244

23132245
def test_pickle(self):
23142246

0 commit comments

Comments
 (0)