Home > Software design >  How to convert string date column to timestamp in a new column in Python Pandas
How to convert string date column to timestamp in a new column in Python Pandas

Time:05-17

I have the following example dataframe:

d = {'col1': ["2022-05-16T12:31:00Z", "2021-01-11T11:32:00Z"]}
df = pd.DataFrame(data=d)
df

    col1
0   2022-05-16T12:31:00Z
1   2021-01-11T11:32:00Z

I need a second column (say col2) which will have the corresponding timestamp value for each col1 date string value from col1.

How can I do that without using a for loop?

CodePudding user response:

Maybe try this?

import pandas as pd
import numpy as np

d = {'col1': ["2022-05-16T12:31:00Z", "2021-01-11T11:32:00Z"]}
df = pd.DataFrame(data=d)

df['col2'] = pd.to_datetime(df['col1'])
df['col2'] = df.col2.values.astype(np.int64) // 10 ** 9

df

CodePudding user response:

Let us try to_datetime

df['col2'] = pd.to_datetime(df['col1'])
df
Out[614]: 
                   col1                      col2
0  2022-05-16T12:31:00Z 2022-05-16 12:31:00 00:00
1  2021-01-11T11:32:00Z 2021-01-11 11:32:00 00:00

Update

st = pd.to_datetime('1970-01-01T00:00:00Z')
df['unix'] = (pd.to_datetime(df['col1'])- st).dt.total_seconds()
Out[632]: 
0    1.652704e 09
1    1.610365e 09
Name: col1, dtype: float64
  • Related