I am trying to go through every row in a DataFrame index and remove all rows that are not between a certain time.
I have been looking for solutions but none of them separate the Date from the Time, and all I want to do is drop the rows that are outside of a Time range.
You can use the
between_timefunction directly:Original answer:
You can use the
indexer_between_timeIndexmethod.For example, to include those times between 9am and 6pm (inclusive):
to do the opposite and exclude those times between 6pm and 9am (exclusive):
Note:
indexer_between_time‘s argumentsinclude_startandinclude_endare by defaultTrue, settinginclude_starttoFalsemeans that datetimes whose time-part is preciselystart_time(the first argument), in this case 6pm, will not be included.Example:
Note: the same syntax (using
ix) works for a DataFrame: