diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index eb2fef482ff17..9f7f4c2b81645 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -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] diff --git a/pandas/tests/extension/base/interface.py b/pandas/tests/extension/base/interface.py index 00a480d311b58..52d70a31721fd 100644 --- a/pandas/tests/extension/base/interface.py +++ b/pandas/tests/extension/base/interface.py @@ -1,4 +1,5 @@ import numpy as np +import pytest from pandas.compat import StringIO @@ -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', [ + [-3, [-1, -1]], + [-1, [1, -1]], + [0, [0, 1]], + [1, [-1, 0]], + [3, [-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)