rsp
rsp

Reputation: 359

Convert pandas DataFrame index to Timestamp format

I created a dataframe. Running some commands on the iPython I get the following results:

In [5]: df.tail()
Out[5]: 
                 open     high      low    close  volume
date                                                        
2017-02-22 15:00  1.05131  1.05137  1.05074  1.05075     543
2017-02-22 15:30  1.05074  1.05165  1.05072  1.05139     506
2017-02-22 16:00  1.05137  1.05193  1.05121  1.05141     488
2017-02-22 16:30  1.05144  1.05205  1.05056  1.05065     747
2017-02-22 17:00  1.05061  1.05167  1.04988  1.04997     753

In [6]: df.index[0]
Out[6]: '2011-11-21 14:30'

In [7]: type(df.index[0])
Out[7]: str

As you see, the index is in str format but I need it to be in Timestamp format. Is it possible?

Upvotes: 18

Views: 20033

Answers (1)

Vaishali
Vaishali

Reputation: 38415

Try this

df.index = pd.to_datetime(df.index)

Upvotes: 22

Related Questions