From d714b90b20d897c08a2028ed3b2d17a545bf678a Mon Sep 17 00:00:00 2001 From: Khemkaran Date: Wed, 16 Jul 2025 11:46:59 +0530 Subject: [PATCH 1/3] none of the included dtypes present in df will raise clear error message --- pandas/core/methods/describe.py | 2 ++ pandas/tests/frame/methods/test_describe.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/pandas/core/methods/describe.py b/pandas/core/methods/describe.py index 944e28a9b0238..a0ccf192b3087 100644 --- a/pandas/core/methods/describe.py +++ b/pandas/core/methods/describe.py @@ -172,6 +172,8 @@ def describe(self, percentiles: Sequence[float] | np.ndarray) -> DataFrame: ldesc.append(describe_func(series, percentiles)) col_names = reorder_columns(ldesc) + if len(ldesc) == 0: + raise ValueError("None of the included dtypes are present in the DataFrame") d = concat( [x.reindex(col_names) for x in ldesc], axis=1, diff --git a/pandas/tests/frame/methods/test_describe.py b/pandas/tests/frame/methods/test_describe.py index 50656ca85e90a..a80c26c0a3996 100644 --- a/pandas/tests/frame/methods/test_describe.py +++ b/pandas/tests/frame/methods/test_describe.py @@ -371,6 +371,13 @@ def test_describe_when_include_all_exclude_not_allowed(self, exclude): with pytest.raises(ValueError, match=msg): df.describe(include="all", exclude=exclude) + def test_describe_when_included_dtypes_not_present(self): + # GH#61863 + df = pd.DataFrame({"a": [1, 2, 3]}) + msg = "None of the included dtypes are present in the DataFrame" + with pytest.raises(ValueError, match=msg): + df.describe(include=["datetime"]) + def test_describe_with_duplicate_columns(self): df = DataFrame( [[1, 1, 1], [2, 2, 2], [3, 3, 3]], From c171633914d62a94fd0409109e75c738aa51cb0d Mon Sep 17 00:00:00 2001 From: Khemkaran Date: Wed, 16 Jul 2025 11:58:06 +0530 Subject: [PATCH 2/3] minor change in test test_describe_when_included_dtypes_not_present --- pandas/tests/frame/methods/test_describe.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/frame/methods/test_describe.py b/pandas/tests/frame/methods/test_describe.py index a80c26c0a3996..33f1d6dd5fe09 100644 --- a/pandas/tests/frame/methods/test_describe.py +++ b/pandas/tests/frame/methods/test_describe.py @@ -373,7 +373,7 @@ def test_describe_when_include_all_exclude_not_allowed(self, exclude): def test_describe_when_included_dtypes_not_present(self): # GH#61863 - df = pd.DataFrame({"a": [1, 2, 3]}) + df = DataFrame({"a": [1, 2, 3]}) msg = "None of the included dtypes are present in the DataFrame" with pytest.raises(ValueError, match=msg): df.describe(include=["datetime"]) From 09245fdae2310a961a4b5b3fbf04db7c57632a26 Mon Sep 17 00:00:00 2001 From: Khemkaran Date: Wed, 16 Jul 2025 22:46:40 +0530 Subject: [PATCH 3/3] moved check to _select_data --- pandas/core/methods/describe.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/core/methods/describe.py b/pandas/core/methods/describe.py index a0ccf192b3087..cc33c4e83a5ac 100644 --- a/pandas/core/methods/describe.py +++ b/pandas/core/methods/describe.py @@ -172,8 +172,6 @@ def describe(self, percentiles: Sequence[float] | np.ndarray) -> DataFrame: ldesc.append(describe_func(series, percentiles)) col_names = reorder_columns(ldesc) - if len(ldesc) == 0: - raise ValueError("None of the included dtypes are present in the DataFrame") d = concat( [x.reindex(col_names) for x in ldesc], axis=1, @@ -201,6 +199,9 @@ def _select_data(self) -> DataFrame: include=self.include, exclude=self.exclude, ) + if len(data.columns) == 0: + msg = "None of the included dtypes are present in the DataFrame" + raise ValueError(msg) return data