pandas.DataFrame.shift#
- DataFrame.shift(periods=1, freq=None, axis=0, fill_value=NoDefault.no_default)[源代码]#
使用可选时间按所需期间数移位索引 freq 。
什么时候 freq 未传递,则在不重新对齐数据的情况下移动索引。如果 freq 传递(在本例中,索引必须是日期或日期时间),否则将引发 NotImplementedError ),则索引将使用句点和 freq 。 freq 当指定为“INFER”时,只要在索引中设置了FREQ或INFERSED_FREQ属性,就可以推断。
- 参数
- periods集成
要换班的期间数。可以是正数,也可以是负数。
- freqDateOffset、tSeries、Offsets、TimeDelta或str,可选
从tSeries模块或时间规则使用的偏移量(例如‘EOM’)。如果 freq 则移位索引值,但不重新对齐数据。也就是说,使用 freq 如果您希望在移动时扩展索引并保留原始数据。如果 freq 被指定为“INFER”,则将从索引的FREQ或INFERSED_FREQ属性中进行推断。如果这两个属性都不存在,则抛出ValueError。
- axis{0或‘index’,1或‘Columns’,无},默认为无
换个方向。
- fill_value对象,可选
用于新引入的缺失值的标量值。默认设置取决于的数据类型 self 。对于数字数据,
np.nan
使用的是。用于日期时间、时间增量或期间数据等。NaT
使用的是。对于扩展数据类型,self.dtype.na_value
是使用的。在 1.1.0 版更改.
- 退货
- DataFrame
输入对象的副本,已移动。
参见
Index.shift
索引的移动值。
DatetimeIndex.shift
DatetimeIndex的移位值。
PeriodIndex.shift
周期索引的移位值。
tshift
移动时间索引,使用索引的频率(如果可用)。
示例
>>> df = pd.DataFrame({"Col1": [10, 20, 15, 30, 45], ... "Col2": [13, 23, 18, 33, 48], ... "Col3": [17, 27, 22, 37, 52]}, ... index=pd.date_range("2020-01-01", "2020-01-05")) >>> df Col1 Col2 Col3 2020-01-01 10 13 17 2020-01-02 20 23 27 2020-01-03 15 18 22 2020-01-04 30 33 37 2020-01-05 45 48 52
>>> df.shift(periods=3) Col1 Col2 Col3 2020-01-01 NaN NaN NaN 2020-01-02 NaN NaN NaN 2020-01-03 NaN NaN NaN 2020-01-04 10.0 13.0 17.0 2020-01-05 20.0 23.0 27.0
>>> df.shift(periods=1, axis="columns") Col1 Col2 Col3 2020-01-01 NaN 10 13 2020-01-02 NaN 20 23 2020-01-03 NaN 15 18 2020-01-04 NaN 30 33 2020-01-05 NaN 45 48
>>> df.shift(periods=3, fill_value=0) Col1 Col2 Col3 2020-01-01 0 0 0 2020-01-02 0 0 0 2020-01-03 0 0 0 2020-01-04 10 13 17 2020-01-05 20 23 27
>>> df.shift(periods=3, freq="D") Col1 Col2 Col3 2020-01-04 10 13 17 2020-01-05 20 23 27 2020-01-06 15 18 22 2020-01-07 30 33 37 2020-01-08 45 48 52
>>> df.shift(periods=3, freq="infer") Col1 Col2 Col3 2020-01-04 10 13 17 2020-01-05 20 23 27 2020-01-06 15 18 22 2020-01-07 30 33 37 2020-01-08 45 48 52