DETECTING MISSING DATES IN PANDAS
Listing B.14 shows the contents of pandas_missing_dates.py that shows how to detect missing date values in a CSV file.
Listing B.14: pandas_missing_dates.py
import pandas as pd
# A data frame from a dictionary of lists
data = {'Date': ['2021-01-18', '2021-01-20', '2021-01-
21', '2021-01-24'],
'Name': ['Joe', 'John', 'Jane', 'Jim']}
df = pd.Data frame(data)
# Setting the Date values as index:
df = df.set_index('Date')
# to_datetime() converts string format to a DateTime
object:
df.index = pd.to_datetime(df.index)
start_d="2021-01-18"
end_d="2021-01-25"
# display dates that are not in the sequence:
print("MISSING DATES BETWEEN",start_d,"and",end_d,":")
dates = pd.date_range(start=start_d, end=end_d).
difference(df.index)
for date in dates:
print("date:",date)
print()
Listing...