Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an index label as a string in Pandas

I have a Pandas dataframe, the index/labels are date. I just want to get out the starting date (ie the first entry) and the ending date (ie the last entry). What is the best way to do that?

Any help would be much appreciated.

like image 613
user3628698 Avatar asked Jan 17 '26 10:01

user3628698


1 Answers

You could use the index's format method. For example,

In [44]: df = pd.DataFrame({'foo':1}, index=pd.date_range('2000-1-1', periods=5, freq='D'))

In [45]: df
Out[45]: 
            foo
2000-01-01    1
2000-01-02    1
2000-01-03    1
2000-01-04    1
2000-01-05    1

[5 rows x 1 columns]

In [46]: df.index[[0,-1]].format()
Out[46]: ['2000-01-01', '2000-01-05']
like image 63
unutbu Avatar answered Jan 21 '26 02:01

unutbu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!