some info about my dataset # Convert 'WeekDate' to datetime format datasetnewitem =…
- User
some info about my dataset # Convert 'WeekDate' to datetime format dataset_newitem = dataset_newitem.with_columns( pl.col("WeekDate").str.strptime(pl.Datetime, "%Y-%m-%d") )
Group by ‘MaterialID’, ‘SalesOrg’, ‘DistrChan’, 'CL4' and 'WeekDate', then sum 'OrderQuantity'
y_cl4 = dataset_newitem.groupby(['MaterialID', 'SalesOrg', 'DistrChan', 'CL4', 'WeekDate']).agg( pl.sum("OrderQuantity").alias("OrderQuantity") )
Sort by 'WeekDate'
y_cl4 = y_cl4.sort("WeekDate") # Concatenate ‘MaterialID’, ‘SalesOrg’, ‘DistrChan’, ‘CL4’ to a new column ‘unique_id’ y_cl4 = y_cl4.with_columns( pl.concat_str([pl.col('MaterialID'), pl.col('SalesOrg'), pl.col('DistrChan'), pl.col('CL4')], separator='_').alias('unique_id') )
Drop the original columns
y_cl4 = y_cl4.drop(['MaterialID', 'SalesOrg', 'DistrChan', 'CL4'])
Renaming columns to 'ds' and 'y' to meet the input requirements of the StatsForecast library
y_cl4 = y_cl4.rename({'WeekDate': 'ds', 'OrderQuantity': 'y'}) y_cl4.head() ds y unique_id datetime[μs] f64 str 2022-06-27 00:00:00 12.0 "12499186_US01_… 2022-06-27 00:00:00 128.0 "12506328_US01_… 2022-06-27 00:00:00 32.0 "12506326_US01_… 2022-06-27 00:00:00 96.0 "12520808_US01_… 2022-06-27 00:00:00 252.0 "12409760_US01_… 4275 series ranging from 1 week to 74 weeks, weekly data length ┆ count │ │ --- ┆ --- │ │ u32 ┆ u32 │ ╞════════╪═══════╡ │ 1 ┆ 1942 │ │ 2 ┆ 357 │ │ 3 ┆ 157 │ │ 4 ┆ 107 │ │ 5 ┆ 74 │ │ 6 ┆ 40 │ │ 7 ┆ 48 │ │ 8 ┆ 37 │ │ 9 ┆ 39 │ │ 10 ┆ 47 │ │ 11 ┆ 54 │ │ 12 ┆ 36 │ │ 13 ┆ 35 │ │ 14 ┆ 43 │ │ 15 ┆ 47 │ │ 16 ┆ 45 │ │ 17 ┆ 36 │ │ 18 ┆ 37 │ │ 19 ┆ 51 │ │ 20 ┆ 35 │ │ 21 ┆ 41 │ │ 22 ┆ 29 │ │ 23 ┆ 26 │ │ 24 ┆ 33 │ │ 25 ┆ 35 │ │ 26 ┆ 41 │ │ 27 ┆ 39 │ │ 28 ┆ 34 │ │ 29 ┆ 37 │ │ 30 ┆ 31 │ │ 31 ┆ 32 │ │ 32 ┆ 26 │ │ 33 ┆ 30 │ │ 34 ┆ 22 │ │ 35 ┆ 39 │ │ 36 ┆ 32 │ │ 37 ┆ 32 │ │ 38 ┆ 33 │ │ 39 ┆ 37 │ │ 40 ┆ 34 │ │ 41 ┆ 24 │ │ 42 ┆ 22 │ │ 43 ┆ 17 │ │ 44 ┆ 18 │ │ 45 ┆ 13 │ │ 46 ┆ 9 │ │ 47 ┆ 18 │ │ 48 ┆ 15 │ │ 49 ┆ 17 │ │ 50 ┆ 12 │ │ 51 ┆ 15 │ │ 52 ┆ 10 │ │ 53 ┆ 11 │ │ 54 ┆ 6 │ │ 55 ┆ 9 │ │ 56 ┆ 7 │ │ 57 ┆ 11 │ │ 58 ┆ 11 │ │ 59 ┆ 9 │ │ 60 ┆ 13 │ │ 61 ┆ 14 │ │ 62 ┆ 7 │ │ 63 ┆ 5 │ │ 64 ┆ 3 │ │ 65 ┆ 6 │ │ 66 ┆ 6 │ │ 67 ┆ 5 │ │ 68 ┆ 11 │ │ 69 ┆ 7 │ │ 70 ┆ 4 │ │ 71 ┆ 2 │ │ 72 ┆ 4 │ │ 73 ┆ 3 │ │ 74 ┆ 1 │ I'm training the ensemble models arima, ets, theta and imapa on series with 32 data points and up since it;s less noisy # Filter the lengths DataFrame for lengths greater than 31 lengths_filtered = lengths.filter(pl.col('length') > 31)
- Assistant
- User
show me the code how to do it
- Assistant
- User
hold on, we have to check the dates and how are they distributed first before imputation
- Assistant
- User
I'm using polars and I think you misunderstnd me, we do somehting simialr to this # Filtering series with 15 or fewer data points series_with_few_points = lengths.filter(pl.col('length') <= 15) limited_data_series = y_cl4.join( series_with_few_points.select(pl.col('unique_id')), on='unique_id', how='semi'
) so we kinda want to check if all the series with 15 or fewer data points have the same datapoint on the same time period. last date in dataset is end of november, so if the ones with 1 data point have this data point on end of november then we could backfill, but if they are distributed randomly then this might not be feasible - Assistant
Certainly, to check if the series with 15 or fewer data points have data points in the same time period, you can proceed to filter those series and then explore the distribution of their 'WeekDate' values. Here's how you might do this using Polars:
import polars as pl # You have a DataFrame called 'lengths' which contains the length of each series # and a DataFrame called 'y_cl4' which is the modified dataset # Filter series with 15 or fewer data points series_with_few_points = lengths.filter(pl.col('length') <= 15) # Join with the y_cl4 to get only these limited series limited_data_series = y_cl4.join( series_with_few_points.select(pl.col('unique_id')), on='unique_id', how='semi' ) # Check the min and max dates within the limited_data_series min_max_dates = limited_data_series.select([ pl.col('unique_id'), pl.min('ds').alias('min_date'), pl.max('ds').alias('max_date') ]).groupby('unique_id').agg() print(min_max_dates) # See the distribution of WeekDates across these series date_distribution = limited_data_series.groupby('ds').agg( pl.count('unique_id').alias('count') ).sort('ds') print(date_distribution) # Check if any of the series with few points have data points at the end of November end_of_november = limited_data_series.filter(pl.col('ds') == "2022-11-30") print(end_of_november) - User
Check the min and max dates within the limited_data_series
min_max_dates = limited_data_series.select([ pl.col('unique_id'), pl.min('ds').alias('min_date'), pl.max('ds').alias('max_date') ]).groupby('unique_id').agg()
print(min_max_dates) shape: (0, 1) ┌───────────┐ │ unique_id │ │ --- │ │ str │ ╞═══════════╡ └───────────┘
- Assistant
I apologize for the confusion. It seems there was an error in the code. The
groupbyshould be applied before theselectstatement, and the.agg()call is unnecessary sincemin()andmax()are already aggregation functions. Here is the corrected code to check the min and max dates within thelimited_data_series:# Check the min and max dates within the limited_data_series min_max_dates = limited_data_series.groupby('unique_id').agg([ pl.col('ds').min().alias('min_date'), pl.col('ds').max().alias('max_date') ]) print(min_max_dates)This code will group your data by
unique_idand then calculate the minimum and maximum dates for each , which should give you the desired result. If there are still issues, please ensure that the actually contains data after performing the join operation and that the join conditions are correct. If the is empty, the join might be filtering out all the rows, which means there is a mismatch in the or the logic in the join. - User
Check the min and max dates within the limited_data_series
min_max_dates = limited_data_series.groupby('unique_id').agg([ pl.col('ds').min().alias('min_date'), pl.col('ds').max().alias('max_date') ]) shape: (0, 3) ┌───────────┬──────────────┬──────────────┐ │ unique_id ┆ min_date ┆ max_date │ │ --- ┆ --- ┆ --- │ │ str ┆ datetime[μs] ┆ datetime[μs] │ ╞═══════════╪══════════════╪══════════════╡ └───────────┴──────────────┴──────────────┘ print(min_max_dates)
- Assistant
It looks like the
min_max_datesDataFrame is still not providing the expected output of unique_id, min_date, and max_date rows. The shape description (shape: (0, 3)) indicates that the result is an empty DataFrame with 0 rows and 3 columns, which means that thegroupbyandaggoperations did not produce any grouped data.This could be due to one of the following reasons:
- User
limited_data_series is empty
- Assistant
- User
okay back to to the beginning, how would you do this to see if this approach is feasbiel
- Assistant
- User
limited_series_grouped = limited_data_series.groupby('unique_id').agg([ pl.col('ds').list().alias('dates'), pl.col('ds').min().alias('min_date'), pl.col('ds').max().alias('max_date') ])
TypeError Traceback (most recent call last) Cell In[39], line 2 1 limited_series_grouped = limited_data_series.groupby('unique_id').agg([ ----> 2 pl.col('ds').list().alias('dates'), 3 pl.col('ds').min().alias('min_date'), 4 pl.col('ds').max().alias('max_date') 5 ])
TypeError: 'ExprListNameSpace' object is not callable, also, when I was saying about end of novermber, it was an example, meaning I think this appraoch would only be feasible if the dates kinda lined up, what I mean is that for this particular series, it has 1 data point on 02/22/22 and other similar series that matching materialID-salesorg-distrchan but not cl4 that could be used to provide order quantity via backfill or forward fill to meet the minimum threshold for the model to run, but if the other similar series only have data in 2023 and non in 2022 then this appraoch wouldn't make sense, you see what I'm saying?
- Assistant
- User
hold on, you dont ave to split into different components, there's dataset_newitem.head() MaterialID SalesOrg DistrChan SoldTo DC WeekDate OrderQuantity DeliveryQuantity ParentProductCode PL2 PL3 PL4 PL5 CL4 Item Type i64 str i64 i64 str str f64 f64 i64 str str str str str str 12407848 "US03" 8 1086912 "5420" "2022-08-22" 240.0 240.0 12407848 "US6" "US66E" "US66E6E30" "US66E6E306E30D… "1108285" "New Item" 12407848 "US03" 8 1091945 "5420" "2022-08-22" 24.0 24.0 12407848 "US6" "US66E" "US66E6E30" "US66E6E306E30D… "1108285" "New Item" 12407848 "US03" 8 1091945 "5420" "2022-08-08" 168.0 168.0 12407848 "US6" "US66E" "US66E6E30" "US66E6E306E30D… "1108285" "New Item" this is before the unqiue id is created by concating 4 column aggregation, we can use dataset_newitem instead of splitting
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.