diff --git a/doc/source/whatsnew/v2.3.0.rst b/doc/source/whatsnew/v2.3.0.rst index e217e8c8557bb..0d96678bba82d 100644 --- a/doc/source/whatsnew/v2.3.0.rst +++ b/doc/source/whatsnew/v2.3.0.rst @@ -211,6 +211,8 @@ Styler Other ^^^^^ +- Bug in :meth:`DataFrame.where` where using a non-bool type array in the function would return a ``ValueError`` instead of a ``TypeError`` (:issue:`56330`) + .. ***DO NOT USE THIS SECTION*** diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 81ffb243cd302..0a2c977cc8419 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -10675,11 +10675,11 @@ def _where( if not isinstance(cond, ABCDataFrame): # This is a single-dimensional object. if not is_bool_dtype(cond): - raise ValueError(msg.format(dtype=cond.dtype)) + raise TypeError(msg.format(dtype=cond.dtype)) else: for _dt in cond.dtypes: if not is_bool_dtype(_dt): - raise ValueError(msg.format(dtype=_dt)) + raise TypeError(msg.format(dtype=_dt)) if cond._mgr.any_extension_types: # GH51574: avoid object ndarray conversion later on cond = cond._constructor( diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index a9ee31299d469..0373c15d15272 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -136,7 +136,7 @@ def test_getitem_boolean(self, mixed_float_frame, mixed_int_frame, datetime_fram subframe_obj = datetime_frame[indexer_obj] tm.assert_frame_equal(subframe_obj, subframe) - with pytest.raises(ValueError, match="Boolean array expected"): + with pytest.raises(TypeError, match="Boolean array expected"): datetime_frame[datetime_frame] # test that Series work diff --git a/pandas/tests/frame/indexing/test_where.py b/pandas/tests/frame/indexing/test_where.py index 3d36d0471f02f..7a66b247ebcaa 100644 --- a/pandas/tests/frame/indexing/test_where.py +++ b/pandas/tests/frame/indexing/test_where.py @@ -250,7 +250,7 @@ def test_where_invalid_input_single(self, cond): df = DataFrame({"a": [1, 2, 3]}) msg = "Boolean array expected for the condition" - with pytest.raises(ValueError, match=msg): + with pytest.raises(TypeError, match=msg): df.where(cond) @pytest.mark.parametrize( @@ -272,7 +272,7 @@ def test_where_invalid_input_multiple(self, cond): df = DataFrame({"a": [1, 2, 3], "b": [2, 2, 2]}) msg = "Boolean array expected for the condition" - with pytest.raises(ValueError, match=msg): + with pytest.raises(TypeError, match=msg): df.where(cond) def test_where_dataframe_col_match(self): diff --git a/pandas/tests/series/indexing/test_where.py b/pandas/tests/series/indexing/test_where.py index e1139ea75f48b..dac01e49098d9 100644 --- a/pandas/tests/series/indexing/test_where.py +++ b/pandas/tests/series/indexing/test_where.py @@ -201,7 +201,7 @@ def test_where_invalid_input(cond): s = Series([1, 2, 3]) msg = "Boolean array expected for the condition" - with pytest.raises(ValueError, match=msg): + with pytest.raises(TypeError, match=msg): s.where(cond) msg = "Array conditional must be same shape as self"