have DataFrame Type data data, Among them index List as date , have other value column , Supplement missing date column index The code is as follows :
import pandas as pd import datetime
# establish DataFrame data , include index Column sum value column , among index List as date , But the format is string format data = pd.DataFrame(
data={'index':['2020-10-09','2020-10-10','2020-10-17','2020-10-15'],'value':
range(4)}) # Calculate minimum and maximum dates date_start = data['index'].min() date_end = data['index'
].max() # According to the minimum date and maximum date , Calculate date interval , because date_start and date_end by string type , Therefore, you need to change to date type first delta =
datetime.datetime.strptime(date_end, "%Y-%m-%d")-datetime.datetime.strptime(
date_start, "%Y-%m-%d") for i in range(1,delta.days+1): date = (datetime.
datetime.strptime(date_end, "%Y-%m-%d")-datetime.timedelta(days=i)).strftime(
'%Y-%m-%d') # If the data is missing , Then complement if(date not in data['index'].values): new_date = pd.
DataFrame(data={'index':date,'value':-1},index=[data.shape[0]]) data = data.
append(new_date,ignore_index=True) # Sort by date column data = data.sort_values('index').
reset_index(drop=True)

Technology