diff --git a/pandas/core/frame.py b/pandas/core/frame.py index ce3481fc17c5b..d3284428059f0 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2827,9 +2827,9 @@ def fillna(self, value=None, method=None, axis=None, inplace=False, downcast=downcast, **kwargs) @Appender(_shared_docs['shift'] % _shared_doc_kwargs) - def shift(self, periods=1, freq=None, axis=0): + def shift(self, periods=1, freq=None, axis=0, fill_value=np.nan): return super(DataFrame, self).shift(periods=periods, freq=freq, - axis=axis) + axis=axis, fill_value=fill_value) def set_index(self, keys, drop=True, append=False, inplace=False, verify_integrity=False): diff --git a/pandas/core/generic.py b/pandas/core/generic.py index cdc37e00f70e0..68c4db04d8750 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5032,13 +5032,14 @@ def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None, """) @Appender(_shared_docs['shift'] % _shared_doc_kwargs) - def shift(self, periods=1, freq=None, axis=0): + def shift(self, periods=1, freq=None, axis=0, fill_value=np.nan): if periods == 0: return self block_axis = self._get_block_manager_axis(axis) if freq is None: - new_data = self._data.shift(periods=periods, axis=block_axis) + new_data = self._data.shift(periods=periods, axis=block_axis, + fill_value=fill_value) else: return self.tshift(periods, freq) diff --git a/pandas/core/internals.py b/pandas/core/internals.py index 6cd5eceed5f2a..91a34b5e053a0 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -1060,12 +1060,13 @@ def diff(self, n, axis=1, mgr=None): new_values = algos.diff(self.values, n, axis=axis) return [self.make_block(values=new_values, fastpath=True)] - def shift(self, periods, axis=0, mgr=None): + def shift(self, periods, axis=0, fill_value=np.nan, mgr=None): """ 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 = _maybe_upcast(self.values) + new_values, fill_value = _maybe_upcast(self.values, + fill_value=fill_value) # make sure array sent to np.roll is c_contiguous f_ordered = new_values.flags.f_contiguous diff --git a/pandas/core/series.py b/pandas/core/series.py index da47ab5dfb003..470fe0e2b729d 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2382,8 +2382,9 @@ def fillna(self, value=None, method=None, axis=None, inplace=False, **kwargs) @Appender(generic._shared_docs['shift'] % _shared_doc_kwargs) - def shift(self, periods=1, freq=None, axis=0): - return super(Series, self).shift(periods=periods, freq=freq, axis=axis) + def shift(self, periods=1, freq=None, axis=0, fill_value=np.nan): + return super(Series, self).shift(periods=periods, freq=freq, axis=axis, + fill_value=fill_value) def reindex_axis(self, labels, axis=0, **kwargs): """ for compatibility with higher dims """