【问题标题】:Combining Date and Time series columns in Pandas结合 Pandas 中的日期和时间序列列
【发布时间】:2023-02-07 18:40:44
【问题描述】:

enter image description here

我有一个 excel 文件,它在单独的列中包含日期和时间数据。他们的类型是熊猫核心系列。 我需要将这两列合并为一列以便进行进一步分析。

如何以正确的方式组合这些列?

我尝试使用 pandas.to_datetime() 函数将它们组合起来,但出现错误:

In: df['dt'] = pd.to_datetime(df['Date']+df['Time'])
Out: TypeError: unsupported operand type(s) for +: 'Timestamp' and 'datetime.time'

【问题讨论】:

    标签: python pandas dataframe datetime timestamp


    【解决方案1】:

    使用 to_timedelta 将时间转换为字符串并添加到列 Date

    df['dt'] = df['Date']+pd.to_timedelta(df['Time'].astype(str))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-12
      • 1970-01-01
      • 2021-10-22
      • 2016-10-11
      • 2017-05-16
      • 2017-09-15
      • 1970-01-01
      • 2013-08-01
      相关资源
      最近更新 更多