Skip to content

Commit 7cf1be9

Browse files
committed
Fix #12169 - Resample category data with timedelta index
1 parent fe201a2 commit 7cf1be9

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pandas/core/series.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
177177
default=np.nan)
178178
else:
179179
data = np.nan
180-
elif isinstance(index, PeriodIndex):
180+
# GH #12169
181+
elif isinstance(index, (PeriodIndex, TimedeltaIndex)):
181182
data = ([data.get(i, nan) for i in index]
182183
if data else np.nan)
183184
else:

pandas/tseries/tests/test_resample.py

+12
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,18 @@ def test_resample_base_with_timedeltaindex(self):
11471147
self.assertTrue(without_base.index.equals(exp_without_base))
11481148
self.assertTrue(with_base.index.equals(exp_with_base))
11491149

1150+
def test_resample_categorical_data_with_timedeltaindex(self):
1151+
# GH #12169
1152+
df = DataFrame({'Group_obj': 'A'},
1153+
index=pd.to_timedelta(list(range(20)), unit='s'))
1154+
df['Group'] = df['Group_obj'].astype('category')
1155+
result = df.resample('10s').agg(lambda x: (x.value_counts().index[0]))
1156+
expected = DataFrame({'Group_obj': ['A', 'A'],
1157+
'Group': ['A', 'A']},
1158+
index=pd.to_timedelta([0, 10], unit='s'))
1159+
expected = expected.reindex_axis(['Group_obj', 'Group'], 1)
1160+
tm.assert_frame_equal(result, expected)
1161+
11501162
def test_resample_daily_anchored(self):
11511163
rng = date_range('1/1/2000 0:00:00', periods=10000, freq='T')
11521164
ts = Series(np.random.randn(len(rng)), index=rng)

0 commit comments

Comments
 (0)