Reindex a pandas DataFrame onto a complete daily date range

Quick Overview

Write a pandas function that parses string dates and puts the frame on a complete daily DatetimeIndex from the first date to the last, with missing days shown as NaN. It tests date parsing, building a daily range, label alignment, and handling gaps and duplicate dates.

Reindex a pandas DataFrame onto a complete daily date range

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `daily_date_range_index(df: pd.DataFrame) -> pd.DataFrame`. The input has a `'date'` column of string dates and a `'value'` column. Return the frame reindexed onto a complete daily `DatetimeIndex` covering every calendar day from the minimum to the maximum date (inclusive). Requirements: - Parse `'date'` to datetimes (for example with `pd.to_datetime`) and set it as the index. - Build the target index with `pd.date_range(..., freq='D')` from the minimum to the maximum date, inclusive. - Days absent from the input appear with `NaN` in `'value'`. For example: ```python df = pd.DataFrame({ 'date': ['2024-01-01', '2024-01-03'], 'value': [10, 30], }) daily_date_range_index(df) # value # 2024-01-01 10.0 # 2024-01-02 NaN # 2024-01-03 30.0 # freq='D' ``` ```hint Watch the labels `reindex` matches rows by index label. Think about what the existing index has to look like (its type, whether labels repeat) for that match to work, and what happens to the `'value'` dtype once new rows appear. ``` ### What a Strong Answer Covers - Converting the string dates to timestamps before building the range, so old and new labels compare equal - Building a daily range that includes both the first and the last date - Aligning with `reindex` so existing values are kept and missing days become `NaN` - Side effects of the reindex: `'value'` turns into a float column, and duplicate dates make `reindex` raise - Edge cases such as unsorted input, an empty frame, or date strings that carry a time of day ### Follow-up Questions - How would you fill the gaps instead of leaving `NaN`? - What if the frame has duplicate dates before reindexing? - How would you build the daily index separately for each group, for example one date range per store?

Overview: Write a pandas function that parses string dates and puts the frame on a complete daily DatetimeIndex from the first date to the last, with missing days shown as NaN. It tests date parsing, building a daily range, label alignment, and handling gaps and duplicate dates.

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

Implement daily_date_range_index(df: pd.DataFrame) -> pd.DataFrame. The input has a 'date' column of string dates and a 'value' column. Return the frame reindexed onto a complete daily DatetimeIndex covering every calendar day from the minimum to the maximum date (inclusive).

Requirements:

  • Parse 'date' to datetimes (for example with pd.to_datetime ) and set it as the index.
  • Build the target index with pd.date_range(..., freq='D') from the minimum to the maximum date, inclusive.
  • Days absent from the input appear with NaN in 'value' .

For example:

df = pd.DataFrame({
    'date': ['2024-01-01', '2024-01-03'],
    'value': [10, 30],
})

daily_date_range_index(df)
#             value
# 2024-01-01   10.0
# 2024-01-02    NaN
# 2024-01-03   30.0
# freq='D'

What a Strong Answer Covers Guidance

  • Converting the string dates to timestamps before building the range, so old and new labels compare equal
  • Building a daily range that includes both the first and the last date
  • Aligning with reindex so existing values are kept and missing days become NaN
  • Side effects of the reindex: 'value' turns into a float column, and duplicate dates make reindex raise
  • Edge cases such as unsorted input, an empty frame, or date strings that carry a time of day

Follow-up Questions Guidance

  • How would you fill the gaps instead of leaving NaN ?
  • What if the frame has duplicate dates before reindexing?
  • How would you build the daily index separately for each group, for example one date range per store?