Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
resample grouping pr0blem
#1
i have a scenario in which i have a dataframe which contains 3 columns ( date, product_id and sales_amt)

the dates are supposed to span a whole month ( eg nov 2019) but there are some missing days in the dataframe for each product_id.

does anyone have any tips on python code that can loop through the dates for a particular month AND product and add a new row to the dataframe with the missing date, product_id and a sales_amt of zero?

Goal at the end is to have an entry for every day of that month per product_id. i believe resample can handle this but what would the code look like?

thanks for any guidance on this.

Thanks
Reply
#2
excluding dataframe manipulation, you can do something like:
from datetime import date, timedelta
datelist = [date(2019, 10, 1), date(2019, 10, 3), date(2019, 10, 15), date(2019, 10, 29)]
beg_of_month = date(2019, 10, 1)
end_of_month = date(2019, 10, 31)
date_set = set(datelist[0] + timedelta(x) for x in range((end_of_month - beg_of_month).days))
nodates = sorted(date_set - set(datelist))
for dt in nodates:
    print(dt)
which yields:
Output:
2019-10-02 2019-10-04 2019-10-05 2019-10-06 2019-10-07 2019-10-08 2019-10-09 2019-10-10 2019-10-11 2019-10-12 2019-10-13 2019-10-14 2019-10-16 2019-10-17 2019-10-18 2019-10-19 2019-10-20 2019-10-21 2019-10-22 2019-10-23 2019-10-24 2019-10-25 2019-10-26 2019-10-27 2019-10-28 2019-10-30
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Grouping Data based on 30% bracket purnima1 4 1,190 Mar-10-2023, 07:38 PM
Last Post: deanhystad
  python resample by day and get weekstart data JaneTan 0 707 Dec-15-2022, 03:38 AM
Last Post: JaneTan
  Resample from monthly to weekly works, but not vice versa JaneTan 0 576 Dec-14-2022, 12:58 AM
Last Post: JaneTan
  Python Resample: How do I keep NaN as NaN? JaneTan 3 2,205 Dec-08-2022, 10:18 PM
Last Post: deanhystad
  Grouping and sum of a list of objects Otbredbaron 1 3,195 Oct-23-2021, 01:42 PM
Last Post: Gribouillis
  Grouping and summing of dataset jef 0 1,634 Oct-04-2020, 11:03 PM
Last Post: jef
  Grouping algorithm riccardoob 7 2,990 May-19-2020, 01:22 PM
Last Post: deanhystad
  Help Grouping by Intervals on list paul41 1 2,109 Dec-03-2019, 09:43 PM
Last Post: michael1789
  Grouping a list of various time into intervals paul41 1 3,770 Nov-24-2019, 01:47 PM
Last Post: buran
  Splitting lines ang grouping three at once samsonite 5 2,752 Jun-21-2019, 05:19 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020