Home > OS >  How can we retrieve the value associated with that DateTimeIndex in a pandas dataframe?
How can we retrieve the value associated with that DateTimeIndex in a pandas dataframe?

Time:07-25

Given a dataframe and a specific index whose type is DateTimeIndex, how can we retrieve the value associated with that index? Consider the the following code where we have the index of a maximum in a certain range:

index = pd.date_range('7-20-2022', '7-21-2022', freq='min')
np.random.seed(0)
df = pd.DataFrame(np.cumsum(np.random.randn(len(index))), index=index)
df_limited = df.loc['7-20-2022'].between_time('14:00', '15:00')
idx  = df_limited.idxmax()

What is the value at idx?

None of max=df_limited.loc[idx], max=df_limited.iloc[idx], or max = df_limited[(df_limited.index == idx)] work.

CodePudding user response:

idx is a Series, you can use its value for indexing:

max = df_limited.loc[idx.iloc[0], 0]

Result of print(max):

-46.80161593790951

CodePudding user response:

You can loc the value of series

df_limited.loc[idx]
Out[470]: 
                               0
2022-07-20 14:06:00 -46.80161594
  • Related