Here’s a one-row dataframe:
import pandas as pd
import datetime
df = pd.DataFrame(columns = ['time', 'score'])
df.at[0, 'time'] = '2022-06-11 07:34:54.168327+00:00'
df.at[0, 'score'] = 2793.7
df['time'] = pd.to_datetime(df['time'])
I want to check if the last row of this dataframe is between two times, say 4pm and 9pm. How can I do it?
Googling, it seems the between_time method might be usable, but the command seems to select only rows between these two times, so it doesn’t look directly applicable to this problem. The naive attempt df.iloc[-1].between_time('16:00', '21:00')
returns a Index must be DatetimeIndex
error, as well.
I imagine one can use between_time
to select the relevant rows and then check if the last row is one of those rows, but that seems highly inefficient.