用于将数据系列或数据框中的数据按指定的位置移动。这对于某些时间序列分析特别有用,例如计算数据的变化量或滞后值
1 对Series/DataFrame数据进行移动
1.0 原始数据
import pandas as pd
import numpy as np
df1=pd.DataFrame(np.arange(12).reshape(3,4),
columns=['a','b','c','d'],
index=['A','B','C'])
df1
'''
a b c d
A 0 1 2 3
B 4 5 6 7
C 8 9 10 11
'''
1.1 正数:向下移动,负数:向上移动
df1.shift(1)
'''
a b c d
A NaN NaN NaN NaN
B 0.0 1.0 2.0 3.0
C 4.0 5.0 6.0 7.0
'''
df1.shift(-2)
'''
a b c d
A 8.0 9.0 10.011.0
B NaN NaN NaN NaN
C NaN NaN NaN NaN
'''
1.2 axis 沿着那个轴shift(默认0)
df1.shift(-2,axis=1)
'''
a b c d
A 2 3 NaN NaN
B 6 7 NaN NaN
C 10 11 NaN NaN
'''
1.3 fill_value (NaN 用什么填充)
df1.shift(-2,
axis=1,
fill_value='-1')
'''
a b c d
A 2 3 -1 -1
B 6 7 -1 -1
C 10 11 -1 -1
'''
1.4 对单列进行shift
df1['a'].shift(-2,
fill_value='-1')
'''
A 8
B -1
C -1
Name: a, dtype: object
'''
2 DateTime shift
import pandas as pd
df=pd.date_range(start='2023-09-13 10:00:00',
end='2023-09-13 18:00:00',
freq='1h')
df
'''
DatetimeIndex(['2023-09-13 10:00:00', '2023-09-13 11:00:00',
'2023-09-13 12:00:00', '2023-09-13 13:00:00',
'2023-09-13 14:00:00', '2023-09-13 15:00:00',
'2023-09-13 16:00:00', '2023-09-13 17:00:00',
'2023-09-13 18:00:00'],
dtype='datetime64[ns]', freq='H')
'''
默认按照dataframe的frequency 进行shift
df.shift(2,)
'''
DatetimeIndex(['2023-09-13 12:00:00', '2023-09-13 13:00:00',
'2023-09-13 14:00:00', '2023-09-13 15:00:00',
'2023-09-13 16:00:00', '2023-09-13 17:00:00',
'2023-09-13 18:00:00', '2023-09-13 19:00:00',
'2023-09-13 20:00:00'],
dtype='datetime64[ns]', freq='H')
'''
freq 手动指定frequency
df.shift(2,freq='1min')
'''
DatetimeIndex(['2023-09-13 10:02:00', '2023-09-13 11:02:00',
'2023-09-13 12:02:00', '2023-09-13 13:02:00',
'2023-09-13 14:02:00', '2023-09-13 15:02:00',
'2023-09-13 16:02:00', '2023-09-13 17:02:00',
'2023-09-13 18:02:00'],
dtype='datetime64[ns]', freq='H')
'''