Skip to content

BUG: CoW and np.asarray leads to surprising issues #52823

Open
@bashtage

Description

@bashtage

Reproducible Example

import pandas as pd
import numpy as np

x = pd.Series([1,2,3.0])
y = np.asarray(x)
y[1:] = np.nan

pd.options.mode.copy_on_write = True
w = pd.Series([1,2,3.0])
z = np.asarray(w)
z[1:] = np.nan

Issue Description

This probably isn't a bug but it makes interaction outside of pandas considerably harder.

It seems that it would be good to somewhere have a formal way too ensure that casts to numpy arrays are always writable. Perhaps a writeable keyword in to_numpy?

Expected Behavior

Maybe leave the CoW behavior behind when moving outside of pandas.

Installed Versions

INSTALLED VERSIONS

commit : 478d340
python : 3.11.2.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22621
machine : AMD64
processor : AMD64 Family 23 Model 113 Stepping 0, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.0.0
numpy : 1.23.5
pytz : 2023.3
dateutil : 2.8.2
setuptools : 65.6.3
pip : 23.0.1
Cython : 0.29.33
pytest : 7.0.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.12.0
pandas_datareader: None
bs4 : None
bottleneck : None
brotli :
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.0.dev0+1817.98c51a6
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions