Skip to content

Commit 5a2d4e4

Browse files
address feedback
1 parent 46ce29b commit 5a2d4e4

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

pandas/core/arrays/string_.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,11 @@ def _putmask(self, mask: npt.NDArray[np.bool_], value) -> None:
734734
ExtensionArray._putmask(self, mask, value)
735735

736736
def isin(self, values: ArrayLike) -> npt.NDArray[np.bool_]:
737-
if not isinstance(values, BaseStringArray):
737+
if isinstance(values, BaseStringArray) or (
738+
isinstance(values, ExtensionArray) and is_string_dtype(values.dtype)
739+
):
740+
values = values.astype(self.dtype, copy=False)
741+
else:
738742
if not lib.is_string_array(np.asarray(values), skipna=True):
739743
values = np.array(
740744
[val for val in values if isinstance(val, str) or isna(val)],
@@ -744,8 +748,6 @@ def isin(self, values: ArrayLike) -> npt.NDArray[np.bool_]:
744748
return np.zeros(self.shape, dtype=bool)
745749

746750
values = self._from_sequence(values, dtype=self.dtype)
747-
else:
748-
values = values.astype(self.dtype, copy=False)
749751

750752
return isin(np.asarray(self), np.asarray(values))
751753

pandas/tests/arrays/string_/test_string.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,10 @@ def test_isin(dtype, fixed_now_ts):
682682
expected = pd.Series([True, False, False])
683683
tm.assert_series_equal(result, expected)
684684

685+
result = s.isin([fixed_now_ts])
686+
expected = pd.Series([False, False, False])
687+
tm.assert_series_equal(result, expected)
688+
685689

686690
def test_isin_string_array(dtype, dtype2):
687691
s = pd.Series(["a", "b", None], dtype=dtype)
@@ -695,6 +699,19 @@ def test_isin_string_array(dtype, dtype2):
695699
tm.assert_series_equal(result, expected)
696700

697701

702+
def test_isin_arrow_string_array(dtype):
703+
pa = pytest.importorskip("pyarrow")
704+
s = pd.Series(["a", "b", None], dtype=dtype)
705+
706+
result = s.isin(pd.array(["a", "c"], dtype=pd.ArrowDtype(pa.string())))
707+
expected = pd.Series([True, False, False])
708+
tm.assert_series_equal(result, expected)
709+
710+
result = s.isin(pd.array(["a", None], dtype=pd.ArrowDtype(pa.string())))
711+
expected = pd.Series([True, False, True])
712+
tm.assert_series_equal(result, expected)
713+
714+
698715
def test_setitem_scalar_with_mask_validation(dtype):
699716
# https://github.com/pandas-dev/pandas/issues/47628
700717
# setting None with a boolean mask (through _putmaks) should still result

0 commit comments

Comments
 (0)