Skip to content

Commit 090dc14

Browse files
authored
MNT _convert_container columns_name -> column_names (scikit-learn#33955)
1 parent a421648 commit 090dc14

11 files changed

Lines changed: 43 additions & 43 deletions

File tree

sklearn/compose/tests/test_column_transformer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def test_column_transformer_dataframe(constructor_name):
202202

203203
X_array = np.array([[0, 1, 2], [2, 4, 6]]).T
204204
X_df = _convert_container(
205-
X_array, constructor_name, columns_name=["first", "second"]
205+
X_array, constructor_name, column_names=["first", "second"]
206206
)
207207

208208
X_res_first = np.array([0, 1, 2]).reshape(-1, 1)
@@ -530,7 +530,7 @@ def test_column_transformer_list():
530530
@pytest.mark.parametrize("constructor_name", ["array", "pandas", "polars"])
531531
def test_column_transformer_sparse_stacking(csr_container, constructor_name):
532532
X = np.array([[0, 1, 2], [2, 4, 6]]).T
533-
X = _convert_container(X, constructor_name, columns_name=["first", "second"])
533+
X = _convert_container(X, constructor_name, column_names=["first", "second"])
534534

535535
col_trans = ColumnTransformer(
536536
[("trans1", Trans(), [0]), ("trans2", SparseMatrixTrans(csr_container), 1)],

sklearn/datasets/_arff_parser.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,21 +185,21 @@ def _io_to_generator(gzip_file):
185185
pd = check_pandas_support("fetch_openml with as_frame=True")
186186

187187
columns_info = OrderedDict(arff_container["attributes"])
188-
columns_names = list(columns_info.keys())
188+
column_names = list(columns_info.keys())
189189

190190
# calculate chunksize
191191
first_row = next(arff_container["data"])
192-
first_df = pd.DataFrame([first_row], columns=columns_names, copy=False)
192+
first_df = pd.DataFrame([first_row], columns=column_names, copy=False)
193193

194194
row_bytes = first_df.memory_usage(deep=True).sum()
195195
chunksize = get_chunk_n_rows(row_bytes)
196196

197197
# read arff data with chunks
198-
columns_to_keep = [col for col in columns_names if col in columns_to_select]
198+
columns_to_keep = [col for col in column_names if col in columns_to_select]
199199
dfs = [first_df[columns_to_keep]]
200200
for data in chunk_generator(arff_container["data"], chunksize):
201201
dfs.append(
202-
pd.DataFrame(data, columns=columns_names, copy=False)[columns_to_keep]
202+
pd.DataFrame(data, columns=column_names, copy=False)[columns_to_keep]
203203
)
204204
# dfs[0] contains only one row, which may not have enough data to infer to
205205
# column's dtype. Here we use `dfs[1]` to configure the dtype in dfs[0]

sklearn/ensemble/_hist_gradient_boosting/tests/test_monotonic_constraints.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ def test_predictions(global_random_seed, use_feature_names):
225225
f_c = rng.randint(low=0, high=9, size=n_samples)
226226

227227
X = np.c_[f_a, f_0, f_b, f_1, f_c]
228-
columns_name = ["f_a", "f_0", "f_b", "f_1", "f_c"]
228+
column_names = ["f_a", "f_0", "f_b", "f_1", "f_c"]
229229
constructor_name = "pandas" if use_feature_names else "array"
230-
X = _convert_container(X, constructor_name, columns_name=columns_name)
230+
X = _convert_container(X, constructor_name, column_names=column_names)
231231

232232
noise = rng.normal(loc=0.0, scale=0.01, size=n_samples)
233233
y = 5 * f_0 + np.sin(10 * np.pi * f_0) - 5 * f_1 - np.cos(10 * np.pi * f_1) + noise
@@ -261,24 +261,24 @@ def test_predictions(global_random_seed, use_feature_names):
261261
# First non-categorical feature (POS)
262262
# assert pred is all increasing when f_0 is all increasing
263263
X = np.c_[constant, linspace, constant, constant, constant]
264-
X = _convert_container(X, constructor_name, columns_name=columns_name)
264+
X = _convert_container(X, constructor_name, column_names=column_names)
265265
pred = gbdt.predict(X)
266266
assert is_increasing(pred)
267267
# assert pred actually follows the variations of f_0
268268
X = np.c_[constant, sin, constant, constant, constant]
269-
X = _convert_container(X, constructor_name, columns_name=columns_name)
269+
X = _convert_container(X, constructor_name, column_names=column_names)
270270
pred = gbdt.predict(X)
271271
assert np.all((np.diff(pred) >= 0) == (np.diff(sin) >= 0))
272272

273273
# Second non-categorical feature (NEG)
274274
# assert pred is all decreasing when f_1 is all increasing
275275
X = np.c_[constant, constant, constant, linspace, constant]
276-
X = _convert_container(X, constructor_name, columns_name=columns_name)
276+
X = _convert_container(X, constructor_name, column_names=column_names)
277277
pred = gbdt.predict(X)
278278
assert is_decreasing(pred)
279279
# assert pred actually follows the inverse variations of f_1
280280
X = np.c_[constant, constant, constant, sin, constant]
281-
X = _convert_container(X, constructor_name, columns_name=columns_name)
281+
X = _convert_container(X, constructor_name, column_names=column_names)
282282
pred = gbdt.predict(X)
283283
assert ((np.diff(pred) <= 0) == (np.diff(sin) >= 0)).all()
284284

sklearn/inspection/_plot/tests/test_boundary_decision_display.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ def test_dataframe_support(pyplot, constructor_name):
485485
* https://github.com/scikit-learn/scikit-learn/issues/28717
486486
"""
487487
df = _convert_container(
488-
X, constructor_name=constructor_name, columns_name=["col_x", "col_y"]
488+
X, constructor_name=constructor_name, column_names=["col_x", "col_y"]
489489
)
490490
estimator = LogisticRegression().fit(df, y)
491491

sklearn/inspection/_plot/tests/test_plot_partial_dependence.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def test_plot_partial_dependence_str_features(
206206
bmi = diabetes.data[:, diabetes.feature_names.index("bmi")]
207207

208208
X = _convert_container(
209-
diabetes.data, input_type, columns_name=diabetes.feature_names
209+
diabetes.data, input_type, column_names=diabetes.feature_names
210210
)
211211

212212
if feature_names_type is None:
@@ -808,8 +808,8 @@ def test_plot_partial_dependence_with_categorical(
808808
pyplot, categorical_features, array_type
809809
):
810810
X = [[1, 1, "A"], [2, 0, "C"], [3, 2, "B"]]
811-
column_name = ["col_A", "col_B", "col_C"]
812-
X = _convert_container(X, array_type, columns_name=column_name)
811+
column_names = ["col_A", "col_B", "col_C"]
812+
X = _convert_container(X, array_type, column_names=column_names)
813813
y = np.array([1.2, 0.5, 0.45]).T
814814

815815
preprocessor = make_column_transformer((OneHotEncoder(), categorical_features))
@@ -821,7 +821,7 @@ def test_plot_partial_dependence_with_categorical(
821821
model,
822822
X,
823823
features=["col_C"],
824-
feature_names=column_name,
824+
feature_names=column_names,
825825
categorical_features=categorical_features,
826826
)
827827

@@ -843,7 +843,7 @@ def test_plot_partial_dependence_with_categorical(
843843
model,
844844
X,
845845
features=[("col_A", "col_C")],
846-
feature_names=column_name,
846+
feature_names=column_names,
847847
categorical_features=categorical_features,
848848
)
849849

@@ -991,8 +991,8 @@ def test_grid_resolution_with_categorical(pyplot, categorical_features, array_ty
991991
respect to the number of categories in the categorical features targeted.
992992
"""
993993
X = [["A", 1, "A"], ["B", 0, "C"], ["C", 2, "B"]]
994-
column_name = ["col_A", "col_B", "col_C"]
995-
X = _convert_container(X, array_type, columns_name=column_name)
994+
column_names = ["col_A", "col_B", "col_C"]
995+
X = _convert_container(X, array_type, column_names=column_names)
996996
y = np.array([1.2, 0.5, 0.45]).T
997997

998998
preprocessor = make_column_transformer((OneHotEncoder(), categorical_features))
@@ -1007,7 +1007,7 @@ def test_grid_resolution_with_categorical(pyplot, categorical_features, array_ty
10071007
model,
10081008
X,
10091009
features=["col_C"],
1010-
feature_names=column_name,
1010+
feature_names=column_names,
10111011
categorical_features=categorical_features,
10121012
grid_resolution=2,
10131013
)

sklearn/inspection/tests/test_pd_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
def test_check_feature_names(feature_names, array_type, expected_feature_names):
1717
X = np.random.randn(10, 3)
1818
column_names = ["a", "b", "c"]
19-
X = _convert_container(X, constructor_name=array_type, columns_name=column_names)
19+
X = _convert_container(X, constructor_name=array_type, column_names=column_names)
2020
feature_names_validated = _check_feature_names(X, feature_names)
2121
assert feature_names_validated == expected_feature_names
2222

sklearn/preprocessing/tests/test_function_transformer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def test_function_transformer_raise_error_with_mixed_dtype(X_type):
192192
dtype = "object"
193193

194194
data = ["one", "two", "three", "one", "one", 5, 6]
195-
data = _convert_container(data, X_type, columns_name=["value"], dtype=dtype)
195+
data = _convert_container(data, X_type, column_names=["value"], dtype=dtype)
196196

197197
def func(X):
198198
return np.array([mapping[X[i]] for i in range(X.size)], dtype=object)
@@ -201,7 +201,7 @@ def inverse_func(X):
201201
return _convert_container(
202202
[inverse_mapping[x] for x in X],
203203
X_type,
204-
columns_name=["value"],
204+
column_names=["value"],
205205
dtype=dtype,
206206
)
207207

sklearn/tests/test_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ def test_feature_names_in_on_dataframes(constructor_name, minversion):
924924
data = [[1, 4, 2], [3, 3, 6]]
925925
columns = ["col_0", "col_1", "col_2"]
926926
df = _convert_container(
927-
data, constructor_name, columns_name=columns, minversion=minversion
927+
data, constructor_name, column_names=columns, minversion=minversion
928928
)
929929

930930
class NoOpTransformer(TransformerMixin, BaseEstimator):
@@ -946,7 +946,7 @@ def transform(self, X):
946946
assert_allclose(df, X_out)
947947

948948
bad_names = ["a", "b", "c"]
949-
df_bad = _convert_container(data, constructor_name, columns_name=bad_names)
949+
df_bad = _convert_container(data, constructor_name, column_names=bad_names)
950950
with pytest.raises(ValueError, match="The feature names should match"):
951951
no_op.transform(df_bad)
952952

sklearn/utils/_testing.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ def assert_run_python_script_without_output(source_code, pattern=".+", timeout=6
967967
def _convert_container(
968968
container,
969969
constructor_name,
970-
columns_name=None,
970+
column_names=None,
971971
dtype=None,
972972
minversion=None,
973973
categorical_feature_names=None,
@@ -983,8 +983,8 @@ def _convert_container(
983983
"sparse_csr_array", "sparse_csc_array", "pyarrow", "polars", \
984984
"polars_series"}
985985
The type of the returned container.
986-
columns_name : index or array-like, default=None
987-
For pandas/polars container supporting `columns_names`, it will affect
986+
column_names : index or array-like, default=None
987+
For pandas/polars container supporting `column_names`, it will affect
988988
specific names.
989989
dtype : dtype, default=None
990990
Force the dtype of the container. Does not apply to `"slice"`
@@ -1012,7 +1012,7 @@ def _convert_container(
10121012
return np.asarray(container, dtype=dtype)
10131013
elif constructor_name == "pandas":
10141014
pd = pytest.importorskip("pandas", minversion=minversion)
1015-
result = pd.DataFrame(container, columns=columns_name, dtype=dtype, copy=False)
1015+
result = pd.DataFrame(container, columns=column_names, dtype=dtype, copy=False)
10161016
if categorical_feature_names is not None:
10171017
for col_name in categorical_feature_names:
10181018
result[col_name] = result[col_name].astype("category")
@@ -1021,9 +1021,9 @@ def _convert_container(
10211021
pa = pytest.importorskip("pyarrow", minversion=minversion)
10221022
array = np.asarray(container)
10231023
array = array[:, None] if array.ndim == 1 else array
1024-
if columns_name is None:
1025-
columns_name = [f"col{i}" for i in range(array.shape[1])]
1026-
data = {name: array[:, i] for i, name in enumerate(columns_name)}
1024+
if column_names is None:
1025+
column_names = [f"col{i}" for i in range(array.shape[1])]
1026+
data = {name: array[:, i] for i, name in enumerate(column_names)}
10271027
result = pa.Table.from_pydict(data)
10281028
if categorical_feature_names is not None:
10291029
for col_idx, col_name in enumerate(result.column_names):
@@ -1034,7 +1034,7 @@ def _convert_container(
10341034
return result
10351035
elif constructor_name == "polars":
10361036
pl = pytest.importorskip("polars", minversion=minversion)
1037-
result = pl.DataFrame(container, schema=columns_name, orient="row")
1037+
result = pl.DataFrame(container, schema=column_names, orient="row")
10381038
if categorical_feature_names is not None:
10391039
for col_name in categorical_feature_names:
10401040
result = result.with_columns(pl.col(col_name).cast(pl.Categorical))

sklearn/utils/tests/test_indexing.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ def test_safe_indexing_2d_container_axis_1(array_type, indices_type, indices):
214214
if indices_type == "slice" and isinstance(indices[1], int):
215215
indices_converted[1] += 1
216216

217-
columns_name = ["col_0", "col_1", "col_2"]
217+
column_names = ["col_0", "col_1", "col_2"]
218218
array = _convert_container(
219-
[[1, 2, 3], [4, 5, 6], [7, 8, 9]], array_type, columns_name
219+
[[1, 2, 3], [4, 5, 6], [7, 8, 9]], array_type, column_names
220220
)
221221
indices_converted = _convert_container(indices_converted, indices_type)
222222

@@ -278,9 +278,9 @@ def test_safe_indexing_1d_container_mask(array_type, indices_type):
278278
[(0, [[4, 5, 6], [7, 8, 9]]), (1, [[2, 3], [5, 6], [8, 9]])],
279279
)
280280
def test_safe_indexing_2d_mask(array_type, indices_type, axis, expected_subset):
281-
columns_name = ["col_0", "col_1", "col_2"]
281+
column_names = ["col_0", "col_1", "col_2"]
282282
array = _convert_container(
283-
[[1, 2, 3], [4, 5, 6], [7, 8, 9]], array_type, columns_name
283+
[[1, 2, 3], [4, 5, 6], [7, 8, 9]], array_type, column_names
284284
)
285285
indices = [False, True, True]
286286
indices = _convert_container(indices, indices_type)
@@ -332,9 +332,9 @@ def test_safe_indexing_1d_scalar(array_type):
332332
)
333333
@pytest.mark.parametrize("indices", [2, "col_2"])
334334
def test_safe_indexing_2d_scalar_axis_1(array_type, expected_output_type, indices):
335-
columns_name = ["col_0", "col_1", "col_2"]
335+
column_names = ["col_0", "col_1", "col_2"]
336336
array = _convert_container(
337-
[[1, 2, 3], [4, 5, 6], [7, 8, 9]], array_type, columns_name
337+
[[1, 2, 3], [4, 5, 6], [7, 8, 9]], array_type, column_names
338338
)
339339

340340
if isinstance(indices, str) and array_type in ("array", "sparse"):
@@ -513,7 +513,7 @@ def test_get_column_indices_pandas_nonunique_columns_error(key):
513513
def test_get_column_indices_dataframes(constructor_name):
514514
"""Check _get_column_indices for edge cases with 2d input X."""
515515
df = _convert_container(
516-
[[1, 2, 3], [4, 5, 6]], constructor_name, columns_name=["a", "b", "c"]
516+
[[1, 2, 3], [4, 5, 6]], constructor_name, column_names=["a", "b", "c"]
517517
)
518518

519519
key_results = [

0 commit comments

Comments
 (0)