From 19f09407bb23712c479cfec988e97d3ae1643d49 Mon Sep 17 00:00:00 2001 From: Atharva Date: Tue, 13 Oct 2026 19:56:13 +0530 Subject: [PATCH 1/2] automating category_dtype conversion test cases --- openml/datasets/functions.py | 19 ++++++++++++------- tests/test_datasets/test_dataset_functions.py | 18 +++++++++++++++++- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/openml/datasets/functions.py b/openml/datasets/functions.py index 970331cf1..614f72c7e 100644 --- a/openml/datasets/functions.py +++ b/openml/datasets/functions.py @@ -577,13 +577,18 @@ def attributes_arff_from_df(df: pd.DataFrame) -> list[tuple[str, list[str] | str categories = df[column_name].cat.categories categories_dtype = pd.api.types.infer_dtype(categories) if categories_dtype not in ("string", "unicode"): - raise ValueError( - f"The column '{column_name}' of the dataframe is of " - "'category' dtype. Therefore, all values in " - "this columns should be string. Please " - "convert the entries which are not string. " - f"Got {categories_dtype} dtype in this column.", - ) + try: + # [ENH] Attempt to convert categories to string automatically + new_categories = categories.astype(str) + df[column_name] = df[column_name].cat.rename_categories(new_categories) + categories = df[column_name].cat.categories + except Exception as e: + raise ValueError( + f"The column '{column_name}' of the dataframe is of " + "'category' dtype. Therefore, all values in " + "this columns should be string. Automatic conversion failed. " + f"Got {categories_dtype} dtype in this column.", + ) attributes_arff.append((column_name, categories.tolist())) elif column_dtype == "boolean": # boolean are encoded as categorical. diff --git a/tests/test_datasets/test_dataset_functions.py b/tests/test_datasets/test_dataset_functions.py index fae4a1c4e..f138c2b48 100644 --- a/tests/test_datasets/test_dataset_functions.py +++ b/tests/test_datasets/test_dataset_functions.py @@ -663,7 +663,7 @@ def test_attributes_arff_from_df_numeric_column(self): ] def test_attributes_arff_from_df_mixed_dtype_categories(self): - # liac-arff imposed categorical attributes to be of sting dtype. We + # liac-arff imposed categorical attributes to be of string dtype. We # raise an error if this is not the case. df = pd.DataFrame([[1], ["2"], [3.0]]) df[0] = df[0].astype("category") @@ -671,6 +671,22 @@ def test_attributes_arff_from_df_mixed_dtype_categories(self): with pytest.raises(ValueError, match=err_msg): attributes_arff_from_df(df) + def test_attributes_arff_from_df_integer_categories(self): + df = pd.DataFrame({"category": [1,2,1]}) + df["category"] = df["category"].astype("category") + + attributes = attributes_arff_from_df(df) + assert attributes == [("category", ["1", "2"])] + + def test_attributes_arff_from_df_missing_values(self): + df = pd.DataFrame({"category": [1, None, 2]}) + df["category"] = df["category"].astype("category") + + attributes = attributes_arff_from_df(df) + + assert attributes == [("category", ["1", "2"])] + assert df["category"].isna().sum() == 1 + def test_attributes_arff_from_df_unknown_dtype(self): # check that an error is raised when the dtype is not supptagorted by # liac-arff From 465d483da74f4c20284c7ab7a3572577a01fe7fd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 13 Oct 2026 14:54:22 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- openml/datasets/functions.py | 2 +- tests/test_datasets/test_dataset_functions.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/openml/datasets/functions.py b/openml/datasets/functions.py index 614f72c7e..d32b1210b 100644 --- a/openml/datasets/functions.py +++ b/openml/datasets/functions.py @@ -582,7 +582,7 @@ def attributes_arff_from_df(df: pd.DataFrame) -> list[tuple[str, list[str] | str new_categories = categories.astype(str) df[column_name] = df[column_name].cat.rename_categories(new_categories) categories = df[column_name].cat.categories - except Exception as e: + except Exception: raise ValueError( f"The column '{column_name}' of the dataframe is of " "'category' dtype. Therefore, all values in " diff --git a/tests/test_datasets/test_dataset_functions.py b/tests/test_datasets/test_dataset_functions.py index f138c2b48..ed74d77ca 100644 --- a/tests/test_datasets/test_dataset_functions.py +++ b/tests/test_datasets/test_dataset_functions.py @@ -672,7 +672,7 @@ def test_attributes_arff_from_df_mixed_dtype_categories(self): attributes_arff_from_df(df) def test_attributes_arff_from_df_integer_categories(self): - df = pd.DataFrame({"category": [1,2,1]}) + df = pd.DataFrame({"category": [1, 2, 1]}) df["category"] = df["category"].astype("category") attributes = attributes_arff_from_df(df)