1

I have a dataframe with gaps between date.

Dataframe

It`s necessary to groupby the first 2 columns of this dataframe and then fill the gaps.

What I am trying is this:

df.groupby(['cod_interno', 'unidade_lojas', 'data']).apply(lambda x : x.resample('D').ffill()).reset_index(level=0,drop=True)

However I got this error message:

TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Int64Index'

Is there a better approach to solve this ?

I tried this approach Resampling a multi-index DataFrame, but I not so familiar with 'stack()'

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Cesar
  • 575
  • 3
  • 16

1 Answers1

1

I believe you can use DataFrame.set_index with column data with datetimes and then use resample after groupby, lambda function is not necessary:

df.set_index('data').groupby(['cod_interno', 'unidade_lojas']).resample('D').ffill()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252