Skip to content

Commit 2d92818

Browse files
committed
Manage str.encode for pyarrow.lib.LargeStringScalar
1 parent 4c61857 commit 2d92818

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

pandas/core/strings/object_array.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,15 @@ def _str_fullmatch(
239239
return self._str_map(f, na_value=na, dtype=np.dtype(bool))
240240

241241
def _str_encode(self, encoding, errors: str = "strict"):
242-
f = lambda x: x.encode(encoding, errors=errors)
243-
return self._str_map(f, dtype=object)
242+
def encode_func(x):
243+
if x is str:
244+
return x.encode(encoding=encoding, errors=errors)
245+
else:
246+
# Manage AttributeError: 'pyarrow.lib.LargeStringScalar'
247+
# object has no attribute 'encode'
248+
return str(x).encode(encoding=encoding, errors=errors)
249+
250+
return self._str_map(encode_func, dtype=object)
244251

245252
def _str_find(self, sub, start: int = 0, end=None):
246253
return self._str_find_(sub, start, end, side="left")

pandas/tests/strings/test_strings.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,16 @@ def test_encode_errors_kwarg(any_string_dtype):
561561
ser.str.encode("cp1252")
562562

563563
result = ser.str.encode("cp1252", "ignore")
564-
expected = ser.map(lambda x: x.encode("cp1252", "ignore"))
564+
565+
def encode_func(x):
566+
if x is str:
567+
return x.encode("cp1252", "ignore")
568+
else:
569+
# Manage AttributeError: 'pyarrow.lib.LargeStringScalar'
570+
# object has no attribute 'encode'
571+
return str(x).encode("cp1252", "ignore")
572+
573+
expected = ser.map(encode_func).astype("object")
565574
tm.assert_series_equal(result, expected)
566575

567576

0 commit comments

Comments
 (0)