Skip to content

Commit 9ebc1a4

Browse files
authored
Update base branch (#1)
* Fix issue pandas-dev#47101 Bisecting two years ago ( pandas-dev#47101 (comment) ) shows this regression was introduced in b2d54d9 in 2021. Somehow this hasn't been patched since then. PR pandas-dev#48313 was supposed to address this, but the PR was closed and never merged and the bug has persisted. * Add a test as per PR guidelines * Fix typo * Resolve inconsistent namespace as per PR test https://results.pre-commit.ci/run/github/858127/1723498369.6V12SWx7T-WpLZDAXXkz0Q This web UI commit will still fail, as the E501 line-too-long check will fail until the next commit * Resolve E501 linting errors https://results.pre-commit.ci/run/github/858127/1723498369.6V12SWx7T-WpLZDAXXkz0Q * Fix test TypeErrors np.equal([1,2,3], "") fails * Quote style for Ruff * typing and remove code backtick possibly incorrectly triggering ruff formatter * mpy supression for caught error * trailing space
1 parent 47b56ea commit 9ebc1a4

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

pandas/core/missing.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,25 @@ def mask_missing(arr: ArrayLike, values_to_mask) -> npt.NDArray[np.bool_]:
124124
new_mask = np.zeros(arr.shape, dtype=np.bool_)
125125
new_mask[arr_mask] = arr[arr_mask] == x
126126
else:
127-
new_mask = arr == x
127+
# GH#47101
128+
# Fix where type bool has no attribute to_numpy() by first
129+
# attempting to broadcast with np.equal for some cases, and then
130+
# an explicit type check when checking the mask for any straggling
131+
# cases. Where a literal comparison would fail np.equal we fall back
132+
# to the original equality check.
133+
try:
134+
# In case of an uncastable type, this will emit TypeError
135+
new_mask = np.equal(arr, x) # type: ignore[arg-type]
136+
except TypeError:
137+
# Old behaviour for uncastable types
138+
new_mask = arr == x
128139

129140
if not isinstance(new_mask, np.ndarray):
130141
# usually BooleanArray
131-
new_mask = new_mask.to_numpy(dtype=bool, na_value=False)
142+
if isinstance(new_mask, bool):
143+
new_mask = np.array([new_mask], dtype= bool)
144+
else:
145+
new_mask = new_mask.to_numpy(dtype=bool, na_value=False)
132146
mask |= new_mask
133147

134148
if na_mask.any():

pandas/tests/frame/methods/test_replace.py

+7
Original file line numberDiff line numberDiff line change
@@ -1518,3 +1518,10 @@ def test_replace_object_splitting(self, using_infer_string):
15181518
assert len(df._mgr.blocks) == 2
15191519
else:
15201520
assert len(df._mgr.blocks) == 1
1521+
1522+
def test_replace_bool_to_numpy_attributeerror(self):
1523+
# GH#47101
1524+
pass_pre_patch = DataFrame({"d":[None]})
1525+
tm.assert_frame_equal(pass_pre_patch, pass_pre_patch.replace("", pd.NA))
1526+
fail_pre_patch = DataFrame({"d":[pd.NA]})
1527+
tm.assert_frame_equal(fail_pre_patch, fail_pre_patch.replace("", pd.NA))

0 commit comments

Comments
 (0)