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
  • 704,970
  • 168
  • 1,303
  • 1,425
Cesar
  • 555
  • 3
  • 13

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
  • 729,927
  • 78
  • 1,141
  • 1,090