【问题标题】:pandas time differences (delta between rows)熊猫时差(行之间的增量)
【发布时间】:2021-08-05 07:04:38
【问题描述】:

我有一个带有时间戳(字符串)的列,如下所示:

2017-10-25T09:57:00.319Z
2017-10-25T09:59:00.319Z
2017-10-27T11:03:00.319Z

Tbh 我不知道 Z 的含义,但我想它并不那么重要。

如何将上述字符串转换为正确的时间戳以计算差异/增量(例如以秒或分钟为单位)

我想要一列列出一个时间戳到另一个时间戳之间的差异。

【问题讨论】:

    标签: python pandas time delta


    【解决方案1】:

    您可以使用pd.to_datetime() 将字符串转换为日期时间格式。然后通过.diff() 获取时差/增量。最后通过.dt.total_seconds()将timedelta转换为秒,如下:

    (假设您的字符串列名为Date):

    df['Date'] = pd.to_datetime(df['Date'])
    df['TimeDelta'] = df['Date'].diff().dt.total_seconds()
    

    结果:

    以秒为单位的时间增量:

    print(df)
    
                                  Date  TimeDelta
    0 2017-10-25 09:57:00.319000+00:00        NaN
    1 2017-10-25 09:59:00.319000+00:00      120.0
    2 2017-10-27 11:03:00.319000+00:00   176640.0
    

    【讨论】:

      猜你喜欢
      • 2014-06-25
      • 2013-02-15
      • 1970-01-01
      • 2021-01-24
      • 2017-06-10
      • 2016-01-20
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多