From 4aa01fcc294c85e821e6ba89a59ffeb1c776f40b Mon Sep 17 00:00:00 2001 From: Aleksey Bilogur Date: Mon, 27 Feb 2017 18:31:29 -0500 Subject: [PATCH 1/2] Enable fill-value in Series.shift, DataFrame.shift. --- pandas/core/frame.py | 4 ++-- pandas/core/generic.py | 6 ++++-- pandas/core/internals.py | 4 ++-- pandas/core/series.py | 5 +++-- 4 files changed, 11 insertions(+), 8 deletions(-) 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..f6a9e64fe822c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5032,13 +5032,15 @@ 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): + import pdb; pdb.set_trace() + 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..57e7f69c337f2 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -1060,12 +1060,12 @@ 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..f22d0affd8440 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): + import pdb; pdb.set_trace() + 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 """ From 8ef9305609bcc4794cdecb16f9769dca64750535 Mon Sep 17 00:00:00 2001 From: Aleksey Bilogur Date: Mon, 27 Feb 2017 18:39:22 -0500 Subject: [PATCH 2/2] flake8 fixes. --- pandas/core/generic.py | 5 ++--- pandas/core/internals.py | 3 ++- pandas/core/series.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index f6a9e64fe822c..68c4db04d8750 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5033,14 +5033,13 @@ 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, fill_value=np.nan): - import pdb; pdb.set_trace() - 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, fill_value=fill_value) + 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 57e7f69c337f2..91a34b5e053a0 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -1065,7 +1065,8 @@ def shift(self, periods, axis=0, fill_value=np.nan, mgr=None): # 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, fill_value=fill_value) + 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 f22d0affd8440..470fe0e2b729d 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2383,8 +2383,8 @@ def fillna(self, value=None, method=None, axis=None, inplace=False, @Appender(generic._shared_docs['shift'] % _shared_doc_kwargs) def shift(self, periods=1, freq=None, axis=0, fill_value=np.nan): - import pdb; pdb.set_trace() - return super(Series, self).shift(periods=periods, freq=freq, axis=axis, fill_value=fill_value) + 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 """