0

I have tried to make a datetime object selected from my dataframe (with a time column: (Time_utc) in Unix time), an aware datetime using to_datetime() method in pandas, but I don't seem to know how. I compared my output (out) with the Unix time converter online tool, and I see that what I indeed have is a naive datetime object.

I tried the following steps:

import pandas as pd

df['date_time'] = pd.to_datetime(df.Time_utc, origin='unix', unit='s')

Printed my results as:

print(df.date_time[0])

out: 2021-08-13 13:27:48

`Result from unixtimeconverter online tool:

Fri Aug 13 2021 13:27:48 GMT+0000
Fri Aug 13 2021 15:27:48 GMT+0200 (Central European Summer Time)

How do I make my out to be an aware datetime?

5
  • How is your input formatted? Did you try to set keyword utc=True? Commented Nov 9, 2022 at 17:39
  • Thanks for your quick response. I tried adding utc=True in the to_datetime() method and I get, after printing print(df.date_time[0]): 2021-08-13 13:27:48+00:00 Also, if I check for the tzinfo(), it now shows it's UTC aware time: Commented Nov 10, 2022 at 10:02
  • Also, if I check for the tzinfo(), it now shows it's UTC aware time: print(df.date_time[0].tzinfo): out: UTC. But the offset is still +00:00. How do I get it to +02:00? Commented Nov 10, 2022 at 10:10
  • You can set a timezone. +02:00 could be "Europe/Berlin" for instance. Command would be df['date_time'].dt.tz_convert("Europe/Berlin") Commented Nov 10, 2022 at 11:30
  • 1
    Wow! Works like magic! Thanks so much @FObersteiner Commented Nov 10, 2022 at 13:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.