Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
idx1 = pd.RangeIndex(6)
idx2 = pd.Index([0,1,2,3,4,5])
idx1_np = idx1.to_numpy()
idx2_np = idx2.to_numpy()
idx1_np[0] = 500
idx2_np[0] = 500
print(idx1)
print(idx2)
Issue Description
I created one RangeIndex
and one Int64Index
. Both of these indexes can be converted into numpy.array
by using to_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
Comment From: topper-123
Yeah, this is a bug, thanks for catching. Probably connected to differences between RangeIndex
and Int64Index
implementation of to_numpy
.
If you want a go at fixing this, we welcome contributions.
Comment From: topper-123
I've looked more into this, and the underlying issue is that Index.to_numpy
by default does not copy, but returns the original ndarray underlying the Index. E.g.
>>> 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 to_numpy
to be a reference rather than a copy of the original ._values
. I personally would change the default for the copy
parameter to be True
and give extra warnings in the doc string about the risk when using copy=False
, but that'd be a backward compatibility problem.
@jbrockmendel , do you have an opinion on this?
@Dranikf, I changed your original post to emphazise that this is a Index.to_numpy
issue, not a Series
issue.
Comment From: jreback
the change should be to return a read only array here
Comment From: topper-123
the change should be to return a read only array here
yeah, that’s also reasonable.
Comment From: jbrockmendel
the change should be to return a read only array here
+1
Comment From: phofl
Is this a regression? Otherwise no need to label as 1.5.2