Skip to content

Commit

Permalink
Fix pandas-dev#12169 - Resample category data with timedelta index
Browse files Browse the repository at this point in the history
closes pandas-dev#12169

Author: Bran Yang <[email protected]>

Closes pandas-dev#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 pandas-dev#12169 - Resample category data with timedelta index
  • Loading branch information
BranYang authored and cldy committed Feb 11, 2016
1 parent 2653e68 commit fa1e2c8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ Bug Fixes
- Bug in ``DataFrame.query`` containing an assignment (:issue:`8664`)

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


- Bug in timezone info lost when broadcasting scalar datetime to ``DataFrame`` (:issue:`11682`)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', 'B', 'C'],
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'): 'B',
pd.to_timedelta(20, unit='s'): 'C'},
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)
Expand Down
12 changes: 12 additions & 0 deletions pandas/tseries/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fa1e2c8

Please sign in to comment.