Home > front end >  Convert column of Timestamps to datetime.datetime. Works on 1 row, not on column
Convert column of Timestamps to datetime.datetime. Works on 1 row, not on column

Time:06-26

I've looked at every answer on this site, including this one: convert timestamp to datetime.datetime in pandas.Series and nothing is working. It always returns a Timestamp.

I have a dataframe with a time column which contains class 'pandas._libs.tslibs.timestamps.Timestamp'> values of the format 2022-06-24 15:07:52.

I'm trying to use pandas' pd.df.to_sql function to write my entire dataframe to a MySQL database. I'm running into an error however because the type of the time column in the database is Datetime, so I need to convert the Timestamps to Datetime format in the dataframe.

I've tried df['time'] = pd.to_datetime(df['time']), which returns Timestamps.

The only thing that worked is df['time'][0].to_pydatetime() when applied to a single row. However when I try df['time'] = df['time'].apply(lambda x: x.to_pydatetime()), it doesn't work. The elements are still Timestamps.

I read in another answer somewhere that to_pydatetime won't work on a Series or column, so I also tried to extract the column as a list, then apply to_pydatetime() to its elements (which works, each element is converted to datetime.datetime) and then put that list back into the dataframe. However when I do that, each element is converted again to a Timestamp...

CodePudding user response:

import pandas as pd

df = pd.DataFrame({'time': [1451602801, 1451606401, 1451610001, 1451613601, 1451617201]})
df['datetime'] = pd.to_datetime(df['time'], unit='s')
print(df)

Output

         time            datetime
0  1451602801 2015-12-31 23:00:01
1  1451606401 2016-01-01 00:00:01
2  1451610001 2016-01-01 01:00:01
3  1451613601 2016-01-01 02:00:01
4  1451617201 2016-01-01 03:00:01

CodePudding user response:

import pandas as pd

df = pd.DataFrame({'time': [1451602801, 1451606401, 1451610001, 1451613601, 1451617201]})
df['datetime'] = pd.to_datetime(df['time'], unit='s')

# Wrap the numpy output as a series and force dtype to object:
df['timestamp'] = pd.Series(df.datetime.dt.to_pydatetime(), dtype='O')
print(type(df.datetime[0]))
print(type(df.timestamp[0]))

Output:

<class 'pandas._libs.tslibs.timestamps.Timestamp'>
<class 'datetime.datetime'>
  • Related