Description
Code Sample, a copy-pastable example if possible
import pandas as pd
a = pd.DataFrame({"col1": [1, 2, 3]}, index=["a", "b", "c"])
b = a.copy()
b.index.values[0] = "newval"
assert a.index[0] == "newval"
Problem description
Index
objects are assumed to be immutable, but wrap a mutable object. In my experience, it's pretty common to just grab the .values
from pandas object. This has caused surprising behaviour in my own code, and has been discussed here before #19862.
I would like to make sure that the values numpy array for index objects has it'sWRITABLE
flag set to false, to make Index
object more immutable.
Does this sound reasonable? If so, I can give a PR a shot.
Expected Output
# This should have thrown an error
b.index.values[0] = "newval"
Output of pd.show_versions()
[paste the output of pd.show_versions()
here below this line]
INSTALLED VERSIONS
commit : None
python : 3.7.6.final.0
python-bits : 64
OS : Darwin
OS-release : 19.3.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.0.3
numpy : 1.18.2
pytz : 2018.9
dateutil : 2.7.5
pip : 20.0.2
setuptools : 46.0.0
Cython : 0.29.14
pytest : 5.3.5
hypothesis : 5.6.0
sphinx : 2.4.4
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.4.1
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.10.1
IPython : 7.13.0
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
fastparquet : 0.3.3
gcsfs : None
lxml.etree : 4.4.1
matplotlib : 3.2.0
numexpr : 2.7.1
odfpy : None
openpyxl : 2.6.2
pandas_gbq : None
pyarrow : 0.16.0
pytables : None
pytest : 5.3.5
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : 3.6.1
tabulate : 0.8.3
xarray : 0.15.0
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
numba : 0.48.0