Skip to content

Commit b0276b2

Browse files
update constructor copy test + do not copy in maybe_convert_objects?
1 parent 3b0b779 commit b0276b2

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

pandas/_libs/lib.pyx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,7 @@ cpdef ndarray[object] ensure_string_array(
762762
out = arr.astype(str).astype(object)
763763
out[arr.isna()] = na_value
764764
return out
765+
arr = arr.to_numpy(dtype=object)
765766
elif not util.is_array(arr):
766767
arr = np.array(arr, dtype="object")
767768

@@ -2735,17 +2736,13 @@ def maybe_convert_objects(ndarray[object] objects,
27352736
from pandas.core.arrays.string_ import StringDtype
27362737

27372738
dtype = StringDtype()
2738-
return dtype.construct_array_type()._from_sequence(
2739-
objects, dtype=dtype, copy=True
2740-
)
2739+
return dtype.construct_array_type()._from_sequence(objects, dtype=dtype)
27412740

27422741
elif using_string_dtype() and is_string_array(objects, skipna=True):
27432742
from pandas.core.arrays.string_ import StringDtype
27442743

27452744
dtype = StringDtype(na_value=np.nan)
2746-
return dtype.construct_array_type()._from_sequence(
2747-
objects, dtype=dtype, copy=True
2748-
)
2745+
return dtype.construct_array_type()._from_sequence(objects, dtype=dtype)
27492746

27502747
seen.object_ = True
27512748
elif seen.interval_:

pandas/tests/frame/test_constructors.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,19 +309,28 @@ def test_1d_object_array_does_not_copy(self, using_infer_string):
309309
# no numpy arrays to compare
310310
pass
311311
else:
312-
# TODO(infer_string): this should be fixed to still share memory?
313-
assert not np.shares_memory(df[0].to_numpy(), arr)
312+
assert np.shares_memory(df[0].to_numpy(), arr)
314313
else:
315314
assert np.shares_memory(df.values, arr)
316315

317316
df = DataFrame(arr, dtype=object, copy=False)
318317
assert np.shares_memory(df.values, arr)
319318

320-
@pytest.mark.xfail(using_string_dtype(), reason="conversion copies")
321-
def test_2d_object_array_does_not_copy(self):
319+
def test_2d_object_array_does_not_copy(self, using_infer_string):
322320
# https://github.com/pandas-dev/pandas/issues/39272
323321
arr = np.array([["a", "b"], ["c", "d"]], dtype="object")
324322
df = DataFrame(arr, copy=False)
323+
if using_infer_string:
324+
if df[0].dtype.storage == "pyarrow":
325+
# object dtype strings are converted to arrow memory,
326+
# no numpy arrays to compare
327+
pass
328+
else:
329+
assert np.shares_memory(df[0].to_numpy(), arr)
330+
else:
331+
assert np.shares_memory(df.values, arr)
332+
333+
df = DataFrame(arr, dtype=object, copy=False)
325334
assert np.shares_memory(df.values, arr)
326335

327336
def test_constructor_dtype_list_data(self):

0 commit comments

Comments
 (0)