2

I have a column of dates in this undesirable string format of 'Sun May 1 00:00:10 2016', representing 'Weekday, Mon, Day, hh:mm:ss, Year'.

How can I can format this using pandas.to_datetime or numpy?

Appreciate the help!

import pandas as pd

date = 'Sun May  1 00:00:10 2016'
df = pd.DataFrame([date], columns=['date'])
1
  • 2
    df = pd.DataFrame([pd.to_datetime(date)], columns=['date']) Commented Mar 25, 2017 at 3:40

1 Answer 1

1

In general, for this type of tasks you should use strptime method from datetime module:

from datetime import datetime as dt
s = 'Sun May 1 00:00:10 2016'
f = '%a %B %d %H:%M:%S %Y'
dt.strptime(s, f)
datetime.datetime(2016, 5, 1, 0, 0, 10)

As far as pandas is concerned:

df = pd.DataFrame({"date":
                  ['Sun May 1 00:00:10 2016','Sun May 2 00:00:10 2016']})
df
date
0   Sun May 1 00:00:10 2016
1   Sun May 2 00:00:10 2016


df.date.apply(lambda s: dt.strptime(s,f))
0   2016-05-01 00:00:10
1   2016-05-02 00:00:10
Name: date, dtype: datetime64[ns]

EDIT

To make it more complete, the way suggested by user1753919 in comment to your question is also "just works" in this case:

pd.to_datetime(df.date)
0   2016-05-01 00:00:10
1   2016-05-02 00:00:10
Name: date, dtype: datetime64[ns]

However, the results of timing might be of interest:

%timeit df.date.apply(lambda s: dt.strptime(s,f))
%timeit pd.to_datetime(df.date)
1000 loops, best of 3: 369 µs per loop
1000 loops, best of 3: 771 µs per loop
Sign up to request clarification or add additional context in comments.

1 Comment

your timing results are completely misleading; try with a larger number

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.