【问题标题】:Python: Create a Time series data in required formatPython:以所需格式创建时间序列数据
【发布时间】:2021-08-19 11:43:05
【问题描述】:

任何人都可以帮助以以下格式在 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

'''

【问题讨论】:

    标签: python pandas datetime


    【解决方案1】:

    我认为这段代码可以正常工作

    from datetime import date, datetime, timedelta
    
    start = datetime(year=2020, month=4, day=1)
    end = datetime(year=2021, month=4, day=1)
    
    interval = timedelta(minutes=10)
    
    while(start<end):
        print(start.strftime('%d/%m/%Y %H:%M:%S'))
        start += interval
    

    【讨论】:

      【解决方案2】:

      我会为此使用 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 语法,了解所需的时间格式

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-09
        • 1970-01-01
        • 1970-01-01
        • 2020-01-15
        相关资源
        最近更新 更多