Skip to content

Commit

Permalink
python: fix already mutable borrowed append (#3943)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jul 8, 2022
1 parent 05aacf9 commit 0377c80
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
14 changes: 10 additions & 4 deletions py-polars/polars/internals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,10 +1301,16 @@ def append(self, other: Series, append_chunks: bool = True) -> None:
]
"""
if append_chunks:
self._s.append(other._s)
else:
self._s.extend(other._s)
try:
if append_chunks:
self._s.append(other._s)
else:
self._s.extend(other._s)
except RuntimeError as e:
if str(e) == "Already mutably borrowed":
return self.append(other.clone(), append_chunks)
else:
raise e

def filter(self, predicate: Series | list) -> Series:
"""
Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1631,3 +1631,9 @@ def test_reverse() -> None:

s = pl.Series("values", ["a", "b", None, "y", "x"])
assert s.reverse().to_list() == ["x", "y", None, "b", "a"]


def test_mutable_borrowed_append_3915() -> None:
s = pl.Series("s", [1, 2, 3])
s.append(s)
assert s.to_list() == [1, 2, 3, 1, 2, 3]

0 comments on commit 0377c80

Please sign in to comment.