1

I was answering a question on StackOverflow, when I encountered the following problem with pandas 1.0.1

import pandas as pd
import numpy as np
df=  pd.DataFrame(np.random.random(10), range(10), columns=['foo'])

print(df)
print('\nInitial Index\n')
print(df.index)

print('\nSet DataFrame.index.values\n')
df.index.values[1::2] = 0
print(df.index.values)
print('\nBut df.index does not change\n')
print(df.index)
print(df)
print('\nSetting Index : df.index = df.index.values \n')

df.index = df.index.values
print('\nIndex changed\n')
print(df)

        foo
0  0.213399
1  0.369334
2  0.924745
3  0.778120
4  0.594977
5  0.916565
6  0.603158
7  0.703187
8  0.462739
9  0.728851

Initial Index

RangeIndex(start=0, stop=10, step=1)

Set DataFrame.index.values

[0 0 2 0 4 0 6 0 8 0]

But df.index does not change

RangeIndex(start=0, stop=10, step=1)
        foo
0  0.213399
1  0.369334
2  0.924745
3  0.778120
4  0.594977
5  0.916565
6  0.603158
7  0.703187
8  0.462739
9  0.728851

Setting Index : df.index = df.index.values


Index changed

        foo
0  0.213399
0  0.369334
2  0.924745
0  0.778120
4  0.594977
0  0.916565
6  0.603158
0  0.703187
8  0.462739
0  0.728851

it seems that we can change the values ​​attribute from outside the df.index object but this does not change the index. Is this really thought like this?

shouldn't the .setter be removed from the property if this doesn't really change the index?

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Index.values.html

ansev
  • 30,322
  • 5
  • 17
  • 31

1 Answers1

2

For index replace we have the rename function for it, also it dose not support assignment from its values

df.rename(index=dict.fromkeys(df.index[1::2],0))
Out[162]: 
        foo
0  0.166692
0  0.889263
2  0.509025
0  0.688095
4  0.995862
0  0.258366
6  0.080054
0  0.406233
8  0.296096
0  0.680760

The problem here is for RangeIndex

    if isinstance(data, RangeIndex):
        return RangeIndex(start=data, copy=copy, dtype=dtype, name=name)

If we change the range index to other type like below, it will work

df.index=[100,0,1,2,3,4,5,6,7,8]
df.index.values[1::2] = 0 
df
Out[177]: 
          foo
100  0.166692
0    0.889263
1    0.509025
0    0.688095
3    0.995862
0    0.258366
5    0.080054
0    0.406233
7    0.296096
0    0.680760
BENY
  • 317,841
  • 20
  • 164
  • 234
  • yes but really the question is why it is allowed to change `df.index.values` ​​if this doesn't really change the index, that is, should `df.index.values ​​[1 :: 2] = 0` return an error? – ansev Aug 02 '20 at 21:03
  • 1
    @ansev it is happen to range index ~ for other type index should work ~ – BENY Aug 02 '20 at 21:16