Skip to content

Commit e9558d3

Browse files
BranYangjreback
authored andcommitted
Fix #12169 - Resample category data with timedelta index
closes #12169 Author: Bran Yang <[email protected]> Closes #12271 from BranYang/issue12169 and squashes the following commits: 4a5605f [Bran Yang] add tests to Series/test_constructors; and update whatsnew 7cf1be9 [Bran Yang] Fix #12169 - Resample category data with timedelta index
1 parent e048150 commit e9558d3

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

doc/source/whatsnew/v0.18.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,7 @@ Bug Fixes
845845
- Bug in ``DataFrame.query`` containing an assignment (:issue:`8664`)
846846

847847
- Bug in ``from_msgpack`` where ``__contains__()`` fails for columns of the unpacked ``DataFrame``, if the ``DataFrame`` has object columns. (:issue:`11880`)
848+
- Bug in ``df.resample()`` on categorical data with ``TimedeltaIndex`` (:issue:`12169`)
848849

849850

850851
- Bug in timezone info lost when broadcasting scalar datetime to ``DataFrame`` (:issue:`11682`)

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/tests/series/test_constructors.py

+18
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,24 @@ def test_constructor_dict_multiindex(self):
519519
ser = ser.reindex(index=expected.index)
520520
check(ser, expected)
521521

522+
def test_constructor_dict_timedelta_index(self):
523+
# GH #12169 : Resample category data with timedelta index
524+
# construct Series from dict as data and TimedeltaIndex as index
525+
# will result NaN in result Series data
526+
expected = Series(
527+
data=['A', 'B', 'C'],
528+
index=pd.to_timedelta([0, 10, 20], unit='s')
529+
)
530+
531+
result = Series(
532+
data={pd.to_timedelta(0, unit='s'): 'A',
533+
pd.to_timedelta(10, unit='s'): 'B',
534+
pd.to_timedelta(20, unit='s'): 'C'},
535+
index=pd.to_timedelta([0, 10, 20], unit='s')
536+
)
537+
# this should work
538+
assert_series_equal(result, expected)
539+
522540
def test_constructor_subclass_dict(self):
523541
data = tm.TestSubDict((x, 10.0 * x) for x in range(10))
524542
series = Series(data)

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)