emax
emax

Reputation: 7245

Python: how to convert timestamp to date in a pandas column?

I have a dataframe like the following

print (df)
            T
0  1573119509
1  1573119512
2  1573119517
3  1573119520

I would like to convert the column T to a date. This is what I am doing:

df['Td'] = pd.to_datetime(df['T']).apply(lambda x: x.date())

but it returns:

print (df)
            T           Td
0  1573119509   1970-01-01
1  1573119512   1970-01-01
2  1573119517   1970-01-01
3  1573119520   1970-01-01

Upvotes: 1

Views: 791

Answers (1)

jezrael
jezrael

Reputation: 862441

Use parameter unit='s' in to_datetime and then Series.dt.date:

df['Td'] = pd.to_datetime(df['T'], unit='s').dt.date
print (df)
            T          Td
0  1573119509  2019-11-07
1  1573119512  2019-11-07
2  1573119517  2019-11-07
3  1573119520  2019-11-07

Upvotes: 4

Related Questions