Skip to content

Commit e19d7cf

Browse files
committed
BUG: astype_view check raising on minimum versions build
1 parent fb754d7 commit e19d7cf

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-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

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from pandas.compat import pa_version_under7p0
55

6+
import pandas as pd
67
from pandas import (
78
DataFrame,
89
Series,
@@ -84,6 +85,13 @@ def test_astype_different_target_dtype(using_copy_on_write, dtype):
8485
tm.assert_frame_equal(df2, df_orig.astype(dtype))
8586

8687

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

0 commit comments

Comments
 (0)