Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the 'seconds' of Pandas dataframe index?

Given a dataframe with time series that looks like this:

                      Close
2015-02-20 14:00:00  1200.1
2015-02-20 14:10:00  1199.8
2015-02-21 14:00:00  1199.3
2015-02-21 14:10:00  1199.0
2015-02-22 14:00:00  1198.4
2015-02-22 14:10:00  1199.7

How can I get rid of the 'seconds' of the index so it looks like this:

                   Close
2015-02-20 14:00  1200.1
2015-02-20 14:10  1199.8
2015-02-21 14:00  1199.3
2015-02-21 14:10  1199.0
2015-02-22 14:00  1198.4
2015-02-22 14:10  1199.7

Thanks

like image 815
hernanavella Avatar asked Feb 22 '15 23:02

hernanavella


People also ask

How do I get rid of second time in python?

If you just want strings, you could remove the trailing seconds with a regex ':\d\d$' .

How do I get rid of extra index in Pandas?

Drop the index column of Pandas DataFrame We can remove the index column in existing dataframe by using reset_index() function. This function will reset the index and assign the index columns start with 0 to n-1.

How do you remove an index from a series?

Pandas Series: reset_index() function For a Series with a MultiIndex, only remove the specified levels from the index. Removes all levels by default. Just reset the index, without inserting it as a column in the new DataFrame. The name to use for the column containing the original Series values.


1 Answers

you can use map and strftime like this:

df.index =df.index.map(lambda t: t.strftime('%Y-%m-%d %H:%M'))
like image 116
JAB Avatar answered Sep 29 '22 15:09

JAB