1

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

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

I would like display also seconds and milliseconds. This i what I am doing df['Td'] = pd.to_datetime(df['T'], unit='s').dt.date. How can I show seconds and milliseconds for plotting all the different points?
@emax I think you can use df['Td'] = pd.to_datetime(df['T'], unit='s') only, becase converting to dates remove times, also seconds and milliseconds. If need better precision only is necessary not rounding like in my previous answer and use df['Td'] = pd.to_datetime(df['T'], unit='ms') change s to ms

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.