How to use reindex() function to check missing dates In Python Pandas
Here we are typecasting the string type date into datetime type and with help of reindex() we are checking all the dates that are missing in the given data Frame and assign it to True otherwise assign it to False.
Python3
import pandas as pd # A dataframe from a dictionary of lists data = { 'Date' : [ '2021-01-18' , '2021-01-20' , '2021-01-23' , '2021-01-25' ], 'Name' : [ 'Jia' , 'Tanya' , 'Rohan' , 'Sam' ]} df = pd.DataFrame(data) df[ 'Date' ] = pd.to_datetime(df[ 'Date' ]) df.set_index( 'Date' , inplace = True ) df.reindex(pd.date_range( '2021-01-17' , '2021-01-29' ) ).isnull(). all ( 1 ) |
Output:
Check missing dates in Pandas
In this article, we will learn how to check missing dates in Pandas.
A data frame is created from a dictionary of lists using pd.DataFrame() which accepts the data as its parameter. Note that here, the dictionary consists of two lists named Date and Name. Both of them are of the same length and some dates are missing from the given sequence of dates ( From 2021-01-18 to 2021-01-25 ).