关于 pandas 数据帧索引的不变性
Regarding the immutability of pandas dataframe indexes
我在文档中读到索引对象是不可变的,一旦创建就不能更改。但是我可以在创建后更改这些值。
我是不是漏掉了什么?
这是我试过的:
ser = pd.Series([5,0,3,8,4], index=['red','blue','yellow','white','green'])
ser
red 5
blue 0
yellow 3
white 8
green 4
dtype: int64
ser.index = ['red','blue','yellow','white','test']
ser
red 5
blue 0
yellow 3
white 8
test 4
dtype: int64
您可以像您所做的那样更改对象ser.index
引用,但不能改变对象一旦分配:
>>> import pandas as pd
>>> ser = pd.Series([5,0,3,8,4], index=['red','blue','yellow','white','green'])
>>> ser.index[2] = 'coconut'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/indexes/base.py", line 1404, in __setitem__
raise TypeError("Index does not support mutable operations")
TypeError: Index does not support mutable operations
>>> lst = [''] * 5
>>> ser.index = lst
>>> ser.index
Index(['', '', '', '', ''], dtype='object')
>>> lst[:] = ['Mango', 'Banana', 'Orange', 'Pear', 'Apple'] # update list
>>> lst
['Mango', 'Banana', 'Orange', 'Pear', 'Apple']
>>> ser.index
Index(['', '', '', '', ''], dtype='object')
请注意,罪魁祸首在 Index
class 的 __setitem__
方法中:
def __setitem__(self, key, value):
raise TypeError("Index does not support mutable operations")
当您尝试设置索引的元素时会引发此 TypeError
。但是,这并没有说明重新分配索引。
OTOH,如果你考虑 df.set_index
这是一种设置索引的方法,你会看到,最后,这是完成的:
frame.index = index # Line 3016
也就是说,您可以随时重新分配索引。
一个类似的例子应该有所帮助。假设您知道字符串的不变性。
string = 'test'
这个是可以的:
string = 'test2' # reassignment
但是这个不是:
string[0] = c # item assignment... mutating the same object!
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-233-27903bb729b1> in <module>()
----> 1 s[0] = c
TypeError: 'str' object does not support item assignment
以类似的方式可变性!=重新分配。 ser.index
与此类似。您可能会将 index
视为有序的冻结集。
我在文档中读到索引对象是不可变的,一旦创建就不能更改。但是我可以在创建后更改这些值。
我是不是漏掉了什么?
这是我试过的:
ser = pd.Series([5,0,3,8,4], index=['red','blue','yellow','white','green'])
ser
red 5
blue 0
yellow 3
white 8
green 4
dtype: int64
ser.index = ['red','blue','yellow','white','test']
ser
red 5
blue 0
yellow 3
white 8
test 4
dtype: int64
您可以像您所做的那样更改对象ser.index
引用,但不能改变对象一旦分配:
>>> import pandas as pd
>>> ser = pd.Series([5,0,3,8,4], index=['red','blue','yellow','white','green'])
>>> ser.index[2] = 'coconut'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/indexes/base.py", line 1404, in __setitem__
raise TypeError("Index does not support mutable operations")
TypeError: Index does not support mutable operations
>>> lst = [''] * 5
>>> ser.index = lst
>>> ser.index
Index(['', '', '', '', ''], dtype='object')
>>> lst[:] = ['Mango', 'Banana', 'Orange', 'Pear', 'Apple'] # update list
>>> lst
['Mango', 'Banana', 'Orange', 'Pear', 'Apple']
>>> ser.index
Index(['', '', '', '', ''], dtype='object')
请注意,罪魁祸首在 Index
class 的 __setitem__
方法中:
def __setitem__(self, key, value):
raise TypeError("Index does not support mutable operations")
当您尝试设置索引的元素时会引发此 TypeError
。但是,这并没有说明重新分配索引。
OTOH,如果你考虑 df.set_index
这是一种设置索引的方法,你会看到,最后,这是完成的:
frame.index = index # Line 3016
也就是说,您可以随时重新分配索引。
一个类似的例子应该有所帮助。假设您知道字符串的不变性。
string = 'test'
这个是可以的:
string = 'test2' # reassignment
但是这个不是:
string[0] = c # item assignment... mutating the same object!
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-233-27903bb729b1> in <module>()
----> 1 s[0] = c
TypeError: 'str' object does not support item assignment
以类似的方式可变性!=重新分配。 ser.index
与此类似。您可能会将 index
视为有序的冻结集。