Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Series' object has no attribute 'to_datetime'

I am trying to convert one column str type to datetime type. But when I write the code:

df.timeStamp = df.timeStamp.to_datetime

it just tell me

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

But when I try

pd.to_datetime(df.timeStamp)

it works.

I am new to python and hope someone could explain why it happens.

I appreciate your time!

like image 986
wen Avatar asked Jan 23 '19 00:01

wen


2 Answers

I am kind of late, but still useful for future readers.

Below code converts a column of type object in a pandas df to type timestamp

df.timeStamp = pd.to_datetime(df.timeStamp)
like image 156
Espanta Avatar answered Nov 01 '22 13:11

Espanta


Because to_datetime is only a valid attribute to pandas module, that's all.

So that's why:

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

(see highlighted part)

So of course, to_datetime can't be used that way.

like image 23
U12-Forward Avatar answered Nov 01 '22 14:11

U12-Forward