From 7cf1be9749fb5151aa3ad91f85a7d9052088ad20 Mon Sep 17 00:00:00 2001 From: Bran Yang Date: Wed, 10 Feb 2016 01:39:46 +0800 Subject: [PATCH 1/2] Fix #12169 - Resample category data with timedelta index --- pandas/core/series.py | 3 ++- pandas/tseries/tests/test_resample.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 68ae58737916b..a6cfacc369122 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -177,7 +177,8 @@ def __init__(self, data=None, index=None, dtype=None, name=None, default=np.nan) else: data = np.nan - elif isinstance(index, PeriodIndex): + # GH #12169 + elif isinstance(index, (PeriodIndex, TimedeltaIndex)): data = ([data.get(i, nan) for i in index] if data else np.nan) else: diff --git a/pandas/tseries/tests/test_resample.py b/pandas/tseries/tests/test_resample.py index c761a35649ba9..233d795015089 100644 --- a/pandas/tseries/tests/test_resample.py +++ b/pandas/tseries/tests/test_resample.py @@ -1147,6 +1147,18 @@ def test_resample_base_with_timedeltaindex(self): self.assertTrue(without_base.index.equals(exp_without_base)) self.assertTrue(with_base.index.equals(exp_with_base)) + def test_resample_categorical_data_with_timedeltaindex(self): + # GH #12169 + df = DataFrame({'Group_obj': 'A'}, + index=pd.to_timedelta(list(range(20)), unit='s')) + df['Group'] = df['Group_obj'].astype('category') + result = df.resample('10s').agg(lambda x: (x.value_counts().index[0])) + expected = DataFrame({'Group_obj': ['A', 'A'], + 'Group': ['A', 'A']}, + index=pd.to_timedelta([0, 10], unit='s')) + expected = expected.reindex_axis(['Group_obj', 'Group'], 1) + tm.assert_frame_equal(result, expected) + def test_resample_daily_anchored(self): rng = date_range('1/1/2000 0:00:00', periods=10000, freq='T') ts = Series(np.random.randn(len(rng)), index=rng) From 4a5605fab91af55ae34a4d87160748f87a1bf63a Mon Sep 17 00:00:00 2001 From: Bran Yang Date: Wed, 10 Feb 2016 21:24:36 +0800 Subject: [PATCH 2/2] add tests to Series/test_constructors; and update whatsnew --- doc/source/whatsnew/v0.18.0.txt | 2 ++ pandas/tests/series/test_constructors.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index 421822380c2da..3b181bd874c19 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -796,3 +796,5 @@ Bug Fixes - Bug in ``.skew`` and ``.kurt`` due to roundoff error for highly similar values (:issue:`11974`) - Bug in ``buffer_rd_bytes`` src->buffer could be freed more than once if reading failed, causing a segfault (:issue:`12098`) + +- Bug in ``df.resample`` on categorical data with TimedeltaIndex (:issue:`12169`) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 6ae24bbccfa74..deaa0cd081fc0 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -519,6 +519,24 @@ def test_constructor_dict_multiindex(self): ser = ser.reindex(index=expected.index) check(ser, expected) + def test_constructor_dict_timedelta_index(self): + # GH #12169 : Resample category data with timedelta index + # construct Series from dict as data and TimedeltaIndex as index + # will result NaN in result Series data + expected = Series( + data=['A'] * 3, + index=pd.to_timedelta([0, 10, 20], unit='s') + ) + + result = Series( + data={pd.to_timedelta(0, unit='s'): 'A', + pd.to_timedelta(10, unit='s'): 'A', + pd.to_timedelta(20, unit='s'): 'A'}, + index=pd.to_timedelta([0, 10, 20], unit='s') + ) + # this should work + assert_series_equal(result, expected) + def test_constructor_subclass_dict(self): data = tm.TestSubDict((x, 10.0 * x) for x in range(10)) series = Series(data)