Skip to content

Commit

Permalink
Update base branch (#1)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tigerhawkvok authored Sep 9, 2024
1 parent 47b56ea commit 9ebc1a4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
18 changes: 16 additions & 2 deletions pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,25 @@ def mask_missing(arr: ArrayLike, values_to_mask) -> npt.NDArray[np.bool_]:
new_mask = np.zeros(arr.shape, dtype=np.bool_)
new_mask[arr_mask] = arr[arr_mask] == x
else:
new_mask = arr == x
# GH#47101
# Fix where type bool has no attribute to_numpy() by first
# attempting to broadcast with np.equal for some cases, and then
# an explicit type check when checking the mask for any straggling
# cases. Where a literal comparison would fail np.equal we fall back
# to the original equality check.
try:
# In case of an uncastable type, this will emit TypeError
new_mask = np.equal(arr, x) # type: ignore[arg-type]
except TypeError:
# Old behaviour for uncastable types
new_mask = arr == x

if not isinstance(new_mask, np.ndarray):
# usually BooleanArray
new_mask = new_mask.to_numpy(dtype=bool, na_value=False)
if isinstance(new_mask, bool):
new_mask = np.array([new_mask], dtype= bool)
else:
new_mask = new_mask.to_numpy(dtype=bool, na_value=False)
mask |= new_mask

if na_mask.any():
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/frame/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1518,3 +1518,10 @@ def test_replace_object_splitting(self, using_infer_string):
assert len(df._mgr.blocks) == 2
else:
assert len(df._mgr.blocks) == 1

def test_replace_bool_to_numpy_attributeerror(self):
# GH#47101
pass_pre_patch = DataFrame({"d":[None]})
tm.assert_frame_equal(pass_pre_patch, pass_pre_patch.replace("", pd.NA))
fail_pre_patch = DataFrame({"d":[pd.NA]})
tm.assert_frame_equal(fail_pre_patch, fail_pre_patch.replace("", pd.NA))

0 comments on commit 9ebc1a4

Please sign in to comment.