“总线错误:10";尝试设置 pandas 列名称时 [英] "Bus error: 10" when trying to set pandas column name
问题描述
这是我正在尝试通过索引重命名熊猫列的代码:
Here is the code I'm executing trying to rename pandas column by index:
import pandas as pd
df = pd.read_csv('input.csv', dtype='unicode', delim_whitespace=True)
df.columns.values[2] = "id"
print(df)
我很确定这不是最好的方法,但是当我使用Python 3.5运行它时,我得到了:
I'm pretty sure this is not the best approach, but when I run this with Python 3.5 I get:
$ python3.5 test.py
Bus error: 10
这是我第一次看到这样的错误.没有回溯,只有这个输出字符串.
This is the first time I see an error like this. There is no traceback, just this output string.
Bus error: 10
是什么意思?
这是input.csv
的内容:
visitIp userId idSite
128.227.50.161 a 35
24.222.206.154 a 35
10.12.0.1 a 35
10.12.0.1 a 35
10.12.0.1 a 35
24.222.206.154 a 35
(使用pandas
0.17.1)
(Using pandas
0.17.1)
推荐答案
公共汽车错误发生于以下情况处理器无法访问无效的内存地址.
Bus error occurs when a processor can't access an invalid memory address.
df.columns
是实例.任何更改它的操作实际上都会返回一个新对象.修改其元素是非法的,例如df.columns[2] = 'id'
会引发异常.
df.columns
is an instance if Index
which is an immutable object in pandas. Any operation changing it, returns in fact a new object. Modifying its elements is illegal, for example df.columns[2] = 'id'
would raise an exception.
您正在访问和修改索引的基础数据.实际上,不是直接数据,而是数据的numpy view
,它可能是一个临时对象. (内部,Index.values
是一个返回self._data.view(ndarray)
的属性.)
You were accessing and modifying an underlying data of the index. Actually, not the data directly but a numpy view
of the data, which could have been a temporary object. (Internally, Index.values
is a property that returns self._data.view(ndarray)
.)
我也无法重现此行为,我也不知道到底发生了什么以及现在为什么起作用.在numpy C/cython代码中,这很可能是未定义的行为.
I couldn't reproduce this behaviour either and I don't know exactly what happened and why it now works. It can very well be an undefined behaviour in numpy C/cython code.
这篇关于“总线错误:10";尝试设置 pandas 列名称时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!