0

I am trying to convert a column in a pandas dataframe from a string to a timestamp.

Due to a slightly annoying constraint (I am limited by my employers software & IT policy) I am running an older version of Pandas (0.14.1). This version does include the "pd.Timestamp".

Essentially, I want to pass a dataframe column formatted as a string to "pd.Timestamp" to create a column of Timestamps. Here is an example dataframe

      'Date/Time String'  'timestamp'
0  2017-01-01 01:02:03        NaN
1  2017-01-02 04:05:06        NaN
2  2017-01-03 07:08:09        NaN

My DataFrame is very big, so iterating through it is really inefficient. But this is what I came up with:

for i in range (len(df['Date/Time String'])):
    df['timestamp'].iloc[i] = pd.Timestamp(df['Date/Time String'].iloc[i])

What would be the sensible way to make this operation much faster?

2
  • 3
    Check df['Date/Time String']=pd.to_datetime(df['Date/Time String']) Commented Nov 28, 2018 at 3:56
  • @W-B - that worked! Commented Nov 28, 2018 at 8:43

1 Answer 1

1

You can check this:

import pandas as pd

df['Date/Time Timestamp'] = pd.to_datetime(df['Date/Time String'])
Sign up to request clarification or add additional context in comments.

Comments

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.