Skip to content

Commit a4369c2

Browse files
committed
Squashed commit of the following:
commit c4b0b97 Author: Tom Augspurger <[email protected]> Date: Thu Aug 16 14:36:39 2018 -0500 Slice based commit c980035 Author: Tom Augspurger <[email protected]> Date: Thu Aug 16 14:20:21 2018 -0500 Updated commit b29dfc6 Author: Tom Augspurger <[email protected]> Date: Thu Aug 16 10:45:38 2018 -0500 Support NDFrame.shift with EAs Uses take internally. Closes pandas-dev#22386 commit b5d81cf Author: William Ayd <[email protected]> Date: Thu Aug 16 03:54:18 2018 -0700 Bump pytest (pandas-dev#22320) commit f07a790 Author: jbrockmendel <[email protected]> Date: Thu Aug 16 03:46:58 2018 -0700 Make more of numpy_helper unnecessary (pandas-dev#22344) commit 7b80d4d Author: Graham Inggs <[email protected]> Date: Thu Aug 16 12:43:02 2018 +0200 Drop redundant TestLocale (pandas-dev#22349) commit 6bcfc46 Author: Matthew Roeschke <[email protected]> Date: Thu Aug 16 03:32:31 2018 -0700 Fix failing dateutil test (pandas-dev#22354) commit 86e8f23 Author: jbrockmendel <[email protected]> Date: Thu Aug 16 03:08:09 2018 -0700 remove last cython: nprofile comments (pandas-dev#22371) commit 70e6f7c Author: Joris Van den Bossche <[email protected]> Date: Wed Aug 15 18:09:50 2018 +0200 DOC: edit docstring example to prevent segfault (pandas-dev#21824) (pandas-dev#22368)
1 parent 31d401f commit a4369c2

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

doc/source/whatsnew/v0.24.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ ExtensionType Changes
462462
- Added ``ExtensionDtype._is_numeric`` for controlling whether an extension dtype is considered numeric (:issue:`22290`).
463463
- The ``ExtensionArray`` constructor, ``_from_sequence`` now take the keyword arg ``copy=False`` (:issue:`21185`)
464464
- Bug in :meth:`Series.get` for ``Series`` using ``ExtensionArray`` and integer index (:issue:`21257`)
465-
- :meth:`~Series.shift` now works with extension arrays, rather than raising an AttributeError (:isseu:`22386`)
465+
- :meth:`~Series.shift` now dispatches to :meth:`ExtensionArray.shift` (:issue:`22386`)
466466
- :meth:`Series.combine()` works correctly with :class:`~pandas.api.extensions.ExtensionArray` inside of :class:`Series` (:issue:`20825`)
467467
- :meth:`Series.combine()` with scalar argument now works for any function type (:issue:`21248`)
468468
- Added ``ExtensionDtype._is_numeric`` for controlling whether an extension dtype is considered numeric.

pandas/core/arrays/base.py

+30
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,36 @@ 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+
if periods == 0:
422+
return self.copy()
423+
empty = self._from_sequence([self.dtype.na_value] * abs(periods),
424+
dtype=self.dtype)
425+
if periods > 0:
426+
a = empty
427+
b = self[:-periods]
428+
else:
429+
a = self[abs(periods):]
430+
b = empty
431+
return self._concat_same_type([a, b])
432+
403433
def unique(self):
404434
"""Compute the ExtensionArray of unique values.
405435

pandas/core/internals/blocks.py

+2-15
Original file line numberDiff line numberDiff line change
@@ -2074,18 +2074,9 @@ def interpolate(self, method='pad', axis=0, inplace=False, limit=None,
20742074
limit=limit),
20752075
placement=self.mgr_locs)
20762076

2077-
20782077
def shift(self, periods, axis=0, mgr=None):
2079-
# type: (int, int, Optional[BlockPlacement]) -> List[ExtensionBlock]
2080-
indexer = np.roll(np.arange(len(self)), periods)
2081-
2082-
if periods > 0:
2083-
indexer[:periods] = -1
2084-
else:
2085-
indexer[periods:] = -1
2086-
2087-
new_values = self.values.take(indexer, allow_fill=True)
2088-
return [self.make_block_same_class(new_values,
2078+
# type: (int, Optional[BlockPlacement]) -> List[ExtensionBlock]
2079+
return [self.make_block_same_class(self.values.shift(periods=periods),
20892080
placement=self.mgr_locs,
20902081
ndim=self.ndim)]
20912082

@@ -2718,10 +2709,6 @@ def _try_coerce_result(self, result):
27182709

27192710
return result
27202711

2721-
def shift(self, periods, axis=0, mgr=None):
2722-
return self.make_block_same_class(values=self.values.shift(periods),
2723-
placement=self.mgr_locs)
2724-
27252712
def to_dense(self):
27262713
# Categorical.get_values returns a DatetimeIndex for datetime
27272714
# categories, so we can't simply use `np.asarray(self.values)` like

pandas/core/sparse/array.py

+5
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,11 @@ def map(self, mapper):
749749
return type(self)(sp_values, sparse_index=self.sp_index,
750750
fill_value=fill_value)
751751

752+
def shift(self, periods=1):
753+
if not self._null_fill_value:
754+
return super(SparseArray, self).shift(periods=periods)
755+
756+
752757
def get_values(self, fill=None):
753758
""" return a dense representation """
754759
# TODO: deprecate for to_dense?

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)