Pandas resample with changing fill limit

Viewed 528

I have a protocol of a machine with all production jobs. I additionaly have energy data from this machine. I want to have minute values for a specific time intervall, so I can analyze the load of the machine and the energy consumption.

Machine protocol:

start_time_rounded  product_number  product_description total_quantity  quantity_per_minute start_time  end_time    duration    duration_minutes
2016-01-01 19:35:00 1   product A   35690.0 16222.727   2016-01-01 19:35:08 2016-01-01 19:37:20 0 days 00:02:12 2.2
2016-01-01 19:40:00 2   product B   64418.0 12840.797   2016-01-01 19:40:06 2016-01-01 19:45:07 0 days 00:05:01 5.017

I want to have the following dataframe:

time    product_number  product_description quantity_per_minute
2016-01-01 19:35:00 1   product A   16222.727
2016-01-01 19:36:00 1   product A   16222.727
2016-01-01 19:37:00 1   product A   16222.727
2016-01-01 19:38:00 NaN NaN NaN
2016-01-01 19:39:00 NaN NaN NaN
2016-01-01 19:40:00 2   product B   12840.797
2016-01-01 19:41:00 2   product B   12840.797
2016-01-01 19:42:00 2   product B   12840.797
2016-01-01 19:43:00 2   product B   12840.797
2016-01-01 19:44:00 2   product B   12840.797
2016-01-01 19:45:00 2   product B   12840.797

I managed to have a dataframe with ongoing time values and I have added the right product to the the right time stamp. I am stuck at how I can add the information to the next time stamps.

I tried to use the DataFrame.ffill function, but it also fills the gaps where no product is in the machine.

I tried:

df = df.resample('min').ffill(limit=df['duration_minutes']

But I receive an error, because the limit must be an integer.

The reindex is done with:

df = df.reindex(pd.date_range(start='01.01.2016 00:00:00', end='31.12.2016 23:59:00', freq='min'))

How can I use the .ffill() function with the duration values as the limit? Thanks in advance!

0 Answers
Related