【问题标题】:Calculating date time from start time and elapsed seconds从开始时间和经过的秒数计算日期时间
【发布时间】:2016-05-30 22:05:20
【问题描述】:

我有一个数据框,其中我的索引是经过的秒数系列。

Depth_m | Temperature_degC | Salinity_PSU | OBS S9604_mV | OBS highsens S9604_mV | OBS S9602_mV | OBS S9603_mV | Time elapsed_sec                           

0.00    |        35.687    |    28.9931   |   36.7530    |        0.0082         |    0.0024    |    0.0059    | 0.0120
0.25    |        35.684    |    28.9932   |   36.7531    |        0.0083         |    0.0026    |    0.0060    | 0.0106
0.50    |        35.687    |    28.9931   |   36.7532    |        0.0079         |    0.0021    |    0.0055    | 0.0099
0.75    |        35.687    |    28.9931   |   36.7532    |        0.0305         |    0.0075    |    0.0056    | 0.0101

我想计算创建一个从开始时间和经过的秒数获得的新系列。 我正在使用带有熊猫的 python v 2.7。 你们中有人知道如何获得吗? 谢谢

【问题讨论】:

    标签: python pandas time-series data-analysis


    【解决方案1】:

    这样的?

    start_time = pd.Timestamp('2016-1-1 00:00')
    df = pd.DataFrame({'seconds': [ 1, 2, 3]})
    df['new_time'] = [start_time + dt.timedelta(seconds=s) for s in df.seconds]
    
    >>> df
       seconds            new_time
    0        1 2016-01-01 00:00:01
    1        2 2016-01-01 00:00:02
    2        3 2016-01-01 00:00:03
    

    【讨论】:

      【解决方案2】:

      这应该可以解决问题

      from __future__ import print_function, division
      import pandas as pd
      
      start_time = 14
      data = pd.read_csv('data.txt', sep="|", header=0,   skip_blank_lines=True)
      data['Time'] = pd.Series(data[' Time elapsed_sec'] + start_time, index=data.index)
      print(data)
      

      缺少像Convert Pandas Column to DateTime这样的日期时间转换

      【讨论】:

      • 非常感谢 Lageos,我首先尝试了您的解决方案,然后意识到我首先需要将经过时间的列用作一个系列,而不是作为我的索引。
      • 现在我得到一个 TypeError: unsupported operand type(s) for +: 'float' and 'datetime.datetime'。可能是因为这个系列只有几分之一秒?
      • 可能需要用... pd.Series(float(data[' Time elapsed_sec'] ) + start_time...进行转换。
      • 虽然我已经设法转换为日期时间格式 (%S),但浮点转换并没有起到作用,但是发生了另一个错误,它说“只能在日期时间上进行减法运算,但是运算符 [add] 已通过" ...还有其他想法吗?
      猜你喜欢
      • 2017-01-19
      • 1970-01-01
      • 2014-09-04
      • 2020-07-20
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多