poniedziałek, 30 stycznia 2023

Pandas write integer dataframe column as a timestamp into the database

To display DataFrame's datatypes use dtypes property.
> print(df.dtypes)

last_login    float64
created_at      int64
In order to cast the `float64` or `int64` column into the `datetime64` one the to_datetime method has to be used. Note that specifying the time unit may be needed to make the conversion correct. Assuming our values contain a UNIX timestamps in seconds the `unit="s"` extra parameter will be required.
df['last_login'] = pd.to_datetime(df['last_login'], unit="s")
df['created_at'] = pd.to_datetime(df['created_at'], unit="s")
Finnaly we receive the following types:
> print(df.dtypes)

last_login    datetime64[ns]
created_at    datetime64[ns]
Now the timestamps in the database will be valid instead of '0000-00-00 00:00:00' we spotted without valid conversion.