Skip to content
Advertisement

Finding label location in a DataFrame Index

I have a pandas dataframe:

import pandas as pnd
d = pnd.Timestamp('2013-01-01 16:00')
dates = pnd.bdate_range(start=d, end = d+pnd.DateOffset(days=10), normalize = False)

df = pnd.DataFrame(index=dates, columns=['a'])
df['a'] = 6

print(df)
                     a
2013-01-01 16:00:00  6
2013-01-02 16:00:00  6
2013-01-03 16:00:00  6
2013-01-04 16:00:00  6
2013-01-07 16:00:00  6
2013-01-08 16:00:00  6
2013-01-09 16:00:00  6
2013-01-10 16:00:00  6
2013-01-11 16:00:00  6

I am interested in find the label location of one of the labels, say,

ds = pnd.Timestamp('2013-01-02 16:00')

Looking at the index values, I know that is integer location of this label 1. How can get pandas to tell what the integer value of this label is?

Advertisement

Answer

You’re looking for the index method get_loc:

In [11]: df.index.get_loc(ds)
Out[11]: 1
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement