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.
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.
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:
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:
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:
This avoids confusion when joining the result back to tables that are not time series.
Related reading
- Add new columns to pandas dataframe based on other dataframe
- Add single element to array in numpy
- Add trend line to pandas
- Add x and y labels to a pandas plot
- Add variables to tuple
- Adding a matplotlib legend
- Adding a y-axis label to secondary y-axis in matplotlib
- Adding custom jars to pyspark in jupyter notebook
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.