python - Pad dates within groups with pandas -
i'm trying pad dates within group ('type') , backfill new rows. i've seen examples using reindex, i'm not sure how pad these dates while maintaining groups.
in:
type date value 2016-01-01 1 2016-01-04 3 b 2016-01-10 4 b 2016-01-13 7
desired out:
type date value 2016-01-01 1 2016-01-02 3 2016-01-03 3 2016-01-04 3 b 2016-01-10 4 b 2016-01-11 7 b 2016-01-12 7 b 2016-01-13 7
df.set_index('date').groupby('type', as_index=false).resample('d').bfill().reset_index().drop('level_0', axis=1) out: date type value 0 2016-01-01 1 1 2016-01-02 3 2 2016-01-03 3 3 2016-01-04 3 4 2016-01-10 b 4 5 2016-01-11 b 7 6 2016-01-12 b 7 7 2016-01-13 b 7
Comments
Post a Comment