Skip to content

Commit 5c8c2ec

Browse files
committed
Fix issue pandas-dev#54654
on pickle roundtrip astype(str) might change original array even when copy is True
1 parent 43691a2 commit 5c8c2ec

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pandas/_libs/lib.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ cpdef ndarray[object] ensure_string_array(
775775

776776
result = np.asarray(arr, dtype="object")
777777

778-
if copy and result is arr:
778+
if copy and (result is arr or np.may_share_memory(arr, result)):
779779
result = result.copy()
780780
elif not copy and result is arr:
781781
already_copied = False

pandas/tests/copy_view/test_astype.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pickle
2+
13
import numpy as np
24
import pytest
35

@@ -131,6 +133,15 @@ def test_astype_string_and_object_update_original(
131133
tm.assert_frame_equal(df2, df_orig)
132134

133135

136+
def test_astype_string_copy_on_pickle_roundrip():
137+
# https://github.com/pandas-dev/pandas/issues/54654
138+
# ensure_string_array may alter array inplace
139+
base = Series(np.array([(1, 2), None, 1], dtype="object"))
140+
base_copy = pickle.loads(pickle.dumps(base))
141+
base_copy.astype(str)
142+
tm.assert_series_equal(base, base_copy)
143+
144+
134145
def test_astype_dict_dtypes(using_copy_on_write):
135146
df = DataFrame(
136147
{"a": [1, 2, 3], "b": [4, 5, 6], "c": Series([1.5, 1.5, 1.5], dtype="float64")}

0 commit comments

Comments
 (0)