Pandas时间序列操作的几种方法

1、查找特定日期的某一天的名称

1
2
3
4
import pandas as pd

day = pd.Timestamp('2021/1/5')
print(day.day_name())

2、执行算数计算

1
2
3
4
5
6
7
8
import pandas as pd 

day = pd.Timestamp('2021/1/5')
day1 = day + pd.Timedelta("3 day") # 后三天
print(day1.day_name())

day2 = day1 + pd.offsets.BDay() # 下一个工作日
print(day2.day_name())

3、使用时区信息来操作转换日期时间

1
2
3
4
5
6
7
import pandas as pd 

day = pd.Timestamp('2021/1/5')
dat_ran = day.tz_localize("UTC")
print(dat_ran)

print(dat_ran.tz_convert("US/Pacific")) # 转换为美国时区

4、使用时间序列

1
2
3
4
import pandas as pd 

dat_ran = pd.date_range(start = '1/1/2021', end = '1/5/2021', freq = 'Min')
print(dat_ran[110])

5、操作日期序列

1
2
3
4
5
6
7
8
import pandas as pd 
import numpy as np

dat_ran = pd.date_range(start ='1/1/2019', end ='1/08/2019',freq ='Min')
df = pd.DataFrame(dat_ran, columns =['date'])

df['data'] = np.random.randint(0, 100, size =(len(dat_ran)))
print(df.head(5))