Skip to content

ENH: fill_value argument for shift #15486 #24128

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

Merged
merged 113 commits into from
Dec 26, 2018
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
113 commits
Select commit Hold shift + click to select a range
c3e550c
Merge pull request #3 from pandas-dev/master
ahcub Dec 6, 2018
9ab2d4d
ENH: fill_value argument for shift
ahcub Dec 6, 2018
6b4789d
!U remove the data frame part form the time series
ahcub Dec 6, 2018
57c087e
!U remove the data frame part form the time series
ahcub Dec 6, 2018
1852ea6
!U fix formatting
ahcub Dec 6, 2018
3461271
!U add test for data_frame
ahcub Dec 6, 2018
8b5cedb
!U remove fill_value from tshift
ahcub Dec 6, 2018
27226fa
!U remove unrelevant change
ahcub Dec 6, 2018
29b8a6e
!U add doc for new fill_value argument for shift method
ahcub Dec 6, 2018
cf6b2dd
!U add GH number
ahcub Dec 6, 2018
9f9962c
!U add versionated tag
ahcub Dec 6, 2018
4c7b762
!U add fill_value to the group by interface
ahcub Dec 6, 2018
b36ca43
!U replace np.nan to None
ahcub Dec 6, 2018
a36e7c7
!U replace np.nan to None
ahcub Dec 6, 2018
4c2ec2c
!U add fill_value to the locations where it was missing
ahcub Dec 7, 2018
fbf3d73
!U fix series test
ahcub Dec 7, 2018
bb722a6
!U fix method name and values comparison for the new test
ahcub Dec 7, 2018
1b1fdc2
!U fix dataframe constructor
ahcub Dec 7, 2018
c3f462c
!U change the assert method used for new tests
ahcub Dec 7, 2018
e44b514
!U change formatting and doc scripts
ahcub Dec 8, 2018
38f621c
!U add doc strings
ahcub Dec 8, 2018
d9d9fdb
!U add check for fill_value present in the categorical categories
ahcub Dec 8, 2018
cc6f370
!U change test for frame
ahcub Dec 9, 2018
3619410
!U change test for series shift with fill value to avoid formatting e…
ahcub Dec 9, 2018
ca5ba24
!U remove dot
ahcub Dec 9, 2018
c195a13
!U remove empty line
ahcub Dec 9, 2018
af90a00
!U fix the line lenght
ahcub Dec 9, 2018
2f9b712
!U format the exception message
ahcub Dec 9, 2018
bbfe7f9
!U fix indents
ahcub Dec 9, 2018
ef55afb
!U fix indents
ahcub Dec 9, 2018
ce3d3c7
!U remove whitespaces
ahcub Dec 9, 2018
e6103a4
!U remove whitespaces
ahcub Dec 9, 2018
d18b6b7
!U fix the line width
ahcub Dec 9, 2018
931df66
!U fix the imports width
ahcub Dec 9, 2018
83f9157
Merge branch 'master' into master
ahcub Dec 9, 2018
81c01bc
!U restore the fill_value
ahcub Dec 9, 2018
4ae4d0b
!U revert imports
ahcub Dec 9, 2018
d4d43a3
!U revert new lines deletion
ahcub Dec 9, 2018
1366161
!U revert changes in imports
ahcub Dec 9, 2018
2643aa5
!U revert changes in imports
ahcub Dec 9, 2018
c010413
!U revert changes in imports
ahcub Dec 9, 2018
689bf8e
!U revert imports
ahcub Dec 9, 2018
463964b
!U revert imports
ahcub Dec 9, 2018
b90b00f
!U revert imports
ahcub Dec 9, 2018
18fab2f
!U revert imports
ahcub Dec 9, 2018
cb03215
!U revert imports
ahcub Dec 9, 2018
c8242f3
!U revert imports
ahcub Dec 9, 2018
8e946cc
!U revert imports
ahcub Dec 9, 2018
69e47f3
!U revert imports
ahcub Dec 9, 2018
8765bee
!U revert imports
ahcub Dec 9, 2018
9d10a6b
!U remove doc from the doc
ahcub Dec 9, 2018
6f4078a
!U change description
ahcub Dec 9, 2018
4227dda
!U change check for isna instead of is None
ahcub Dec 9, 2018
ce721ae
!U remove the -1 implementation detail
ahcub Dec 9, 2018
c743004
!U change doc description + added .. versionadded:: 0.24.0 tag
ahcub Dec 9, 2018
03e3bd4
!U add test for sparse array shift with fill_value
ahcub Dec 11, 2018
e4313da
!U add test for gorupby
ahcub Dec 11, 2018
85d2b16
!U add PR specification to the test
ahcub Dec 11, 2018
da90e89
!U adjust types
ahcub Dec 11, 2018
d71be8a
!U fix the groupby test
ahcub Dec 11, 2018
aca6c9c
!U add check for fill value in defining the type for result data set
ahcub Dec 11, 2018
20361c7
!U fix the types in the test
ahcub Dec 11, 2018
599ccb7
!U fix the test for groupby shift
ahcub Dec 12, 2018
1ac273a
!U update notation of the base shift method
ahcub Dec 12, 2018
b2074a8
!U test categorical shift
ahcub Dec 12, 2018
0fe2f95
!U change the test for categorical
ahcub Dec 12, 2018
7d33f21
!U add check for fill value is not None
ahcub Dec 12, 2018
578859b
!U change n_rows to smaller value
ahcub Dec 12, 2018
a016df3
!U select Z column
ahcub Dec 12, 2018
3947394
!U select as DataFrame
ahcub Dec 12, 2018
b266a50
!U fix formatting errors
ahcub Dec 12, 2018
fa808a4
!U add specification comment
ahcub Dec 13, 2018
71b8df1
!U change test for categorical
ahcub Dec 13, 2018
31a844e
!U add check for different parameters
ahcub Dec 13, 2018
18cbd95
!U move the check to the top
ahcub Dec 13, 2018
d068150
!U remove index test
ahcub Dec 13, 2018
e665f7d
!U remove the None from the list
ahcub Dec 14, 2018
c5a95cb
!U change the sparse ArrayTest
ahcub Dec 14, 2018
b03a3fd
change the way for parametrizing a test
ahcub Dec 17, 2018
d9efb45
update the doc
ahcub Dec 17, 2018
60595c9
update doc
ahcub Dec 17, 2018
7fc9900
add a test for dtype
ahcub Dec 17, 2018
e1a83e2
fix the parametrize arguments
ahcub Dec 17, 2018
7b587c2
!U change the doc formatting
ahcub Dec 18, 2018
7bf6768
!U remove the trailing whitespaces
ahcub Dec 18, 2018
09f0fde
Update pandas/core/generic.py
TomAugspurger Dec 18, 2018
6d65cfa
!U try to remove res_type
ahcub Dec 18, 2018
2162c18
Merge remote-tracking branch 'origin/master'
ahcub Dec 18, 2018
3f9b62b
!U add test for categorical shift with fill value as nan
ahcub Dec 18, 2018
8fa8a15
add different variation of nan
ahcub Dec 18, 2018
988507a
Merge branch 'master' into master
ahcub Dec 20, 2018
6c96108
added base test
TomAugspurger Dec 20, 2018
eb48cfe
change the fill_value so the result data set stays int
ahcub Dec 25, 2018
a72cffe
add a blank line before the version tag
ahcub Dec 25, 2018
25b4661
add a blank line before the version tag
ahcub Dec 25, 2018
d817d0c
Merge remote-tracking branch 'origin/master'
ahcub Dec 25, 2018
0afad10
change the fill_value check for na
ahcub Dec 25, 2018
bb67905
parametrize on fill value with decorator instead of the test internals
ahcub Dec 25, 2018
f5aad59
change check for is None to isna
ahcub Dec 25, 2018
025f0db
change check for is None to isna
ahcub Dec 25, 2018
8ce460a
change check for is None to isna
ahcub Dec 25, 2018
dce5aa1
change check for is None to isna
ahcub Dec 25, 2018
d60632a
fix imports sorting
ahcub Dec 25, 2018
a0ab35d
fix imports sorting
ahcub Dec 25, 2018
fd23842
fix imports sorting
ahcub Dec 25, 2018
3503e86
fix imports sorting
ahcub Dec 25, 2018
674f15d
Add the whatsnew entry
ahcub Dec 25, 2018
3c896ab
change func to meth
ahcub Dec 25, 2018
b9d335a
change the description in whatsnew
ahcub Dec 25, 2018
4fcca01
fix typo
ahcub Dec 25, 2018
a24f5c8
Add categorical to the description
ahcub Dec 25, 2018
a92ddfb
change import of isna in test
ahcub Dec 26, 2018
a03cbf5
add backticks for fill_value and add the issue number
ahcub Dec 26, 2018
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 @@ -3867,9 +3867,9 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
method=method)

@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 @@ -8868,13 +8868,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.copy()

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
4 changes: 2 additions & 2 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1260,12 +1260,12 @@ def diff(self, n, axis=1):
new_values = algos.diff(self.values, n, axis=axis)
return [self.make_block(values=new_values)]

def shift(self, periods, axis=0):
def shift(self, periods, axis=0, fill_value=np.nan):
""" 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)

# 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 @@ -3714,8 +3714,9 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
regex=regex, method=method)

@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):
"""
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/frame/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,19 @@ def test_shift_categorical(self):
xp = DataFrame({'one': s1.shift(1), 'two': s2.shift(1)})
assert_frame_equal(rs, xp)

def test_shift_fill_value(self):
df = DataFrame(np.random.randnint(5), index=date_range('1/1/2000',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the gh number as a comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

periods=5,
freq='H'))
result = df.shift(1, fill_value=0)
tm.assert_equal(result.iloc[0, 0].value, 0)
tm.assert_equal(result.iloc[1, 0].value, df.iloc[0, 0].value)

result = df.shift(2, fill_value=0)
tm.assert_equal(result.iloc[0, 0].value, 0)
tm.assert_equal(result.iloc[1, 0].value, 0)
tm.assert_equal(result.iloc[2, 0].value, df.iloc[0, 0].value)

def test_shift_empty(self):
# Regression test for #8019
df = DataFrame({'foo': []})
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/series/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@ def test_shift2(self):
idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-04'])
pytest.raises(NullFrequencyError, idx.shift, 1)

def test_shift_fill_value(self):
ts = Series(np.random.randn(5),
index=date_range('1/1/2000', periods=5, freq='H'))

# fill_value should have no effect on shift with freq
result = ts.shift(1, freq='5T', fill_value=0)
exp_index = ts.index.shift(1, freq='5T')
tm.assert_index_equal(result.index, exp_index)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compare actual series

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback not sure what you mean. the point of this check is to check the index and values independently, can you please specify which series you meant?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are testing series here, why are you even shifting the index? if you are testing indexes they go elsewhere in pandas/tests/indexing, which I don't see at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I removed the test for index, I just followed the example above the test which did similar checks but without the fill_value

tm.assert_equal(result.iloc[0].value, ts.iloc[0].value)

# check that fill value works
result = ts.shift(1, fill_value=0.0)
tm.assert_equal(result.iloc[0].value, 0.0)
tm.assert_equal(result.iloc[1].value, ts.iloc[0].value)

result = ts.shift(2, fill_value=0.0)
tm.assert_equal(result.iloc[0].value, 0.0)
tm.assert_equal(result.iloc[1].value, 0.0)
tm.assert_equal(result.iloc[2].value, ts.iloc[0].value)

def test_shift_dst(self):
# GH 13926
dates = date_range('2016-11-06', freq='H', periods=10, tz='US/Eastern')
Expand Down