【问题标题】:Pandas DateTime get duration of filePandas DateTime 获取文件的持续时间
【发布时间】:2018-01-23 16:39:30
【问题描述】:

我的数据文件包含大约 100 万行时间序列数据。它已使用df = pd.read_csv(...) 读入 Python。 我正在寻找一种方法来获取文件的持续时间(以秒为单位),我正在寻找的输出只是一个给出持续时间的数字

下面显示了显示数据结构的前 5 个条目和最后 5 个条目:

df.head(5)

                                 X         Y         Z
 TimeStamp                                            
 2017-05-12 11:03:39.560  0.185310 -0.168226  0.385064
 2017-05-12 11:03:39.570  0.184273 -0.290579  0.497026
 2017-05-12 11:03:39.580  0.188649 -0.456002  0.601236
 2017-05-12 11:03:39.590  0.195188 -0.629775  0.679267
 2017-05-12 11:03:39.600  0.196400 -0.789999  0.729308

df.tail(5)

                                 X         Y         Z
 TimeStamp                                            
 2017-05-12 13:18:59.950 -0.045288 -0.018508  1.010065
 2017-05-12 13:18:59.960 -0.045412 -0.018438  1.009695
 2017-05-12 13:18:59.970 -0.045671 -0.018282  1.009768
 2017-05-12 13:18:59.980 -0.045889 -0.018029  1.010952
 2017-05-12 13:18:59.990 -0.045657 -0.017709  1.013374

【问题讨论】:

    标签: python-3.x pandas time-series python-datetime


    【解决方案1】:

    IIUC,让我们试试,给定 TimeStamp 是一个 DatetimeIndex: 首先让我们让你索引到日期时间:

    df.index = pd.to_datetime(df.index)
    
    
    df.reset_index()['TimeStamp'].diff().sum().total_seconds()
    

    (df.index[-1] - df.index[0]).total_seconds()
    

    【讨论】:

    • 我尝试了这个并且出现了一个错误:TypeError: unsupported operand type(s) for -: 'str' and 'str'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 2019-04-21
    • 1970-01-01
    • 2014-02-08
    • 2010-11-17
    • 2020-06-02
    • 1970-01-01
    相关资源
    最近更新 更多