Skip to content

Add test and fix for categorical series .shift #10495. #10498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,4 @@ Bug Fixes

- Bug in `pandas.concat` with ``axis=0`` when column is of dtype ``category`` (:issue:`10177`)
- Bug in ``read_msgpack`` where input type is not always checked (:issue:`10369`)
- Bug in ``Categorical`` ``Series.shift`` (:issue:`10495`)
15 changes: 14 additions & 1 deletion pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,13 @@ def shift(self, periods, axis=0):
""" shift the block by periods, possibly upcast """
# convert integer to float if necessary. need to do a lot more than
# that, handle boolean etc also
new_values, fill_value = com._maybe_upcast(self.values)
if isinstance(self.values, Categorical):
# hack toward fixing issue 10495
values = self.values._codes
else:
values = self.values
new_values, fill_value = com._maybe_upcast(values)

# make sure array sent to np.roll is c_contiguous
f_ordered = new_values.flags.f_contiguous
if f_ordered:
Expand All @@ -906,6 +912,13 @@ def shift(self, periods, axis=0):
if f_ordered:
new_values = new_values.T

if isinstance(self.values, Categorical):
# hack toward fixing issue 10495
new_values[np.isnan(new_values)] = -1
new_values = Categorical.from_codes(new_values,
categories=self.values.categories)


return [make_block(new_values,
ndim=self.ndim, fastpath=True,
placement=self.mgr_locs)]
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,18 @@ def test_comparison_with_unknown_scalars(self):
self.assert_numpy_array_equal(cat == 4 , [False, False, False])
self.assert_numpy_array_equal(cat != 4 , [True, True, True])

def test_shift(self):
# GH10495
# Series.shift should not depend on the dtype being categorical or not
values = ['a', 'b', 'c']
shifts = [-1, 0, 1]
results = [['b', 'c', np.nan], ['a', 'b', 'c'], [np.nan, 'a', 'b']]

for shift, result in zip(shifts, results):
b = pd.Series(pd.Categorical(result, categories=values))
a = pd.Series(values, dtype='category').shift(shift)
self.assert_series_equal(a, b)


class TestCategoricalAsBlock(tm.TestCase):
_multiprocess_can_split_ = True
Expand Down