【问题标题】:python pandas datetime.time - datetime.timepython pandas datetime.time - datetime.time
【发布时间】:2014-02-28 11:30:44
【问题描述】:

我有一个数据框,其中包含两列 datetime.time 项。像

   col1                 col2
02:10:00.008209    02:08:38.053145
02:10:00.567054    02:08:38.053145
02:10:00.609842    02:08:38.053145
02:10:00.728153    02:08:38.053145
02:10:02.394408    02:08:38.053145

如何生成 col3,它是 col1 和 col2 之间的区别? (最好以微秒为单位)?

我四处寻找,但在这里找不到解决方案。有人知道吗?

谢谢!

【问题讨论】:

    标签: python datetime pandas


    【解决方案1】:

    不要使用datetime.time,使用timedelta

    import pandas as pd
    import io
    data = """col1                 col2
    02:10:00.008209    02:08:38.053145
    02:10:00.567054    02:08:38.053145
    02:10:00.609842    02:08:38.053145
    02:10:00.728153    02:08:38.053145
    02:10:02.394408    02:08:38.053145"""
    df = pd.read_table(io.BytesIO(data), delim_whitespace=True)
    df2 = df.apply(pd.to_timedelta)
    diff = df2.col1 - df2.col2
    
    diff.astype("i8")/1e9
    

    输出以秒为单位:

    0    81.955064
    1    82.513909
    2    82.556697
    3    82.675008
    4    84.341263
    dtype: float64
    

    将时间数据帧转换为时间增量数据帧:

    df.applymap(time.isoformat).apply(pd.to_timedelta)
    

    【讨论】:

      【解决方案2】:

      您确定要使用 datetime.time 对象的 DataFrame 吗?在这些人身上几乎没有可以方便地执行的操作,尤其是在包装在 DataFrame 中时。

      最好让每列存储一个表示总微秒数的 int。

      您可以将df 转换为存储微秒的DataFrame,如下所示:

      In [71]: df2 = df.applymap(lambda x: ((x.hour*60+x.minute)*60+x.second)*10**6+x.microsecond)
      
      In [72]: df2
      Out[72]: 
               col1        col2
      0  7800008209  7718053145
      1  7800567054  7718053145
      

      从那里,很容易得到你想要的结果:

      In [73]: df2['col1']-df2['col2']
      Out[73]: 
      0    81955064
      1    82513909
      dtype: int64
      

      【讨论】:

        【解决方案3】:

        pandasdatetime 对象转换为np.datetime64 对象,它们的区别是np.timedelta64 对象。

        考虑一下

        In [30]: df
        Out[30]: 
                               0                          1
        0 2014-02-28 13:30:19.926778 2014-02-28 13:30:47.178474
        1 2014-02-28 13:30:29.814575 2014-02-28 13:30:51.183349
        

        我可以考虑按列的差异

         df[0] - df[1]
        
        
         Out[31]: 
         0   -00:00:27.251696
         1   -00:00:21.368774
         dtype: timedelta64[ns]
        

        因此我可以应用timedelta64 转换。微秒级

        (df[0] - df[1]).apply(lambda x : x.astype('timedelta64[us]')) #no actual difference when displayed
        

        或微秒为整数

        (df[0] - df[1]).apply(lambda x : x.astype('timedelta64[us]').astype('int'))
        
         0   -27251696000
         1   -21368774000
         dtype: int64
        

        编辑: 正如@Jeff 所建议的,最后的表达式可以缩短为

        (df[0] - df[1]).astype('timedelta64[us]')
        

        (df[0] - df[1]).astype('timedelta64[us]').astype('int')
        

        对于熊猫 >= .13。

        【讨论】:

        • 在 pandas >= 0.13,你可以做df[0]-df[1].astype('timedelta[us]')
        猜你喜欢
        • 2019-06-07
        • 1970-01-01
        • 2019-09-07
        • 2016-01-15
        • 1970-01-01
        • 1970-01-01
        • 2018-07-17
        相关资源
        最近更新 更多