Python – How to check missing dates in Pandas


To check missing dates, at first, let us set a dictionary of list with date records i.e. Date of Purchase in our example −

# dictionary of lists
d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'],
   'Date_of_purchase': ['2020-10-10', '2020-10-12', '2020-10-17', '2020-10-16', '2020-10-19', '2020-10-22']}

Now, create a dataframe from the above dictionary of lists −

dataFrame = pd.DataFrame(d)

Next, set it as index −

dataFrame = dataFrame.set_index('Date_of_purchase')

Use to_datetime() to convert string to DateTime object −

dataFrame.index = pd.to_datetime(dataFrame.index)

Display remaining dates in a range −

k = pd.date_range( start="2020-10-10", end="2020-10-22").difference(dataFrame.index);

Example

Following is the code −

import pandas as pd

# dictionary of lists
d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'],
   'Date_of_purchase': ['2020-10-10', '2020-10-12', '2020-10-17', '2020-10-16', '2020-10-19', '2020-10-22'] }

# creating dataframe from the above dictionary of lists
dataFrame = pd.DataFrame(d)
print"DataFrame...\n",dataFrame

# Date_of_purchase set as index
dataFrame = dataFrame.set_index('Date_of_purchase')

# using to_datetime() to convert string to DateTime object
dataFrame.index = pd.to_datetime(dataFrame.index)

# remaining dates displayed as output
print("\nDisplaying remaining dates from a range of dates...")
k = pd.date_range(start="2020-10-10", end="2020-10-22").difference(dataFrame.index);

print(k);

Output

This will produce the following output −

DataFrame...
         Car    Date_of_purchase
0       BMW           2020-10-10
1     Lexus           2020-10-12
2      Audi           2020-10-17
3  Mercedes           2020-10-16
4    Jaguar           2020-10-19
5   Bentley           2020-10-22

Displaying remaining dates from a range of dates...
DatetimeIndex(['2020-10-11', '2020-10-13', '2020-10-14', '2020-10-15',
   '2020-10-18', '2020-10-20', '2020-10-21'],
   dtype='datetime64[ns]', freq=None)

Updated on: 09-Sep-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements