For each plant/product, I would like to add a record for each missing date within a date range. The range is based on the min/max dates regardless of Plant/Product. Then, for each Plant/Product, I would like to forward fill the Qty for each new record.
Here's a sample of the my initial pandas dataframe. I'm would like to turn this:
Plant Product Date Qty
Austin A 2019-08-30 500
Austin A 2019-09-01 700
Austin A 2019-09-03 600
Austin B 2019-08-31 2000
Austin B 2019-09-02 2100
Austin B 2019-09-04 2200
Boston A 2019-08-31 50
Boston A 2019-09-02 60
Into this:
Plant Product Date Qty
Austin A 2019-08-30 500
Austin A 2019-08-31 500
Austin A 2019-09-01 700
Austin A 2019-09-02 700
Austin A 2019-09-03 600
Austin A 2019-09-04 600
Austin B 2019-08-31 2000
Austin B 2019-09-01 2000
Austin B 2019-09-02 2100
Austin B 2019-09-03 2100
Austin B 2019-09-04 2200
Boston A 2019-08-31 50
Boston A 2019-09-01 50
Boston A 2019-09-02 60
Boston A 2019-09-03 60
Boston A 2019-09-04 60
There is a solution in a Stack Overflow post Pandas filling missing dates and values within group, which came close, but only seemed to work when 2 fields (in addition to the date and qty/value) are present. When I tried the following:
ICG=ICG.set_index(
['Date', 'Plant', 'Product']).unstack().asfreq('D').stack().sort_index(level=1).reset_index()
I received an error message on the asfreq() function:
TypeError: Cannot convert input [(Timestamp('2019-08-30 00:00:00'), 'Austin')] of type <class 'tuple'> to Timestamp