Skip to content

Commit

Permalink
Backport PR #35633: BUG: DataFrame.apply with func altering row in-pl…
Browse files Browse the repository at this point in the history
…ace (#35666)

Co-authored-by: jbrockmendel <jbrockmendel@gmail.com>
  • Loading branch information
meeseeksmachine and jbrockmendel committed Aug 11, 2020
1 parent 7957254 commit ac40043
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Fixed regressions
- Fixed regression in :class:`pandas.core.groupby.RollingGroupby` where column selection was ignored (:issue:`35486`)
- Fixed regression in :meth:`DataFrame.shift` with ``axis=1`` and heterogeneous dtypes (:issue:`35488`)
- Fixed regression in ``.groupby(..).rolling(..)`` where a segfault would occur with ``center=True`` and an odd number of values (:issue:`35552`)
- Fixed regression in :meth:`DataFrame.apply` where functions that altered the input in-place only operated on a single row (:issue:`35462`)

.. ---------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,8 @@ def series_generator(self):
blk = mgr.blocks[0]

for (arr, name) in zip(values, self.index):
# GH#35462 re-pin mgr in case setitem changed it
ser._mgr = mgr
blk.values = arr
ser.name = name
yield ser
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/frame/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1522,3 +1522,22 @@ def test_apply_dtype(self, col):
expected = df.dtypes

tm.assert_series_equal(result, expected)


def test_apply_mutating():
# GH#35462 case where applied func pins a new BlockManager to a row
df = pd.DataFrame({"a": range(100), "b": range(100, 200)})

def func(row):
mgr = row._mgr
row.loc["a"] += 1
assert row._mgr is not mgr
return row

expected = df.copy()
expected["a"] += 1

result = df.apply(func, axis=1)

tm.assert_frame_equal(result, expected)
tm.assert_frame_equal(df, result)

0 comments on commit ac40043

Please sign in to comment.