python 生成时间序列
获取当前时间
import datetime
today = datetime.date.today()
生成指定时间
time_str = “2022-01-01 12:00:00”
dt = datetime.datetime.strptime(time_str, “%Y-%m-%d %H:%M:%S”)
print(dt)
计算n天后的日期
n = 5 # 可以修改为任意天数
future_date = today + datetime.timedelta(days=n)
print(f"{n}天后的日期:", future_date)
根据时间戳生成时间序列
start_time = datetime.datetime.combine(today, datetime.time())
end_time = datetime.datetime.combine(future_date, datetime.time(23, 59))
time_sequence = []
current_time = start_time
while current_time <= end_time:
time_sequence.append(current_time)
current_time += datetime.timedelta(minutes=1)
print(time_sequence[1])
根据字符生成时间序列
import pandas as pd
year = 2020
time = pd.date_range(start=str(year)+‘-01-01 00:00’, end=str(year)+‘-12-31 23:00’, freq=‘H’)
print(time[1])