business-science / pytimetk

Time series easier, faster, more fun. Pytimetk.
https://business-science.github.io/pytimetk/
MIT License
696 stars 60 forks source link

`tk.summarize_by_time()`: AttributeError: 'DataFrame' object has no attribute 'groupby' #298

Closed mdancho84 closed 3 months ago

mdancho84 commented 3 months ago
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File /Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/0[1](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/01_sales_demand_forecast.py:1)_sales_demand_forecast.py:4
      1 df \
      [2](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/01_sales_demand_forecast.py:2)     .to_pandas() \
      [3](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/01_sales_demand_forecast.py:3)     .groupby('category_2') \
----> [4](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/01_sales_demand_forecast.py:4)     .summarize_by_time(
      [5](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/01_sales_demand_forecast.py:5)         date_column='order_date',
      [6](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/01_sales_demand_forecast.py:6)         value_column='total_price',
      [7](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/01_sales_demand_forecast.py:7)         freq = 'MS',
      [8](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/01_sales_demand_forecast.py:8)         agg_func = 'sum',
      [9](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/01_sales_demand_forecast.py:9)         engine = 'polars'
     [10](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/01_sales_demand_forecast.py:10)     )

File ~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:209, in summarize_by_time(data, date_column, value_column, freq, agg_func, wide_format, fillna, engine)
    [207](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:207)     return _summarize_by_time_pandas(data, date_column, value_column, freq, agg_func, wide_format, fillna)
    [208](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:208) elif engine == 'polars':
--> [209](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:209)     return _summarize_by_time_polars(data, date_column, value_column, freq, agg_func, wide_format, fillna)
    [210](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:210) else:
    [211](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:211)     raise ValueError("Invalid engine. Use 'pandas' or 'polars'.")

File ~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:350, in _summarize_by_time_polars(data, date_column, value_column, freq, agg_func, wide_format, fillna)
    [343](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:343) exploded_df = df_pl.explode(columns=columns_to_explode)
    [345](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:345) # Group by groups and date
    [346](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:346) data = (
    [347](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:347)     exploded_df
    [348](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:348)         .select(groups + [date_column] + value_column)
    [349](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:349)         .with_columns(pl.col(date_column).dt.truncate(polars_freq))
--> [350](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:350)         .groupby(groups + [date_column])
    [351](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:351)         .agg(agg_columns)
    [352](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:352)         .sort(groups + [date_column])
    [353](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:353) )
    [355](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:355) if wide_format:
    [356](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:356)     # Value columns for aggregation
    [357](https://file+.vscode-resource.vscode-cdn.net/Users/mdancho/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/~/Desktop/learning_labs/lab_95_time_series_py_polars_mlforecast/.conda/lib/python3.11/site-packages/pytimetk/core/summarize_by_time.py:357)     values = data.select(pl.exclude([date_column] + groups)).columns

AttributeError: 'DataFrame' object has no attribute 'groupby'