Skip to content

Commit 49709c4

Browse files
[backport 3.1.x] BUG: ffill/bfill on Arrow arrays without NA returns a view of the original (#71745) (#71897)
Co-authored-by: Maha Rana Yadavalli <[email protected]> Co-authored-by: Maha Rana Yadavalli <[email protected]>
1 parent 55089f8 commit 49709c4

5 files changed

Lines changed: 31 additions & 17 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Bug fixes
2424
~~~~~~~~~
2525

2626
- Fixed ``AttributeError`` when using dateutil timezones such as ``"dateutil/US/Pacific"`` with python-dateutil 3.0 (:issue:`71459`)
27+
- Fixed a bug in :meth:`Series.ffill` and :meth:`Series.bfill` (and DataFrame equivalent) on a PyArrow-backed column without missing values (including the default ``str`` dtype) where the result shared data with the original, so modifying one also changed the other, violating Copy-on-Write semantics (:issue:`71515`)
2728
- Fixed bug in string methods using regex patterns on PyArrow-backed string dtypes where ``\s`` incorrectly did not match (and ``\S`` matched) whitespace such as ``"\x0b"`` and ``"\x1c"`` in all-ASCII data (:issue:`70991`)
2829

2930
.. ---------------------------------------------------------------------------

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1852,7 +1852,9 @@ def _pad_or_backfill(
18521852
copy: bool = True,
18531853
) -> Self:
18541854
if not self._hasna:
1855-
return self
1855+
if not copy:
1856+
return self
1857+
return self._from_pyarrow_array(self._pa_array)
18561858

18571859
if limit is None and limit_area is None:
18581860
method = missing.clean_fill_method(method)

‎pandas/tests/copy_view/test_interp_fillna.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,33 @@ def test_interp_fill_functions(func):
4444
tm.assert_frame_equal(df, df_orig)
4545

4646

47+
@pytest.mark.parametrize("func", ["ffill", "bfill"])
48+
@pytest.mark.parametrize("kwargs", [{}, {"limit": 1}])
49+
@pytest.mark.parametrize(
50+
"vals, dtype",
51+
[
52+
(["a", "b", "c"], "str"),
53+
(["a", "b", "c"], "string[pyarrow]"),
54+
([1, 2, 3], "int64[pyarrow]"),
55+
],
56+
)
57+
def test_interp_fill_functions_no_na_ea(func, kwargs, vals, dtype, frame_or_series):
58+
# GH#71515
59+
if "pyarrow" in dtype:
60+
pytest.importorskip("pyarrow")
61+
obj = frame_or_series(vals, dtype=dtype)
62+
obj_orig = obj.copy()
63+
64+
result = getattr(obj, func)(**kwargs)
65+
result_orig = result.copy()
66+
result.iloc[0] = vals[1]
67+
tm.assert_equal(obj, obj_orig)
68+
69+
result = getattr(obj, func)(**kwargs)
70+
obj.iloc[0] = vals[1]
71+
tm.assert_equal(result, result_orig)
72+
73+
4774
@pytest.mark.parametrize("func", ["ffill", "bfill"])
4875
@pytest.mark.parametrize(
4976
"vals",

‎pandas/tests/extension/test_arrow.py‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -561,14 +561,6 @@ def test_is_not_string_type(self, dtype):
561561
def test_view(self, data):
562562
super().test_view(data)
563563

564-
def test_fillna_no_op_returns_copy(self, data):
565-
data = data[~data.isna()]
566-
567-
valid = data[0]
568-
result = data.fillna(valid)
569-
assert result is not data
570-
tm.assert_extension_array_equal(result, data)
571-
572564
@pytest.mark.xfail(
573565
reason="GH 45419: pyarrow.ChunkedArray does not support views", run=False
574566
)

‎pandas/tests/extension/test_string.py‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,6 @@ def test_dropna_array(self, data_missing):
175175
expected = data_missing[[1]]
176176
tm.assert_extension_array_equal(result, expected)
177177

178-
def test_fillna_no_op_returns_copy(self, data):
179-
data = data[~data.isna()]
180-
181-
valid = data[0]
182-
result = data.fillna(valid)
183-
assert result is not data
184-
tm.assert_extension_array_equal(result, data)
185-
186178
def _get_expected_exception(
187179
self, op_name: str, obj, other
188180
) -> type[Exception] | tuple[type[Exception], ...] | None:

0 commit comments

Comments
 (0)