Find missing calendar dates in a daily pandas time series

Quick Overview

Write a pandas function that returns the calendar dates missing from a daily series between its first and last date as a DatetimeIndex. It tests building an expected date grid, set difference on datetime indexes, and getting the empty case and the freq, name and dtype metadata right.

Find missing calendar dates in a daily pandas time series

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `missing_periods(df)`, where `df` has a `date` column (`datetime64[ns]`, unique, no time component, daily with gaps). Return the calendar dates absent between the series' min and max date (inclusive): build `pd.date_range(df['date'].min(), df['date'].max(), freq='D')` and subtract the dates present. Requirements: - Return a `pd.DatetimeIndex`, sorted ascending, with no duplicates. - `freq` is `None` and `name` is `None`. - When there are no gaps, return an empty index (length `0`) with dtype `datetime64[ns]`. Example: ```python df = pd.DataFrame({'date': pd.to_datetime(['2024-01-01', '2024-01-02', '2024-01-05'])}) missing_periods(df) # DatetimeIndex(['2024-01-03', '2024-01-04'], dtype='datetime64[ns]', freq=None) ``` ```hint Check the metadata After the set operation, look at the result's `freq`, `name` and dtype, and at what `min()` returns when `df` has no rows. ``` ### What a Strong Answer Covers - Building the expected daily grid from the first to the last date, both ends included - Removing the present dates with a set difference and confirming the result is sorted and unique - Returning exactly the required metadata (`freq=None`, `name=None`, nanosecond dtype), including in the no-gap and empty-input cases - Time and space cost in terms of the number of days spanned ### Follow-up Questions - How would you extend this to non-daily frequencies (for example business days or hourly)? - How would you also report the length of each contiguous gap run? - How would you find missing dates separately for each store in a table with a `store_id` column?

Overview: Write a pandas function that returns the calendar dates missing from a daily series between its first and last date as a DatetimeIndex. It tests building an expected date grid, set difference on datetime indexes, and getting the empty case and the freq, name and dtype metadata right.

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

Write missing_periods(df), where df has a date column (datetime64[ns], unique, no time component, daily with gaps). Return the calendar dates absent between the series' min and max date (inclusive): build pd.date_range(df['date'].min(), df['date'].max(), freq='D') and subtract the dates present.

Requirements:

  • Return a pd.DatetimeIndex , sorted ascending, with no duplicates.
  • freq is None and name is None .
  • When there are no gaps, return an empty index (length 0 ) with dtype datetime64[ns] .

Example:

df = pd.DataFrame({'date': pd.to_datetime(['2024-01-01', '2024-01-02', '2024-01-05'])})
missing_periods(df)
# DatetimeIndex(['2024-01-03', '2024-01-04'], dtype='datetime64[ns]', freq=None)

What a Strong Answer Covers Guidance

  • Building the expected daily grid from the first to the last date, both ends included
  • Removing the present dates with a set difference and confirming the result is sorted and unique
  • Returning exactly the required metadata ( freq=None , name=None , nanosecond dtype), including in the no-gap and empty-input cases
  • Time and space cost in terms of the number of days spanned

Follow-up Questions Guidance

  • How would you extend this to non-daily frequencies (for example business days or hourly)?
  • How would you also report the length of each contiguous gap run?
  • How would you find missing dates separately for each store in a table with a store_id column?