Skip to content

Commit 75db35b

Browse files
committed
API: str.center with pyarrow-backed string dtype
1 parent 078e11f commit 75db35b

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ Other API changes
277277
- Made ``dtype`` a required argument in :meth:`ExtensionArray._from_sequence_of_strings` (:issue:`56519`)
278278
- Passing a :class:`Series` input to :func:`json_normalize` will now retain the :class:`Series` :class:`Index`, previously output had a new :class:`RangeIndex` (:issue:`51452`)
279279
- Removed :meth:`Index.sort` which always raised a ``TypeError``. This attribute is not defined and will raise an ``AttributeError`` (:issue:`59283`)
280+
- The ``center`` method on :class:`Series` and :class:`Index` object ``str`` accessors with pyarrow-backed dtype now matches the python behavior in corner cases with an odd number of fill characters when using pyarrow versions 17.0 and above (:issue:`54792`)
280281
- Updated :meth:`DataFrame.to_excel` so that the output spreadsheet has no styling. Custom styling can still be done using :meth:`Styler.to_excel` (:issue:`54154`)
281282
- pickle and HDF (``.h5``) files created with Python 2 are no longer explicitly supported (:issue:`57387`)
282283
- pickled objects from pandas version less than ``1.0.0`` are no longer supported (:issue:`57155`)

pandas/core/arrays/_arrow_string_mixins.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
from __future__ import annotations
22

3+
from functools import partial
34
from typing import (
45
TYPE_CHECKING,
56
Literal,
67
)
78

89
import numpy as np
910

10-
from pandas.compat import pa_version_under10p1
11+
from pandas.compat import (
12+
pa_version_under10p1,
13+
pa_version_under17p0,
14+
)
1115

1216
from pandas.core.dtypes.missing import isna
1317

@@ -49,7 +53,11 @@ def _str_pad(
4953
elif side == "right":
5054
pa_pad = pc.utf8_rpad
5155
elif side == "both":
52-
pa_pad = pc.utf8_center
56+
if pa_version_under17p0:
57+
pa_pad = pc.utf8_center
58+
else:
59+
# GH#54792
60+
pa_pad = partial(pc.utf8_center, lean_left_on_odd_padding=False)
5361
else:
5462
raise ValueError(
5563
f"Invalid side: {side}. Side must be one of 'left', 'right', 'both'"

pandas/core/arrays/string_arrow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ def astype(self, dtype, copy: bool = True):
284284
_str_map = BaseStringArray._str_map
285285
_str_startswith = ArrowStringArrayMixin._str_startswith
286286
_str_endswith = ArrowStringArrayMixin._str_endswith
287+
_str_pad = ArrowStringArrayMixin._str_pad
287288

288289
def _str_contains(
289290
self, pat, case: bool = True, flags: int = 0, na=np.nan, regex: bool = True
@@ -546,7 +547,6 @@ class ArrowStringArrayNumpySemantics(ArrowStringArray):
546547
_str_get = ArrowStringArrayMixin._str_get
547548
_str_removesuffix = ArrowStringArrayMixin._str_removesuffix
548549
_str_capitalize = ArrowStringArrayMixin._str_capitalize
549-
_str_pad = ArrowStringArrayMixin._str_pad
550550
_str_title = ArrowStringArrayMixin._str_title
551551
_str_swapcase = ArrowStringArrayMixin._str_swapcase
552552
_str_slice_replace = ArrowStringArrayMixin._str_slice_replace

pandas/tests/strings/test_case_justify.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,7 @@ def test_center_ljust_rjust_mixed_object():
291291

292292

293293
def test_center_ljust_rjust_fillchar(any_string_dtype):
294-
if any_string_dtype == "string[pyarrow_numpy]":
295-
pytest.skip(
296-
"Arrow logic is different, "
297-
"see https://github.com/pandas-dev/pandas/pull/54533/files#r1299808126",
298-
)
294+
# GH#54533, GH#54792
299295
s = Series(["a", "bb", "cccc", "ddddd", "eeeeee"], dtype=any_string_dtype)
300296

301297
result = s.str.center(5, fillchar="X")

0 commit comments

Comments
 (0)