Reindex a pandas DataFrame onto a business-day date range

Quick Overview

Write a pandas function that reindexes a dated DataFrame onto a Monday to Friday business-day range between its first and last date. It tests bdate_range, label alignment with reindex, how missing weekdays and weekend rows are handled, and dtype changes when NaN appears.

Reindex a pandas DataFrame onto a business-day date range

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `business_day_index(df: pd.DataFrame) -> pd.DataFrame`. The input has a `date` column (`datetime64[ns]`) and a `value` column. Reindex the data onto a business-day `DatetimeIndex` spanning the min to max date. The function must: - Build the index with `pd.bdate_range(start, end)` (default `freq='B'`), where `start` and `end` are the min and max of `date`. - Set `date` as the index and reindex onto that range, preserving the `value` column. - Drop weekend rows in the input. - Show missing weekdays as `NaN`. Example: ```python df = pd.DataFrame({ 'date': pd.to_datetime(['2024-01-01', '2024-01-03', '2024-01-06']), 'value': [10, 30, 60], # 2024-01-06 is a Saturday }) out = business_day_index(df) # index: 2024-01-01, 02, 03, 04, 05 (freq='B') # value: 10.0, NaN, 30.0, NaN, NaN ``` ```hint What alignment does Think about what happens to a row whose label is missing from the target index, and to a target label that has no matching row. Both requirements follow from that behavior. ``` ### Constraints & Assumptions - `date` has no duplicates. - Holidays are out of scope (default Monday to Friday calendar). ### What a Strong Answer Covers - Builds the target range with `pd.bdate_range` from the minimum to the maximum `date` and reindexes a `date`-indexed frame onto it. - Explains how label alignment handles weekend rows and missing weekdays, with no manual date arithmetic. - Explains the dtype of `value` after reindexing. - Names the assumptions the reindex relies on and the inputs that would break it. ### Follow-up Questions - How would you exclude specific holidays as well? - How would you fill the `NaN` gaps instead of leaving them? - What goes wrong if `date` has duplicate values or a time-of-day component, and how would you handle it?

Overview: Write a pandas function that reindexes a dated DataFrame onto a Monday to Friday business-day range between its first and last date. It tests bdate_range, label alignment with reindex, how missing weekdays and weekend rows are handled, and dtype changes when NaN appears.

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

Implement business_day_index(df: pd.DataFrame) -> pd.DataFrame. The input has a date column (datetime64[ns]) and a value column. Reindex the data onto a business-day DatetimeIndex spanning the min to max date.

The function must:

  • Build the index with pd.bdate_range(start, end) (default freq='B' ), where start and end are the min and max of date .
  • Set date as the index and reindex onto that range, preserving the value column.
  • Drop weekend rows in the input.
  • Show missing weekdays as NaN .

Example:

df = pd.DataFrame({
    'date': pd.to_datetime(['2024-01-01', '2024-01-03', '2024-01-06']),
    'value': [10, 30, 60],  # 2024-01-06 is a Saturday
})

out = business_day_index(df)
# index: 2024-01-01, 02, 03, 04, 05 (freq='B')
# value: 10.0, NaN, 30.0, NaN, NaN

Constraints & Assumptions

  • date has no duplicates.
  • Holidays are out of scope (default Monday to Friday calendar).

What a Strong Answer Covers Guidance

  • Builds the target range with pd.bdate_range from the minimum to the maximum date and reindexes a date -indexed frame onto it.
  • Explains how label alignment handles weekend rows and missing weekdays, with no manual date arithmetic.
  • Explains the dtype of value after reindexing.
  • Names the assumptions the reindex relies on and the inputs that would break it.

Follow-up Questions Guidance

  • How would you exclude specific holidays as well?
  • How would you fill the NaN gaps instead of leaving them?
  • What goes wrong if date has duplicate values or a time-of-day component, and how would you handle it?