Skip to content

Commit

Permalink
[python-package] fix mypy error about pandas categorical features (#6253
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jameslamb committed Jan 3, 2024
1 parent 2bd60c8 commit 48e3629
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions python-package/lightgbm/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ def _data_from_pandas(
feature_name: _LGBM_FeatureNameConfiguration,
categorical_feature: _LGBM_CategoricalFeatureConfiguration,
pandas_categorical: Optional[List[List]]
) -> Tuple[np.ndarray, List[str], List[str], List[List]]:
) -> Tuple[np.ndarray, List[str], Union[List[str], List[int]], List[List]]:
if len(data.shape) != 2 or data.shape[0] < 1:
raise ValueError('Input data must be 2 dimensional and non empty.')

Expand All @@ -800,7 +800,7 @@ def _data_from_pandas(

# determine categorical features
cat_cols = [col for col, dtype in zip(data.columns, data.dtypes) if isinstance(dtype, pd_CategoricalDtype)]
cat_cols_not_ordered = [col for col in cat_cols if not data[col].cat.ordered]
cat_cols_not_ordered: List[str] = [col for col in cat_cols if not data[col].cat.ordered]
if pandas_categorical is None: # train dataset
pandas_categorical = [list(data[col].cat.categories) for col in cat_cols]
else:
Expand All @@ -811,10 +811,10 @@ def _data_from_pandas(
data[col] = data[col].cat.set_categories(category)
if len(cat_cols): # cat_cols is list
data[cat_cols] = data[cat_cols].apply(lambda x: x.cat.codes).replace({-1: np.nan})
if categorical_feature == 'auto': # use cat cols from DataFrame

# use cat cols from DataFrame
if categorical_feature == 'auto':
categorical_feature = cat_cols_not_ordered
else: # use cat cols specified by user
categorical_feature = list(categorical_feature) # type: ignore[assignment]

df_dtypes = [dtype.type for dtype in data.dtypes]
# so that the target dtype considers floats
Expand Down

0 comments on commit 48e3629

Please sign in to comment.