0

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?

2
  • 1
    Does this answer your question? pandas datetime to unix timestamp seconds Commented May 16, 2022 at 17:41
  • @FObersteiner Thank you for your comment. It includes similar solutions to the ones I got here. Commented May 16, 2022 at 20:07

2 Answers 2

1

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

Comments

1

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

3 Comments

Thank you for your answer. But I need the timestamp value i.e. 1652706735, not the date object. How can I achieve that?
@edn check the update
Thank you for the update! Can I somehow use the timetuple() function on the column? I get the "'Series' object has no attribute 'timetuple'" error when I try it.

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.