Skip to content

ENH: fill_value argument for shift #15527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 3 additions & 2 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down