Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused levels from categorical unique_id #473

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions nbs/core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,11 @@
" self._validate_df(df)\n",
" if df.index.name != 'unique_id':\n",
" df = df.set_index('unique_id')\n",
" if isinstance(df.index, pd.CategoricalIndex):\n",
" # If `unique_id` is categorical and some levels are not present\n",
" # in `df`, then `.count_values()` in `unique_id` does not\n",
" # produce expected output, so drop these unused levels.\n",
" df.index = df.index.remove_unused_categories()\n",
" df = _parse_ds_type(df)\n",
" self.ga, self.uids, self.last_dates, self.ds = _grouped_array_from_df(df, sort_df)\n",
" self.n_jobs = _get_n_jobs(len(self.ga), self.n_jobs)\n",
Expand Down
5 changes: 5 additions & 0 deletions statsforecast/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,11 @@ def _prepare_fit(self, df, sort_df):
self._validate_df(df)
if df.index.name != "unique_id":
df = df.set_index("unique_id")
if isinstance(df.index, pd.CategoricalIndex):
# If `unique_id` is categorical and some levels are not present
# in `df`, then `.count_values()` in `unique_id` does not
# produce expected output, so drop these unused levels.
df.index = df.index.remove_unused_categories()
df = _parse_ds_type(df)
self.ga, self.uids, self.last_dates, self.ds = _grouped_array_from_df(
df, sort_df
Expand Down