daiyue
daiyue

Reputation: 7448

AttributeError: 'str' object has no attribute 'to_datetime'

I have a code that reads an excel data sheet (a table) into a DataFrame and convert a 'date' column (with values e.g. 20150508) into date time,

df['date'] = df['date'].astype(str)
dates = df['date'].to_datetime() // error occurs

I got a error,

AttributeError: 'str' object has no attribute 'to_datetime'

if I removed the line,

df['date'] = df['date'].astype(str)

the other line can run fine, I am wondering what is the problem.

Upvotes: 2

Views: 13080

Answers (1)

EdChum
EdChum

Reputation: 393883

There is no to_datetime method for Series only for Index objects it's the top-level method you want:

dates = pd.to_datetime(df['date'])

Upvotes: 7

Related Questions