Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bab4b9a

Browse files
committed
BUG: Fix Series.str.zfill for ArrowDtype string arrays #61485
1 parent cfe54bd commit bab4b9a

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

‎doc/source/whatsnew/v3.0.0.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,8 +725,8 @@ Conversion
725725

726726
Strings
727727
^^^^^^^
728+
- Bug in :meth:`Series.str.zfill` raising ``AttributeError`` for ``ArrowDtype(pa.string())``. Now supported via ``_str_zfill`` implementation in ``ArrowExtensionArray`` (:issue:`61485`)
728729
- Bug in :meth:`Series.value_counts` would not respect ``sort=False`` for series having ``string`` dtype (:issue:`55224`)
729-
-
730730

731731
Interval
732732
^^^^^^^^

‎pandas/core/arrays/arrow/array.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,6 +2601,12 @@ def _str_wrap(self, width: int, **kwargs) -> Self:
26012601
result = self._apply_elementwise(predicate)
26022602
return type(self)(pa.chunked_array(result))
26032603

2604+
def _str_zfill(self, width: int) -> Self:
2605+
# TODO: Replace with pc.utf8_zfill when supported by arrow
2606+
predicate = lambda val: val.zfill(width)
2607+
result = self._apply_elementwise(predicate)
2608+
return type(self)(pa.chunked_array(result))
2609+
26042610
@property
26052611
def _dt_days(self) -> Self:
26062612
return type(self)(

‎pandas/core/strings/accessor.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1939,7 +1939,13 @@ def zfill(self, width: int):
19391939
msg = f"width must be of integer type, not {type(width).__name__}"
19401940
raise TypeError(msg)
19411941
f = lambda x: x.zfill(width)
1942-
result = self._data.array._str_map(f)
1942+
array = self._data.array
1943+
1944+
if hasattr(array, "_str_zfill"):
1945+
result = array._str_zfill(width)
1946+
else:
1947+
result = array._str_map(f)
1948+
19431949
return self._wrap_result(result)
19441950

19451951
def slice(self, start=None, stop=None, step=None):

‎pandas/tests/strings/test_string_array.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,19 @@ def test_string_array_extract(nullable_string_dtype):
110110

111111
result = result.astype(object)
112112
tm.assert_equal(result, expected)
113+
114+
115+
@pytest.mark.parametrize(
116+
"values, width, expected",
117+
[
118+
(["a", "ab", "abc", None], 4, ["000a", "00ab", "0abc", None]),
119+
(["1", "-1", "+1", None], 4, ["0001", "-001", "+001", None]),
120+
(["1234", "-1234"], 3, ["1234", "-1234"]),
121+
],
122+
)
123+
def test_string_array_zfill(nullable_string_dtype, values, width, expected):
124+
# GH #61485
125+
s = Series(values, dtype=nullable_string_dtype)
126+
result = s.str.zfill(width)
127+
expected = Series(expected, dtype=nullable_string_dtype)
128+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /