Skip to content

Commit 4217baf

Browse files
update test to be explicit about copy vs nocopy + allow copy=False for masked arrays in case of no NAs
1 parent 6799f55 commit 4217baf

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

pandas/core/arrays/masked.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ def to_numpy(
507507
else:
508508
with warnings.catch_warnings():
509509
warnings.filterwarnings("ignore", category=RuntimeWarning)
510-
data = self._data.astype(dtype, copy=copy)
510+
data = np.array(self._data, dtype=dtype, copy=copy)
511511
return data
512512

513513
@doc(ExtensionArray.tolist)
@@ -581,7 +581,7 @@ def __array__(
581581
the array interface, return my values
582582
We return an object array here to preserve our scalar values
583583
"""
584-
if copy is False:
584+
if copy is False and self._hasna:
585585
raise ValueError(
586586
"Unable to avoid copy while creating an array as requested."
587587
)

pandas/tests/base/test_conversion.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -298,24 +298,27 @@ def test_array_multiindex_raises():
298298

299299

300300
@pytest.mark.parametrize(
301-
"arr, expected",
301+
"arr, expected, zero_copy",
302302
[
303-
(np.array([1, 2], dtype=np.int64), np.array([1, 2], dtype=np.int64)),
304-
(pd.Categorical(["a", "b"]), np.array(["a", "b"], dtype=object)),
303+
(np.array([1, 2], dtype=np.int64), np.array([1, 2], dtype=np.int64), True),
304+
(pd.Categorical(["a", "b"]), np.array(["a", "b"], dtype=object), False),
305305
(
306306
pd.core.arrays.period_array(["2000", "2001"], freq="D"),
307307
np.array([pd.Period("2000", freq="D"), pd.Period("2001", freq="D")]),
308+
False,
308309
),
309-
(pd.array([0, np.nan], dtype="Int64"), np.array([0, np.nan])),
310+
(pd.array([0, np.nan], dtype="Int64"), np.array([0, np.nan]), False),
310311
(
311312
IntervalArray.from_breaks([0, 1, 2]),
312313
np.array([pd.Interval(0, 1), pd.Interval(1, 2)], dtype=object),
314+
False,
313315
),
314-
(SparseArray([0, 1]), np.array([0, 1], dtype=np.int64)),
316+
(SparseArray([0, 1]), np.array([0, 1], dtype=np.int64), False),
315317
# tz-naive datetime
316318
(
317319
DatetimeArray._from_sequence(np.array(["2000", "2001"], dtype="M8[ns]")),
318320
np.array(["2000", "2001"], dtype="M8[ns]"),
321+
True,
319322
),
320323
# tz-aware stays tz`-aware
321324
(
@@ -330,6 +333,7 @@ def test_array_multiindex_raises():
330333
Timestamp("2000-01-02", tz="US/Central"),
331334
]
332335
),
336+
False,
333337
),
334338
# Timedelta
335339
(
@@ -338,6 +342,7 @@ def test_array_multiindex_raises():
338342
dtype=np.dtype("m8[ns]"),
339343
),
340344
np.array([0, 3600000000000], dtype="m8[ns]"),
345+
True,
341346
),
342347
# GH#26406 tz is preserved in Categorical[dt64tz]
343348
(
@@ -348,10 +353,11 @@ def test_array_multiindex_raises():
348353
Timestamp("2016-01-02", tz="US/Pacific"),
349354
]
350355
),
356+
False,
351357
),
352358
],
353359
)
354-
def test_to_numpy(arr, expected, index_or_series_or_array, request):
360+
def test_to_numpy(arr, expected, zero_copy, index_or_series_or_array):
355361
box = index_or_series_or_array
356362

357363
with tm.assert_produces_warning(None):
@@ -374,15 +380,16 @@ def test_to_numpy(arr, expected, index_or_series_or_array, request):
374380
# copy=False semantics are only supported in NumPy>=2.
375381
return
376382

377-
try:
378-
result_nocopy1 = np.array(thing, copy=False)
379-
except ValueError:
380-
# An error is always acceptable for `copy=False`
381-
return
383+
if not zero_copy:
384+
with pytest.raises(ValueError, match="Unable to avoid copy while creating"):
385+
# An error is always acceptable for `copy=False`
386+
np.array(thing, copy=False)
382387

383-
result_nocopy2 = np.array(thing, copy=False)
384-
# If copy=False was given, these must share the same data
385-
assert np.may_share_memory(result_nocopy1, result_nocopy2)
388+
else:
389+
result_nocopy1 = np.array(thing, copy=False)
390+
result_nocopy2 = np.array(thing, copy=False)
391+
# If copy=False was given, these must share the same data
392+
assert np.may_share_memory(result_nocopy1, result_nocopy2)
386393

387394

388395
@pytest.mark.xfail(

0 commit comments

Comments
 (0)