Skip to content

Commit c980035

Browse files
committed
Updated
1 parent b29dfc6 commit c980035

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

doc/source/whatsnew/v0.24.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ ExtensionType Changes
444444
the ``ExtensionDtype`` has gained the method ``construct_array_type`` (:issue:`21185`)
445445
- The ``ExtensionArray`` constructor, ``_from_sequence`` now take the keyword arg ``copy=False`` (:issue:`21185`)
446446
- Bug in :meth:`Series.get` for ``Series`` using ``ExtensionArray`` and integer index (:issue:`21257`)
447-
- :meth:`~Series.shift` now works with extension arrays, rather than raising an AttributeError (:isseu:`22386`)
447+
- :meth:`~Series.shift` now dispatches to :meth:`ExtensionArray.shift` (:issue:`22386`)
448448
- :meth:`Series.combine()` works correctly with :class:`~pandas.api.extensions.ExtensionArray` inside of :class:`Series` (:issue:`20825`)
449449
- :meth:`Series.combine()` with scalar argument now works for any function type (:issue:`21248`)
450450
-

pandas/core/arrays/base.py

+27
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,33 @@ def dropna(self):
400400

401401
return self[~self.isna()]
402402

403+
def shift(self, periods=1):
404+
# type: (int) -> ExtensionArray
405+
"""
406+
Shift values by desired number.
407+
408+
Newly introduced missing values are filled with
409+
``self.dtype.na_value``.
410+
411+
Parameters
412+
----------
413+
periods : int, default 1
414+
The number of periods to shift. Negative values are allowed
415+
for shifting backwards.
416+
417+
Returns
418+
-------
419+
shifted : ExtensionArray
420+
"""
421+
indexer = np.roll(np.arange(len(self)), periods)
422+
423+
if periods > 0:
424+
indexer[:periods] = -1
425+
else:
426+
indexer[periods:] = -1
427+
428+
return self.take(indexer, allow_fill=True)
429+
403430
def unique(self):
404431
"""Compute the ExtensionArray of unique values.
405432

pandas/core/internals/blocks.py

+2-18
Original file line numberDiff line numberDiff line change
@@ -2062,23 +2062,11 @@ def interpolate(self, method='pad', axis=0, inplace=False, limit=None,
20622062
placement=self.mgr_locs)
20632063

20642064
def shift(self, periods, axis=0, mgr=None):
2065-
# type: (int, int, Optional[BlockPlacement]) -> List[ExtensionBlock]
2066-
indexer = np.roll(np.arange(len(self)), periods)
2067-
2068-
if periods > 0:
2069-
indexer[:periods] = -1
2070-
else:
2071-
indexer[periods:] = -1
2072-
2073-
new_values = self.values.take(indexer, allow_fill=True)
2074-
return [self.make_block_same_class(new_values,
2065+
# type: (int, Optional[BlockPlacement]) -> List[ExtensionBlock]
2066+
return [self.make_block_same_class(self.values.shift(periods=periods),
20752067
placement=self.mgr_locs,
20762068
ndim=self.ndim)]
20772069

2078-
@property
2079-
def _ftype(self):
2080-
return getattr(self.values, '_pandas_ftype', Block._ftype)
2081-
20822070

20832071
class NumericBlock(Block):
20842072
__slots__ = ()
@@ -2702,10 +2690,6 @@ def _try_coerce_result(self, result):
27022690

27032691
return result
27042692

2705-
def shift(self, periods, axis=0, mgr=None):
2706-
return self.make_block_same_class(values=self.values.shift(periods),
2707-
placement=self.mgr_locs)
2708-
27092693
def to_dense(self):
27102694
# Categorical.get_values returns a DatetimeIndex for datetime
27112695
# categories, so we can't simply use `np.asarray(self.values)` like

pandas/tests/extension/base/methods.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ def test_container_shift_negative(self, data, frame, periods, indices):
157157
expected,
158158
pd.Series([1] * 5, name='B').shift(periods)
159159
], axis=1)
160-
compare = tm.assert_frame_equal
160+
compare = self.assert_frame_equal
161161
else:
162162
result = data.shift(periods)
163-
compare = tm.assert_series_equal
163+
compare = self.assert_series_equal
164164

165165
compare(result, expected)

0 commit comments

Comments
 (0)