-3

I have this dataframe:

d = {'Region':['And','And'],
'country': ['ES','ES'],
'Date': [01/01/2016,01/01/2017,],
'holiday': [1,1]
df = pd.DataFrame(d)

I would like to add to the dataframe rows for all dates between those two, with a 0 in holiday and the same region and country as the extremes. I would need to do that for several countries, regions and date gaps of different length.

Javier Lopez Tomas
  • 2,072
  • 3
  • 19
  • 41

1 Answers1

1

You have a few problems in your code - you haven't closed the dictionary, and your dates need to be enclosed as strings. Python won't automatically recognise them as datetime objects.

Here is a fixed version of what you have:

d = {'Region':['And','And'],
'country': ['ES','ES'],
'Date': ['01/01/2016','01/01/2017',],
'holiday': [1,1]}
df = pd.DataFrame(d)

The best way to do what you want to do is, after adding date as a datetime object, set the date range (including the in-between values - see here how to do that) as an index in a separate dataframe, which will add the remaining values as NaN. See more here

Josh Friedlander
  • 10,870
  • 5
  • 35
  • 75