Skip to content

Commit

Permalink
only change column order when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoralez committed Dec 24, 2021
1 parent a3cfcad commit e0827d0
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions python-package/lightgbm/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,13 @@ def _data_from_pandas(data, feature_name, categorical_feature, pandas_categorica
if feature_name == 'auto' or feature_name is None:
data = data.rename(columns=str)
elif isinstance(feature_name, list) and is_predict:
features_df = data.columns.astype(str).tolist()
missing_features = set(feature_name) - set(features_df)
df_features = [str(x) for x in data.columns]
missing_features = set(feature_name) - set(df_features)
if missing_features:
raise ValueError(f'The following features are missing: {missing_features}')
sort_idxs = [features_df.index(feature) for feature in feature_name]
data = data.iloc[:, sort_idxs] # ensure column order
sort_idxs = [df_features.index(feature) for feature in feature_name]
if not all(x == i for i, x in enumerate(sort_idxs)):
data = data.iloc[:, sort_idxs] # ensure column order
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]
if pandas_categorical is None: # train dataset
Expand Down

0 comments on commit e0827d0

Please sign in to comment.