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

TST/ENH: Enabel encode_categorical handle 2 (or more ) dimensions array #1153

Merged
merged 3 commits into from
Aug 19, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [BUG] Force `math.softmax` returning `Series`. PR #1139 @Zeroto521
- [INF] Set independent environment for building documentation. PR #1141 @Zeroto521
- [DOC] Add local documentation preview via github action artifact. PR #1149 @Zeroto521
- [ENH] Enabel `encode_categorical` handle 2 (or more ) dimensions array. PR #1153 @Zeroto521

## [v0.23.1] - 2022-05-03

Expand Down
8 changes: 4 additions & 4 deletions janitor/functions/encode_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from enum import Enum
from typing import Hashable, Iterable, Union

import pandas_flavor as pf
import numpy as np
import pandas as pd
import pandas_flavor as pf
from pandas.api.types import is_list_like
Comment on lines +5 to 8
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lint via isort


from janitor.utils import check, check_column, deprecated_alias
Expand Down Expand Up @@ -191,10 +192,9 @@ def _as_categorical_checks(df: pd.DataFrame, **kwargs) -> dict:
raise TypeError(f"{value} should be list-like or a string.")
if is_list_like(value):
if not hasattr(value, "shape"):
value = pd.Index([*value])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will convert value to pd.Index again in line 203.

value = np.asarray(value)

arr_ndim = value.ndim
if (arr_ndim != 1) or isinstance(value, pd.MultiIndex):
if (value.ndim != 1) or isinstance(value, pd.MultiIndex):
Copy link
Member Author

@Zeroto521 Zeroto521 Aug 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arr_ndim only comes once, so use value.ndim directly

raise ValueError(
f"{value} is not a 1-D array. "
"Kindly provide a 1-D array-like object."
Expand Down