maxframe.dataframe.DataFrame.shift#
- DataFrame.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。
- 返回:
输入对象的副本,已偏移。
- 返回类型:
参见
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