0

I have a Pandas data frame that looks like this:

Date Total Absences
17 2022-01-03 7
18 2022-01-04 16
19 2022-01-05 18
20 2022-01-07 18

and so on.....

As this data only has information from weekdays and I would like to do a time series analysis which requires regularity in the time intervals, I am looking to Pad the missing values with a 0.

However, when I run the below code, it results in an empty data frame:

    #As the data doesn't have regular intervals, for example, weekends and holidays, we will need to pad those values. 

all_dates = pd.date_range(2022-1-1, 2022-5-31, freq = "D")
data.index = pd.DatetimeIndex(data.index)
data = data.reindex(all_dates, fill_value=0)

print(data)

This is the output:

enter image description here

The desired Output is this:

Date Total Absences
17 2022-01-01 0
18 2022-01-02 0
19 2022-01-03 7
20 2022-01-04 16
21 2022-01-05 18
22 2022-01-06 0
23 2022-01-07 18

Please let me know where I am going wrong. Thanks.

  • Can you please add the dataframe into the question as a table and not an image? And also add your desired output. – Zero May 19 '22 at 05:57
  • It is mistake - first date is `2022-1-1`, maybe need `pd.date_range('1984-01-01', '2022-05-31', freq = "D")` – jezrael May 19 '22 at 05:58
  • @Zero: Added the dataframe as a table and the desired output. – Kaustubh Mulay May 19 '22 at 06:09
  • @jezrael: Thanks for your reply. The 1984 values were removed because they didn't make sense. The data is supposed to start from Jan 1, 2022. – Kaustubh Mulay May 19 '22 at 06:10
  • In sample data is not `DatetimeIndex`, so need `data = data .set_index('Data')` and then `data.index = pd.DatetimeIndex(data.index)` – jezrael May 19 '22 at 06:11

0 Answers0