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