diff --git a/CHANGELOG.md b/CHANGELOG.md index 374442297..78171f45f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/janitor/functions/encode_categorical.py b/janitor/functions/encode_categorical.py index 0800ab365..8ef898d37 100644 --- a/janitor/functions/encode_categorical.py +++ b/janitor/functions/encode_categorical.py @@ -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 from janitor.utils import check, check_column, deprecated_alias @@ -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]) + 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): raise ValueError( f"{value} is not a 1-D array. " "Kindly provide a 1-D array-like object."