How to apply .timestamp attribute to a column? [duplicate]

import pandas as pd

df=pd.DataFrame(data=[[pd.Timestamp.now()],[pd.Timestamp.now()],[pd.Timestamp.now()],[pd.Timestamp.now()]],columns=['Date'])
    
df
                        Date
0 2022-01-19 12:15:40.801133
1 2022-01-19 12:15:40.801192
2 2022-01-19 12:15:40.801202
3 2022-01-19 12:15:40.801210

df.Date[0]
Timestamp('2022-01-19 12:15:40.801133')

df.Date[0].timestamp()
1642594540.801133

How can apply the .timestamp attribute to the entire column of the dataframe ? Thank you.


Solution 1:

You can use apply():

df["Date"].apply(lambda x: x.timestamp())

Output:

0    1.642594e+09
1    1.642594e+09
2    1.642594e+09
3    1.642594e+09
Name: Date, dtype: float64

Or if you want to change the existing column:

df["Date"] = df["Date"].apply(lambda x: x.timestamp())

df

    Date
0   1.642594e+09
1   1.642594e+09
2   1.642594e+09
3   1.642594e+09