Skip to content

BUG: astype_view check raising on minimum versions build #51974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pandas/core/dtypes/astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ def astype_is_view(dtype: DtypeObj, new_dtype: DtypeObj) -> bool:
-------
True if new data is a view or not guaranteed to be a copy, False otherwise
"""
if isinstance(dtype, np.dtype) and not isinstance(new_dtype, np.dtype):
new_dtype, dtype = dtype, new_dtype

if dtype == new_dtype:
return True

Expand Down Expand Up @@ -290,7 +293,7 @@ def astype_is_view(dtype: DtypeObj, new_dtype: DtypeObj) -> bool:
numpy_dtype = dtype

if new_numpy_dtype is None and isinstance(new_dtype, np.dtype):
numpy_dtype = new_dtype
new_numpy_dtype = new_dtype

if numpy_dtype is not None and new_numpy_dtype is not None:
# if both have NumPy dtype or one of them is a numpy dtype
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/copy_view/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import pytest

from pandas.compat import pa_version_under7p0
import pandas.util._test_decorators as td

import pandas as pd
from pandas import (
DataFrame,
Series,
Expand Down Expand Up @@ -84,6 +86,14 @@ def test_astype_different_target_dtype(using_copy_on_write, dtype):
tm.assert_frame_equal(df2, df_orig.astype(dtype))


@td.skip_array_manager_invalid_test
def test_astype_numpy_to_ea():
ser = Series([1, 2, 3])
with pd.option_context("mode.copy_on_write", True):
result = ser.astype("Int64")
assert np.shares_memory(get_array(ser), get_array(result))


@pytest.mark.parametrize(
"dtype, new_dtype", [("object", "string"), ("string", "object")]
)
Expand Down