簡體   English   中英

試圖將小時、分鍾和秒添加到 dataframe python 的日期時間列

[英]trying to add hours, minutes and seconds to the date time column of a dataframe python

我正在嘗試將 hh:mm:ss 列添加到格式為 YYYY\MM\DD hh:mm:ss 但出現一些錯誤的列中。 具體數據如下,

col1 = 1:08:10

col2 = 2019/02/22 08:56:32

我試圖通過將 col1 添加到 col2 來將 col3 設為 2019/02/22 10:04:42。

我正在使用下面的代碼,

    col3 = pd.to_timedelta(col2).dt.strftime("%Y/%m/%d %H:%M:%S")) + pd.to_timedelta(col1)

我收到錯誤消息,

KeyError: '/'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
pandas/_libs/tslibs/timedeltas.pyx in pandas._libs.tslibs.timedeltas.array_to_timedelta64()

pandas/_libs/tslibs/timedeltas.pyx in pandas._libs.tslibs.timedeltas.parse_timedelta_string()

pandas/_libs/tslibs/timedeltas.pyx in pandas._libs.tslibs.timedeltas.timedelta_from_spec()

pandas/_libs/tslibs/timedeltas.pyx in pandas._libs.tslibs.timedeltas.parse_timedelta_unit()

ValueError: invalid unit abbreviation: /

請幫忙。

使用to_datetime轉換日期時間,如果可能,一些不匹配的值會為這兩種方法添加errors='coerce'

ol3 = pd.to_datetime(df.col2, format="%Y/%m/%d %H:%M:%S", errors='coerce') + 
      pd.to_timedelta(df.col1, errors='coerce')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM