首页 > 解决方案 > Python:以所需格式创建时间序列数据

问题描述

任何人都可以帮助以以下格式在 Python 中生成时间序列数据。日期-月-年小时-分钟-秒。从 2020 年 4 月 1 日至 2021 年 3 月 31 日:01/04/2020 0.00.00 至 31/03/2021 23:50:00

''' 时间序列

01/04/2020 0:00:00
01/04/2020 0:10:00
.......
.......



31/03/2021 23:50:00

'''

标签: pythonpandasdatetime

解决方案


我会为此使用 pandas .date_range

import pandas as pd

start = '2020-04-01 00:00:00'
end = '2021-03-31 23:50:00'
time_series = pd.date_range(start, end, freq='10min')

# formatted time series can be achieved via:
fmt = '%d-%m-%y %H:%M:%S'
ts_formatted = [i.strftime(fmt) for i in time_series]

查看https://strftime.org/fmt中的语法,了解所需的时间格式


推荐阅读