Skip to content

Commit abaaf88

Browse files
phoflmeeseeksmachine
authored andcommitted
Backport PR pandas-dev#51974: BUG: astype_view check raising on minimum versions build
1 parent ee7e30c commit abaaf88

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pandas/core/dtypes/astype.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ def astype_is_view(dtype: DtypeObj, new_dtype: DtypeObj) -> bool:
263263
-------
264264
True if new data is a view or not guaranteed to be a copy, False otherwise
265265
"""
266+
if isinstance(dtype, np.dtype) and not isinstance(new_dtype, np.dtype):
267+
new_dtype, dtype = dtype, new_dtype
268+
266269
if dtype == new_dtype:
267270
return True
268271

@@ -290,7 +293,7 @@ def astype_is_view(dtype: DtypeObj, new_dtype: DtypeObj) -> bool:
290293
numpy_dtype = dtype
291294

292295
if new_numpy_dtype is None and isinstance(new_dtype, np.dtype):
293-
numpy_dtype = new_dtype
296+
new_numpy_dtype = new_dtype
294297

295298
if numpy_dtype is not None and new_numpy_dtype is not None:
296299
# if both have NumPy dtype or one of them is a numpy dtype

pandas/tests/copy_view/test_astype.py

+10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import pytest
33

44
from pandas.compat import pa_version_under7p0
5+
import pandas.util._test_decorators as td
56

7+
import pandas as pd
68
from pandas import (
79
DataFrame,
810
Series,
@@ -84,6 +86,14 @@ def test_astype_different_target_dtype(using_copy_on_write, dtype):
8486
tm.assert_frame_equal(df2, df_orig.astype(dtype))
8587

8688

89+
@td.skip_array_manager_invalid_test
90+
def test_astype_numpy_to_ea():
91+
ser = Series([1, 2, 3])
92+
with pd.option_context("mode.copy_on_write", True):
93+
result = ser.astype("Int64")
94+
assert np.shares_memory(get_array(ser), get_array(result))
95+
96+
8797
@pytest.mark.parametrize(
8898
"dtype, new_dtype", [("object", "string"), ("string", "object")]
8999
)

0 commit comments

Comments
 (0)