赞
踩
periods:表示shift移动的幅度,正数表示下移,负数表示上移,默认值是1,移动后没有值得索引后面显示缺失
freq:按照freq参数值作为间隔移动时间索引,数据值不发生变化,关于freq可以取得值可以参考date_range创建日期范围freq参数取值表及创建示例_我就是一个小怪兽的博客-CSDN博客
- >>> import pandas as pd
- >>> date_index=pd.date_range('2022-01-01',periods=6)
- >>> time=pd.Series(range(6),index=date_index)
- >>> print(time)
- 2022-01-01 0
- 2022-01-02 1
- 2022-01-03 2
- 2022-01-04 3
- 2022-01-05 4
- 2022-01-06 5
- Freq: D, dtype: int64
- >>> time.shift(periods=2)#向下移动2次
- 2022-01-01 NaN
- 2022-01-02 NaN
- 2022-01-03 0.0
- 2022-01-04 1.0
- 2022-01-05 2.0
- 2022-01-06 3.0
- Freq: D, dtype: float64
- >>> time.shift(periods=-2)#向上移动2次
- 2022-01-01 2.0
- 2022-01-02 3.0
- 2022-01-03 4.0
- 2022-01-04 5.0
- 2022-01-05 NaN
- 2022-01-06 NaN
- Freq: D, dtype: float64
- >>> time.shift(periods=-2,freq='2D')#间隔为两天,向上移动两次,因此时间索引平移了四天
- 2021-12-28 0
- 2021-12-29 1
- 2021-12-30 2
- 2021-12-31 3
- 2022-01-01 4
- 2022-01-02 5
- Freq: D, dtype: int64
- >>> time.shift(periods=-1,freq='2D')
- 2021-12-30 0
- 2021-12-31 1
- 2022-01-01 2
- 2022-01-02 3
- 2022-01-03 4
- 2022-01-04 5
- Freq: D, dtype: int64
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。