Skip to content

Commit 414154f

Browse files
BUG: Replaced ValueError exception with TypeError exception in df.where() function (#56495)
* Replaced ValueError exception with TypeError exception in df.where() function Replaced ValueError exception with TypeError as it is more appropriate for the error it serves. * Updated test_where.py to account for updated exceptions. * Update test_indexing.py to account for modified exceptions * Update test_where.py for series * Added bug fix to v2.2.0.rst * Updated v2.2.0.rst * Updated v2.2.0.rst * Updated v2.2.0.rst * Updated v2.2.0.rst * Updated v2.2.0.rst * resolved merge conflict * resolved conflicts again * Updated v2.2.0.rst * Updated v2.2.0.rst * removed change from 2.20 * Update v2.3.0.rst * Update doc/source/whatsnew/v2.3.0.rst Co-authored-by: Matthew Roeschke <[email protected]> --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 327af12 commit 414154f

File tree

5 files changed

+8
-6
lines changed

5 files changed

+8
-6
lines changed

doc/source/whatsnew/v2.3.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ Styler
212212

213213
Other
214214
^^^^^
215+
- 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`)
216+
215217

216218
.. ***DO NOT USE THIS SECTION***
217219

pandas/core/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10675,11 +10675,11 @@ def _where(
1067510675
if not isinstance(cond, ABCDataFrame):
1067610676
# This is a single-dimensional object.
1067710677
if not is_bool_dtype(cond):
10678-
raise ValueError(msg.format(dtype=cond.dtype))
10678+
raise TypeError(msg.format(dtype=cond.dtype))
1067910679
else:
1068010680
for _dt in cond.dtypes:
1068110681
if not is_bool_dtype(_dt):
10682-
raise ValueError(msg.format(dtype=_dt))
10682+
raise TypeError(msg.format(dtype=_dt))
1068310683
if cond._mgr.any_extension_types:
1068410684
# GH51574: avoid object ndarray conversion later on
1068510685
cond = cond._constructor(

pandas/tests/frame/indexing/test_indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def test_getitem_boolean(self, mixed_float_frame, mixed_int_frame, datetime_fram
136136
subframe_obj = datetime_frame[indexer_obj]
137137
tm.assert_frame_equal(subframe_obj, subframe)
138138

139-
with pytest.raises(ValueError, match="Boolean array expected"):
139+
with pytest.raises(TypeError, match="Boolean array expected"):
140140
datetime_frame[datetime_frame]
141141

142142
# test that Series work

pandas/tests/frame/indexing/test_where.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def test_where_invalid_input_single(self, cond):
250250
df = DataFrame({"a": [1, 2, 3]})
251251
msg = "Boolean array expected for the condition"
252252

253-
with pytest.raises(ValueError, match=msg):
253+
with pytest.raises(TypeError, match=msg):
254254
df.where(cond)
255255

256256
@pytest.mark.parametrize(
@@ -272,7 +272,7 @@ def test_where_invalid_input_multiple(self, cond):
272272
df = DataFrame({"a": [1, 2, 3], "b": [2, 2, 2]})
273273
msg = "Boolean array expected for the condition"
274274

275-
with pytest.raises(ValueError, match=msg):
275+
with pytest.raises(TypeError, match=msg):
276276
df.where(cond)
277277

278278
def test_where_dataframe_col_match(self):

pandas/tests/series/indexing/test_where.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def test_where_invalid_input(cond):
201201
s = Series([1, 2, 3])
202202
msg = "Boolean array expected for the condition"
203203

204-
with pytest.raises(ValueError, match=msg):
204+
with pytest.raises(TypeError, match=msg):
205205
s.where(cond)
206206

207207
msg = "Array conditional must be same shape as self"

0 commit comments

Comments
 (0)