Skip to content

Fixing shift() for ExtensionArray #23947

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

Merged
merged 12 commits into from
Dec 9, 2018
8 changes: 5 additions & 3 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,12 @@ def shift(self, periods=1):
"""
# Note: this implementation assumes that `self.dtype.na_value` can be
# stored in an instance of your ExtensionArray with `self.dtype`.
if periods == 0:
if len == 0 or periods == 0:
return self.copy()
empty = self._from_sequence([self.dtype.na_value] * abs(periods),
dtype=self.dtype)
empty = self._from_sequence(
[self.dtype.na_value] * min(abs(periods), len(self)),
dtype=self.dtype
)
if periods > 0:
a = empty
b = self[:-periods]
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/extension/base/interface.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pytest

from pandas.compat import StringIO

Expand Down Expand Up @@ -86,3 +87,16 @@ def test_isna_extension_array(self, data_missing):
assert not na.all()

assert na.dtype._is_boolean

@pytest.mark.parametrize('periods, indices', [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this to methods.py I think (and obviously move the overrides in arrow as well)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since moving this to methods takes it out of the BaseInterfaceTests class, I can discard the overrides in arrow, since, currently, there is no test class within it deriving from BaseMethodsTests (or BaseExtensionTests for that matter). Is that acceptable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see my comment below

[-4, [-1, -1]],
[-1, [1, -1]],
[0, [0, 1]],
[1, [-1, 0]],
[4, [-1, -1]]
])
def test_shift(self, data, periods, indices):
subset = data[:2]
result = subset.shift(periods)
expected = subset.take(indices, allow_fill=True)
self.assert_extension_array_equal(result, expected)