【问题标题】:Create Pandas DatetimeIndex from origin and offsets从原点和偏移量创建 Pandas DatetimeIndex
【发布时间】:2019-03-18 11:17:52
【问题描述】:

我想将一组秒转换为适合数据帧的 Pandas 索引。

我想我正在寻找dt.total_seconds() 操作的逆操作,就像https://stackoverflow.com/a/36156620/1653571Accessing total_seconds() in pandas data column 的回答一样

给定:

t0="2010-10-01 00:00:00 UTC"
seconds = [0,1,2,3,5,7,11,13]

我想要类似的东西:

ii = pd.DatetimeIndex( ???(t0,seconds))

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    seconds 列表转换为Timedelta 并做一些datetime 算术运算。

    # origin           + offset
    pd.to_datetime(t0) + pd.to_timedelta(seconds, unit='s')
    

    DatetimeIndex(['2010-10-01 00:00:00+00:00', '2010-10-01 00:00:01+00:00',
                   '2010-10-01 00:00:02+00:00', '2010-10-01 00:00:03+00:00',
                   '2010-10-01 00:00:05+00:00', '2010-10-01 00:00:07+00:00',
                   '2010-10-01 00:00:11+00:00', '2010-10-01 00:00:13+00:00'],
                  dtype='datetime64[ns, UTC]', freq=None)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      相关资源
      最近更新 更多