2

I am trying to add hh:mm:ss column to a column having format as YYYY\MM\DD hh:mm:ss but getting some error. The exact data is below,

col1 = 1:08:10

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

i am trying to get col3 as 2019/02/22 10:04:42 by adding col1 to col2.

i am using below code,

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

I am getting error as,

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: /

Please help.

cs95
  • 379,657
  • 97
  • 704
  • 746

1 Answers1

1

Use to_datetime for convert datetimes and if possible some non matched values add errors='coerce' for both methods:

ol3 = pd.to_datetime(df.col2, format="%Y/%m/%d %H:%M:%S", errors='coerce') + 
      pd.to_timedelta(df.col1, errors='coerce')
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • can you have a look into this as well? https://stackoverflow.com/questions/62690058/how-to-remove-hovertext-from-the-outer-most-block-of-a-tree-map-in-plotly-expres – mayank pareek Jul 02 '20 at 10:09
  • @mayankpareek - Never working with this module (plotly.express), so cannot help, sorry :( – jezrael Jul 02 '20 at 10:12