0

I am confused with datetime64 and trying convert it to a normal time format.

I have a column with timestamp format: 2022.01.02D23:10:12.197164900.

Output expected is: 2022-01-02 23:10:12

I'm trying with:

df['executionTime'] = pd.to_datetime(df['executionTime'], format='%Y-%m-%d %H:%M:%S.%f', errors='coerce')

1 Answers1

1

Try this:

df['executionTime'] = pd.to_datetime(df['executionTime'], format='%Y.%m.%dD%H:%M:%S.%f', errors='coerce')
  • It won't result in the expected result I think. The expected output is something like `2022-01-02 23:10:12` – TheFaultInOurStars Mar 14 '22 at 20:27
  • For me it produces `2022-01-02 23:10:12.197164900`, @Amirhossein. –  Mar 14 '22 at 20:28
  • 1
    It seems you were right about the OP's problem:) Although the OP mentioned that "Output expected is : 2022-01-02 23:10:12". Anyway, +1 for your fine answer:) – TheFaultInOurStars Mar 14 '22 at 20:30
  • 1
    @richardec yeah will do :) – Eigenvector Mar 14 '22 at 20:30
  • Yeah...it included the milli-/micro-seconds, but I assumed that the OP wouldn't mind those... –  Mar 14 '22 at 20:31
  • Yeah I don't need miliseconds, btw you guys know how I can actually + or - some minutes on this time format? – Eigenvector Mar 14 '22 at 20:34
  • @Eigenvector Try using `timedelta` to add and subtract date objects. – TheFaultInOurStars Mar 14 '22 at 20:37
  • Sure @Eigenvector: to add, e.g., 5 hours and 3 minutes, use `df['executionTime'] += pd.Timedelta(hours=5, minutes=3)`. Swap the `+=` to `-=` to subtract instead of add. –  Mar 14 '22 at 20:37
  • @richardec like I want to compare with other table where those timestamp is same format, I need to get the row where timestamp + or - 15 minutes. You can suggest me what is the best way to do? – Eigenvector Mar 14 '22 at 20:40