Padd missing dates in Pandas series

proximacentauri

I have a Panas series object created after groupby and resample operations on a dataframe.

I would like to fill missing days between min and max dates with zero quantity for each item_type.

    data = {'date_time': ['2018-01-22 12:40:03', '2018-01-22 13:40:03', '2018-01-23 15:00:05', '2018-01-26 14:30:04'], 
     'quantity': [11, 21, 23, 12], 'item_type': ['543', '543', '842', '543']}
    df = pd.DataFrame(data, columns = ['date_time', 'quantity' , 'item_type']) 
    df.index = df['date_time']
    df.index = pd.to_datetime(df.index)
    min_date = df.index.min()
    max_date = df.index.max()
    grouped = df.groupby('item_type').resample('D')['quantity'].sum()
    print(grouped)

>> Message: item_type   date_time 
543  2018-01-22    32.0
     2018-01-23     NaN
     2018-01-24     NaN
     2018-01-25     NaN
     2018-01-26    12.0
842  2018-01-23    23.0
Name: quantity, dtype: float64

If I don't resampling by day I could get a groupby object that I could iterate over then create a dataframe from each group and get totals, but totals are not for the day:

grouped = df.groupby('item_type')
for item_type, group in grouped:
    df = group.groupby(['date_time']).sum()

How can I get total quantities for each item type per day, with missing days given a quantity value of zero as per desired output below?

item_type   date_time  quantity
543  2018-01-22    32.0
     2018-01-23    0
     2018-01-24    0
     2018-01-25    0
     2018-01-26    12.0
842  2018-01-22    0
     2018-01-23    23.0
     2018-01-24    0
     2018-01-25    0
     2018-01-26    0
cs95

I'm not sure how easy this would be to do during the groupby, but you sure can do this after grouping. Create a date range using pd.date_range and then reindex.

g = df.groupby('item_type').resample('D')['quantity'].sum()
dates = pd.date_range(
      g.index.levels[0].min(), g.index.levels[0].max()
) 
idx = pd.MultiIndex.from_product([g.index.levels[0], dates])

g.reindex(idx, fill_value=0)

543  2018-01-22    32
     2018-01-23     0
     2018-01-24     0
     2018-01-25     0
     2018-01-26    12
842  2018-01-22     0
     2018-01-23    23
     2018-01-24     0
     2018-01-25     0
     2018-01-26     0
Name: quantity, dtype: int64

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to complete time series data with some missing dates with pandas

From Dev

Fill missing dates by group in pandas

From Dev

Add missing dates to pandas dataframe

From Dev

Pad data frame with missing dates in a series

From Dev

transform data frame with missing dates into time series

From Dev

transform data frame with missing dates into time series

From Dev

Pandas cumulative function of series with dates and NaT

From Dev

Change values in pandas Series between dates

From Dev

reindex to add missing dates to pandas dataframe

From Dev

Pandas apply to multiple rows with missing dates

From Dev

Filling missing dates and values for several groups - Pandas

From Dev

Pandas resample by day without filling missing dates

From Dev

Pandas generate missing dates & hours with 0 values

From Dev

Add Missing Dates for Groups in Pandas, using min/max Dates of the Group

From Dev

how to smart indexing regard to dates in a data with missing dates in pandas

From Dev

generate_series for creating a list of months to get missing dates

From Dev

pandas shift time series with missing values

From Dev

Python 2.7 pandas to fill missing number/series

From Dev

Merge two pandas series based on missing data

From Dev

Pandas time subset time series - dates ABOVE certain time

From Dev

How to combine dates and hours column into one index column in a pandas series?

From Dev

ValueError: Series lengths must match to compare when matching dates in Pandas

From Dev

Time-series x-axis dates from datetimeindex pandas

From Dev

How to replace NaTs in a date column with dates in a pandas series?

From Dev

Find out the values and dates from series of data in pandas frame

From Dev

Time-series x-axis dates from datetimeindex pandas

From Dev

Adding Dates (Series) column from one DataFrame to the other Pandas, Python

From Dev

How to add missing dates to index with correct date format in pandas?

From Dev

Making missing time slots and filling in 0 values in a Pandas series

Related Related

  1. 1

    How to complete time series data with some missing dates with pandas

  2. 2

    Fill missing dates by group in pandas

  3. 3

    Add missing dates to pandas dataframe

  4. 4

    Pad data frame with missing dates in a series

  5. 5

    transform data frame with missing dates into time series

  6. 6

    transform data frame with missing dates into time series

  7. 7

    Pandas cumulative function of series with dates and NaT

  8. 8

    Change values in pandas Series between dates

  9. 9

    reindex to add missing dates to pandas dataframe

  10. 10

    Pandas apply to multiple rows with missing dates

  11. 11

    Filling missing dates and values for several groups - Pandas

  12. 12

    Pandas resample by day without filling missing dates

  13. 13

    Pandas generate missing dates & hours with 0 values

  14. 14

    Add Missing Dates for Groups in Pandas, using min/max Dates of the Group

  15. 15

    how to smart indexing regard to dates in a data with missing dates in pandas

  16. 16

    generate_series for creating a list of months to get missing dates

  17. 17

    pandas shift time series with missing values

  18. 18

    Python 2.7 pandas to fill missing number/series

  19. 19

    Merge two pandas series based on missing data

  20. 20

    Pandas time subset time series - dates ABOVE certain time

  21. 21

    How to combine dates and hours column into one index column in a pandas series?

  22. 22

    ValueError: Series lengths must match to compare when matching dates in Pandas

  23. 23

    Time-series x-axis dates from datetimeindex pandas

  24. 24

    How to replace NaTs in a date column with dates in a pandas series?

  25. 25

    Find out the values and dates from series of data in pandas frame

  26. 26

    Time-series x-axis dates from datetimeindex pandas

  27. 27

    Adding Dates (Series) column from one DataFrame to the other Pandas, Python

  28. 28

    How to add missing dates to index with correct date format in pandas?

  29. 29

    Making missing time slots and filling in 0 values in a Pandas series

HotTag

Archive