From 2b314e8c173c6b9bc46282eb00fb6b4d58a7d406 Mon Sep 17 00:00:00 2001 From: Zero Date: Fri, 19 Aug 2022 15:32:26 +0800 Subject: [PATCH 1/3] convert to ndarray first --- janitor/functions/encode_categorical.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/janitor/functions/encode_categorical.py b/janitor/functions/encode_categorical.py index 0800ab365..5227fedf2 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,7 +192,7 @@ 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): From ba0d2c0b76aa19b8a60b81454930f29d8879d44e Mon Sep 17 00:00:00 2001 From: Zero Date: Fri, 19 Aug 2022 15:33:45 +0800 Subject: [PATCH 2/3] simplify a bit `arr_ndim` only comes once --- janitor/functions/encode_categorical.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/janitor/functions/encode_categorical.py b/janitor/functions/encode_categorical.py index 5227fedf2..8ef898d37 100644 --- a/janitor/functions/encode_categorical.py +++ b/janitor/functions/encode_categorical.py @@ -194,8 +194,7 @@ def _as_categorical_checks(df: pd.DataFrame, **kwargs) -> dict: if not hasattr(value, "shape"): 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." From f0ad5c24cf090dd561fc00ec447a6a851b649e65 Mon Sep 17 00:00:00 2001 From: Zero Date: Fri, 19 Aug 2022 15:53:37 +0800 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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