Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert pandas DataFrame index to Timestamp format

Tags:

python

pandas

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?

like image 641
rsp Avatar asked Feb 22 '17 18:02

rsp


People also ask

How do I change the index of a DataFrame to a datetime?

To convert the index of a DataFrame to DatetimeIndex , use Pandas' to_datetime(~) method.

How do I change an index to a column?

Convert the Index to Column using reset_index() Another way is by using the pandas. Dataframe. reset_index() function to convert the index as a column.

How do I change the index of a data frame?

To change the index values we need to use the set_index method which is available in pandas allows specifying the indexes. where, inplace parameter accepts True or False, which specifies that change in index is permanent or temporary. True indicates that change is Permanent.

How do I convert pandas Dataframe to a datetime index?

Pandas DataFrame.to_timestamp (~) method converts the row index or column index to DatetimeIndex. Defaults to the frequency used in PeriodIndex. Use the starting time of the period. Use the ending time of the period. By default, how="start". Converts index to DatetimeIndex. Converts column labels to DatetimeIndex. By default, axis=0.

How to convert pandas timestamp to ISO format?

Pandas Timestamp.isoformat () function is used to convert the given Timestamp object into the ISO format. Example #1: Use Timestamp.isoformat () function to convert the date in the given Timestamp object to ISO format. Now we will use the Timestamp.isoformat () function to convert the date in the given Timestamp object to ISO format.

What is timestamps in pandas?

Timestamp is the pandas equivalent of python’s Datetime and is interchangeable with it in most cases. It’s the type used for the entries that make up a DatetimeIndex, and other timeseries oriented data structures in pandas. Value to be converted to Timestamp.

What is the pandas equivalent of Python's datetime?

Timestamp is the pandas equivalent of python’s Datetime and is interchangeable with it in most cases. It’s the type used for the entries that make up a DatetimeIndex, and other timeseries oriented data structures in pandas. Value to be converted to Timestamp. Offset which Timestamp will have. Time zone for time which Timestamp will have.


1 Answers

Try this

df.index = pd.to_datetime(df.index)
like image 95
Vaishali Avatar answered Sep 16 '22 14:09

Vaishali