-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 14 commits
c3e550c
9ab2d4d
6b4789d
57c087e
1852ea6
3461271
8b5cedb
27226fa
29b8a6e
cf6b2dd
9f9962c
4c7b762
b36ca43
a36e7c7
4c2ec2c
fbf3d73
bb722a6
1b1fdc2
c3f462c
e44b514
38f621c
d9d9fdb
cc6f370
3619410
ca5ba24
c195a13
af90a00
2f9b712
bbfe7f9
ef55afb
ce3d3c7
e6103a4
d18b6b7
931df66
83f9157
81c01bc
4ae4d0b
d4d43a3
1366161
2643aa5
c010413
689bf8e
463964b
b90b00f
18fab2f
cb03215
c8242f3
8e946cc
69e47f3
8765bee
9d10a6b
6f4078a
4227dda
ce721ae
c743004
03e3bd4
e4313da
85d2b16
da90e89
d71be8a
aca6c9c
20361c7
599ccb7
1ac273a
b2074a8
0fe2f95
7d33f21
578859b
a016df3
3947394
b266a50
fa808a4
71b8df1
31a844e
18cbd95
d068150
e665f7d
c5a95cb
b03a3fd
d9efb45
60595c9
7fc9900
e1a83e2
7b587c2
7bf6768
09f0fde
6d65cfa
2162c18
3f9b62b
8fa8a15
988507a
6c96108
eb48cfe
a72cffe
25b4661
d817d0c
0afad10
bb67905
f5aad59
025f0db
8ce460a
dce5aa1
d60632a
a0ab35d
fd23842
3503e86
674f15d
3c896ab
b9d335a
4fcca01
a24f5c8
a92ddfb
a03cbf5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8830,6 +8830,12 @@ def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None, | |
extend the index when shifting and preserve the original data. | ||
axis : {0 or 'index', 1 or 'columns', None}, default None | ||
Shift direction. | ||
|
||
.. versionchanged:: 0.24.0 | ||
|
||
fill_value : None or value, default None (NaN). | ||
Filling the empty space after shift with value provided by | ||
the user instead of NaN | ||
|
||
Returns | ||
------- | ||
|
@@ -8865,16 +8871,27 @@ def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None, | |
2 NaN 15.0 18.0 | ||
3 NaN 30.0 33.0 | ||
4 NaN 45.0 48.0 | ||
|
||
.. versionchanged:: 0.24.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can remove this directive and add a sentence like You can specify the value to fill with. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
>>> df.shift(periods=3, fill_value=0.0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dedent to align with the other examples. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
Col1 Col2 Col3 | ||
0 0.0 0.0 0.0 | ||
1 0.0 0.0 0.0 | ||
2 0.0 0.0 0.0 | ||
3 10.0 13.0 17.0 | ||
4 20.0 23.0 27.0 | ||
""") | ||
|
||
@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=None): | ||
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) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -320,6 +320,20 @@ 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): | ||
# GH #24128 | ||
df = DataFrame(np.random.randnint(5), index=date_range('1/1/2000', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add the gh number as a comment There was a problem hiding this comment. Choose a reason for hiding this commentThe 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': []}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,6 +129,27 @@ 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): | ||
# GH #24128 | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. compare actual series There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting is a bit off. The version added needs to go at the end. Something like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done