pandas
data manipulation
dataframe
missing data
Python

Add missing dates to a pandas DataFrame

Codemia Knowledge Hub ·

To add missing dates to a pandas DataFrame, build the complete date range you expect and reindex the data onto it. Missing dates then appear as explicit rows, and you can decide whether to leave them empty, fill them, or compute them from neighboring values.

Reindex against a complete date range

Convert the date column to datetime, make it the index, build the full range and call reindex. Do the datetime conversion first: filling dates on plain strings does not work.

python
1import pandas as pd
2
3sales = pd.DataFrame(
4    {
5        "date": ["2024-01-01", "2024-01-03", "2024-01-06"],
6        "orders": [10, 15, 7],
7    }
8)
9
10sales["date"] = pd.to_datetime(sales["date"])
11sales = sales.set_index("date")
12
13full_index = pd.date_range(sales.index.min(), sales.index.max(), freq="D")
14completed = sales.reindex(full_index)
15print(completed)

The missing dates now exist as rows with NaN values.

Fill values only once you know what a gap means

Adding dates and filling values are separate decisions. A missing date can mean zero activity, or it can mean nobody collected data that day.

python
completed["orders"] = completed["orders"].fillna(0)
print(completed)

Filling with zero suits something like daily order counts, where a missing day really means no orders. It is wrong when the missing value means unknown.

Use asfreq when the index is already a DatetimeIndex

If the DataFrame already has a datetime index, asfreq does the same in one call:

python
1import pandas as pd
2
3series = pd.DataFrame(
4    {"orders": [10, 15, 7]},
5    index=pd.to_datetime(["2024-01-01", "2024-01-03", "2024-01-06"]),
6)
7
8completed = series.asfreq("D")
9print(completed)

This fits time-series pipelines where the index is already set up correctly.

Add missing dates per group

Many datasets hold several independent series, such as one row per store per date. Fill gaps within each group rather than across the whole table, so each entity gets its own date range:

python
1import pandas as pd
2
3sales = pd.DataFrame(
4    {
5        "store": ["A", "A", "B", "B"],
6        "date": ["2024-01-01", "2024-01-03", "2024-01-01", "2024-01-02"],
7        "orders": [10, 15, 5, 8],
8    }
9)
10
11sales["date"] = pd.to_datetime(sales["date"])
12
13frames = []
14for store, group in sales.groupby("store"):
15    group = group.set_index("date").sort_index()
16    idx = pd.date_range(group.index.min(), group.index.max(), freq="D")
17    filled = group.reindex(idx)
18    filled["store"] = store
19    frames.append(filled.reset_index(names="date"))
20
21result = pd.concat(frames, ignore_index=True)
22print(result)

Each store's timeline stays independent.

Sort first and pick the right frequency

Sort the index before reindexing or resampling, and confirm the cadence you expect. Daily, hourly, business-day and monthly data each need a different frequency string. Filling calendar days into business-day data creates rows you never wanted.

Keep the date as a column

After reindex, the generated dates live in the index. If downstream code expects a normal column, reset the index:

python
completed = completed.reset_index(names="date")
print(completed.head())

This avoids confusion when joining the result back to tables that are not time series.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design