Skip to content

Commit c6274d3

Browse files
committed
DOC: update the pandas.Series.shift docstring
2 parents 0828c25 + aacbf85 commit c6274d3

File tree

1 file changed

+78
-11
lines changed

1 file changed

+78
-11
lines changed

pandas/core/generic.py

+78-11
Original file line numberDiff line numberDiff line change
@@ -8012,26 +8012,93 @@ def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None,
80128012
errors=errors)
80138013

80148014
_shared_docs['shift'] = ("""
8015-
Shift index by desired number of periods with an optional time freq
8015+
Shift index by desired number of periods with an optional time `freq`.
8016+
8017+
When `freq` is not passed, shift the index without realigning the data.
8018+
If `freq` is passed (in this case, the index must be date or datetime,
8019+
or it will raises a `NotImplementedError`), the index will be
8020+
increased using the periods and the `freq`.
80168021
80178022
Parameters
80188023
----------
80198024
periods : int
8020-
Number of periods to move, can be positive or negative
8025+
Number of periods to shift; can be positive or negative.
80218026
freq : DateOffset, timedelta, or time rule string, optional
80228027
Increment to use from the tseries module or time rule (e.g. 'EOM').
8023-
See Notes.
8024-
axis : %(axes_single_arg)s
8025-
8026-
Notes
8027-
-----
8028-
If freq is specified then the index values are shifted but the data
8029-
is not realigned. That is, use freq if you would like to extend the
8030-
index when shifting and preserve the original data.
8028+
If `freq` is specified then the index values are shifted but the
8029+
data is not realigned. That is, use `freq` if you would like to
8030+
extend the index when shifting and preserve the original data.
8031+
axis : {0 or ‘index’, 1 or ‘columns’, None}, default None
8032+
Shift direction.
80318033
80328034
Returns
80338035
-------
8034-
shifted : %(klass)s
8036+
%(klass)s
8037+
Copy of input object, shifted.
8038+
8039+
See Also
8040+
--------
8041+
tshift : Shift the time index, using the index’s frequency if
8042+
available.
8043+
8044+
Examples
8045+
--------
8046+
>>> df = pd.DataFrame({'Col1': [10, 20, 15, 30, 45],
8047+
... 'Col2': [13, 23, 18, 33, 48],
8048+
... 'Col3': [17, 27, 22, 37, 52]})
8049+
8050+
>>> df.shift(periods=3)
8051+
Col1 Col2 Col3
8052+
0 NaN NaN NaN
8053+
1 NaN NaN NaN
8054+
2 NaN NaN NaN
8055+
3 10.0 13.0 17.0
8056+
4 20.0 23.0 27.0
8057+
8058+
>>> df.shift(periods=1, axis=1)
8059+
Col1 Col2 Col3
8060+
0 NaN 10.0 13.0
8061+
1 NaN 20.0 23.0
8062+
2 NaN 15.0 18.0
8063+
3 NaN 30.0 33.0
8064+
4 NaN 45.0 48.0
8065+
8066+
**freq parameter**
8067+
8068+
When using the freq parameter with a :class:pandas.DateTimeIndex
8069+
the index values are shifted but the data is not realigned.
8070+
8071+
>>> names = ['Joaquim', 'Maria', 'Emanuel', 'Jussara', 'Geraldo']
8072+
>>> dates = pd.date_range(start='2018-03-05 11:15:00',
8073+
... freq='5D', periods=5)
8074+
>>> df = pd.DataFrame(data={'names': names}, index=dates)
8075+
>>> df
8076+
names
8077+
2018-03-05 11:15:00 Joaquim
8078+
2018-03-10 11:15:00 Maria
8079+
2018-03-15 11:15:00 Emanuel
8080+
2018-03-20 11:15:00 Jussara
8081+
2018-03-25 11:15:00 Geraldo
8082+
8083+
Shift the index by 2 days (offset-alias 'D').
8084+
8085+
>>> df.shift(periods=2, freq='D')
8086+
names
8087+
2018-03-07 11:15:00 Joaquim
8088+
2018-03-12 11:15:00 Maria
8089+
2018-03-17 11:15:00 Emanuel
8090+
2018-03-22 11:15:00 Jussara
8091+
2018-03-27 11:15:00 Geraldo
8092+
8093+
Shift the index by 75 minutes (offset-alias 'min').
8094+
8095+
>>> df.shift(periods=75, freq='min')
8096+
names
8097+
2018-03-05 12:30:00 Joaquim
8098+
2018-03-10 12:30:00 Maria
8099+
2018-03-15 12:30:00 Emanuel
8100+
2018-03-20 12:30:00 Jussara
8101+
2018-03-25 12:30:00 Geraldo
80358102
""")
80368103

80378104
@Appender(_shared_docs['shift'] % _shared_doc_kwargs)

0 commit comments

Comments
 (0)