maxframe.dataframe.Series.shift#

Series.shift(periods=1, freq=None, axis=0, fill_value=<no_default>)#

通过指定的周期数和可选的时间频率 freq 来偏移索引。

当未传入 freq 时,偏移索引但不重新对齐数据。如果传入了 freq`(此时索引必须是日期或日期时间类型,否则将引发 `NotImplementedError),则将根据周期数和 freq 增加索引。

参数:
  • periods (int) -- 要偏移的周期数。可以为正数或负数。

  • freq (DateOffset, tseries.offsets, timedelta, or str, optional) -- 使用来自 tseries 模块或时间规则(例如 'EOM')的偏移量。如果指定了 freq,则索引值会发生偏移但数据不会重新对齐。也就是说,如果你想在偏移时扩展索引并保留原始数据,请使用 freq

  • axis ({0 or 'index', 1 or 'columns', None}, default None) -- 偏移方向。

  • fill_value (object, optional) -- 用于新引入缺失值的标量值。默认值取决于 self 的数据类型。对于数值数据,使用 np.nan。对于日期时间、时间差或周期数据等,使用 NaT。对于扩展数据类型,使用 self.dtype.na_value

返回:

输入对象的副本,已偏移。

返回类型:

DataFrame or Series

参见

Index.shift

偏移 Index 的值。

DatetimeIndex.shift

偏移 DatetimeIndex 的值。

PeriodIndex.shift

偏移 PeriodIndex 的值。

tshift

偏移时间索引,如果可用则使用索引的频率。

示例

>>> import maxframe.dataframe as md
>>> df = md.DataFrame({'Col1': [10, 20, 15, 30, 45],
...                    'Col2': [13, 23, 18, 33, 48],
...                    'Col3': [17, 27, 22, 37, 52]})
>>> df.shift(periods=3).execute()
   Col1  Col2  Col3
0   NaN   NaN   NaN
1   NaN   NaN   NaN
2   NaN   NaN   NaN
3  10.0  13.0  17.0
4  20.0  23.0  27.0
>>> df.shift(periods=1, axis='columns').execute()
   Col1  Col2  Col3
0   NaN  10.0  13.0
1   NaN  20.0  23.0
2   NaN  15.0  18.0
3   NaN  30.0  33.0
4   NaN  45.0  48.0
>>> df.shift(periods=3, fill_value=0).execute()
   Col1  Col2  Col3
0     0     0     0
1     0     0     0
2     0     0     0
3    10    13    17
4    20    23    27