【问题标题】:Working with Pandas DataFrame: I want to add missing year in timestamp column which includes month/day and time使用 Pandas DataFrame:我想在时间戳列中添加缺少的年份,其中包括月/日和时间
【发布时间】:2022-10-16 03:53:23
【问题描述】:

我有时间戳,但缺少年份。目前该列是对象/系列,我想使用“pd.to_datetime”将其转换为日期时间格式,但由于缺少年份,我收到一条错误消息。如何将年份“2020”插入如下所示的时间戳:

DateTime
1    01/01  00:02:00
2    01/01  00:04:00
...
262799   12/31  23:58:00
262800   12/31  24:00:00

我想要这个输出,但我不知道如何得到它:

DateTime
1    2020/01/01  00:02:00
2    2020/01/01  00:04:00
....
262800   2020/12/31  24:00:00

【问题讨论】:

    标签: python pandas datetime timestamp


    【解决方案1】:

    使用字符串连接:

    df['DateTime'] = '2020/' + df['DateTime']
    

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 2021-08-31
      • 2021-03-14
      • 2021-08-19
      • 1970-01-01
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      相关资源
      最近更新 更多