hamslice
hamslice

Reputation: 149

Pandas dataframe datetime timestamp from string

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?

Upvotes: 0

Views: 3022

Answers (1)

François B.
François B.

Reputation: 1174

You can check this:

import pandas as pd

df['Date/Time Timestamp'] = pd.to_datetime(df['Date/Time String'])

Upvotes: 1

Related Questions