Filter intraday rows to the 09:30 to 16:00 window with pandas between_time

Quick Overview

Write a pandas function that keeps only the rows of an intraday time series whose time of day falls between 09:30 and 16:00 across several dates. It tests time-of-day filtering, boundary handling, index requirements and time zone awareness.

Filter intraday rows to the 09:30 to 16:00 window with pandas between_time

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `filter_between_time(df: pd.DataFrame) -> pd.DataFrame`. The input has an intraday `DatetimeIndex` spanning multiple hours across one or more days. Return only the rows whose time of day falls between `09:30` and `16:00`. Requirements: - Use `df.between_time('09:30', '16:00')`. - Both endpoints are inclusive (the default `between_time` behavior). - Preserve the original row order, the index values and all columns. Example: ```python idx = pd.to_datetime([ '2026-01-05 09:00', '2026-01-05 09:30', '2026-01-05 12:15', '2026-01-05 16:00', '2026-01-05 16:30', ]) df = pd.DataFrame({'price': [10, 11, 12, 13, 14]}, index=idx) filter_between_time(df) # price # 2026-01-05 09:30:00 11 # 2026-01-05 12:15:00 12 # 2026-01-05 16:00:00 13 ``` ```hint Which part of the timestamp matters The window applies to the clock time of each row, whatever its date. Check what the method does at exactly 09:30 and exactly 16:00 before you add any extra logic. ``` ### Constraints & Assumptions - The index is a `DatetimeIndex`, either naive or timezone-aware, and may cover several calendar days. - The frame can have any number of columns of any dtype. - The index is not guaranteed to be sorted and may contain repeated timestamps. ### What a Strong Answer Covers - Filtering on the time-of-day component so that rows from every date inside the window are kept - How the two boundaries are treated and which parameter controls that - What the method requires of the index and what happens when that requirement is not met - Confirmation that row order, index values and every column come through unchanged - Edge cases such as timestamps a few seconds past 16:00 or an index stored in a different time zone than the trading session ### Follow-up Questions - How would you handle a range that wraps past midnight, such as `22:00` to `02:00`? - How do you exclude one endpoint, for example drop the rows stamped exactly `16:00`? - What must be true of the index for `between_time` to work, and what would you do if the timestamps sat in a regular column instead? - The index is stored in UTC but the session window is defined in New York time. How do you filter correctly?

Overview: Write a pandas function that keeps only the rows of an intraday time series whose time of day falls between 09:30 and 16:00 across several dates. It tests time-of-day filtering, boundary handling, index requirements and time zone awareness.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement filter_between_time(df: pd.DataFrame) -> pd.DataFrame. The input has an intraday DatetimeIndex spanning multiple hours across one or more days. Return only the rows whose time of day falls between 09:30 and 16:00.

Requirements:

  • Use df.between_time('09:30', '16:00') .
  • Both endpoints are inclusive (the default between_time behavior).
  • Preserve the original row order, the index values and all columns.

Example:

idx = pd.to_datetime([
    '2026-01-05 09:00', '2026-01-05 09:30',
    '2026-01-05 12:15', '2026-01-05 16:00',
    '2026-01-05 16:30',
])
df = pd.DataFrame({'price': [10, 11, 12, 13, 14]}, index=idx)

filter_between_time(df)
#                      price
# 2026-01-05 09:30:00     11
# 2026-01-05 12:15:00     12
# 2026-01-05 16:00:00     13

Constraints & Assumptions

  • The index is a DatetimeIndex , either naive or timezone-aware, and may cover several calendar days.
  • The frame can have any number of columns of any dtype.
  • The index is not guaranteed to be sorted and may contain repeated timestamps.

What a Strong Answer Covers Guidance

  • Filtering on the time-of-day component so that rows from every date inside the window are kept
  • How the two boundaries are treated and which parameter controls that
  • What the method requires of the index and what happens when that requirement is not met
  • Confirmation that row order, index values and every column come through unchanged
  • Edge cases such as timestamps a few seconds past 16:00 or an index stored in a different time zone than the trading session

Follow-up Questions Guidance

  • How would you handle a range that wraps past midnight, such as 22:00 to 02:00 ?
  • How do you exclude one endpoint, for example drop the rows stamped exactly 16:00 ?
  • What must be true of the index for between_time to work, and what would you do if the timestamps sat in a regular column instead?
  • The index is stored in UTC but the session window is defined in New York time. How do you filter correctly?