-
-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
BUG: Different behavior for different index types #49663
Comments
Yeah, this is a bug, thanks for catching. Probably connected to differences between If you want a go at fixing this, we welcome contributions. |
I've looked more into this, and the underlying issue is that >>> import pandas as pd
>>> idx = pd.RangeIndex(3)
>>> arr = idx.to_numpy()
>>> arr[0] = 3
>>> arr
array([3, 1, 2], dtype=int64)
>>> idx
RangeIndex(start=0, stop=3, step=1) # seemingly unchanged, but...
>>> idx._values
array([3, 1, 2], dtype=int64) # underlying array changed!
>>> arr is idx._values
True
>>> idx.is_unique
True # this is not true for `idx._values` So the issue exists in both your examples, it's just hidden in the first one. It seems the current behavior was added intentionally, as there are tests for this that require the returned array from @jbrockmendel , do you have an opinion on this? @Dranikf, I changed your original post to emphazise that this is a |
the change should be to return a read only array here |
yeah, that’s also reasonable. |
+1 |
Is this a regression? Otherwise no need to label as 1.5.2 |
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
I created one
RangeIndex
and oneInt64Index
. Both of these indexes can be converted intonumpy.array
by usingto_numpy
function. That's what I do in the example.But if I change the object that returned the index set explicitly it is reflected in the original index, but for the default index this does not happen. I think the behaviour should be the same regardless of the type of index.
Expected Behavior
RangeIndex(start=0, stop=5, step=1)
Int64Index([0, 1, 2, 3, 4, 5], dtype='int64')
Installed Versions
INSTALLED VERSIONS
commit : 91111fd
python : 3.10.7.final.0
python-bits : 64
OS : Linux
OS-release : 5.19.16-200.fc36.x86_64
Version : #1 SMP PREEMPT_DYNAMIC Sun Oct 16 22:50:04 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : ru_RU.UTF-8
LOCALE : ru_RU.UTF-8
pandas : 1.5.1
numpy : 1.23.4
pytz : 2021.3
dateutil : 2.8.1
setuptools : 59.6.0
pip : 22.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.0.2
lxml.etree : 4.7.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.0.1
pandas_datareader: None
bs4 : 4.11.0
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.7.3
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : None
zstandard : None
tzdata : None
The text was updated successfully, but these errors were encountered: