python - pandas DataFrame.replace 函数因日期时间而损坏

标签 python python-3.x pandas

在 pandas v0.17.1 (anaconda python v3.4.3) 中,datetime 的替换函数被破坏了。

我正在尝试用新值替换 DataFrame 中的字符串值。此 DataFrame 包含多个列(包括数据时间列)。
replace功能失败

>>> from datetime import datetime
>>> import pandas as pd
>>> df = pd.DataFrame({'no':range(4), 'nm':list('abcd'), 'tm':datetime.now()})
>>> df.replace('a', 'A')

Traceback (most recent call last): File "/home/xxx/anaconda/envs/py3/lib/python3.4/site-packages/pandas/core/internals.py", line 2061, in _try_coerce_args other = other.astype('i8',copy=False).view('i8') ValueError: invalid literal for int() with base 10: 'a'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/xxx/anaconda/envs/py3/lib/python3.4/site-packages/pandas/core/internals.py", line 594, in replace values, _, to_replace, _ = self._try_coerce_args(self.values, to_replace) File "/home/xxx/anaconda/envs/py3/lib/python3.4/site-packages/pandas/core/internals.py", line 2066, in _try_coerce_args raise TypeError TypeError

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in File "/home/xxx/anaconda/envs/py3/lib/python3.4/site-packages/pandas/core/generic.py", line 3110, in replace inplace=inplace, regex=regex) File "/home/xxx/anaconda/envs/py3/lib/python3.4/site-packages/pandas/core/internals.py", line 2870, in replace return self.apply('replace', **kwargs) File "/home/xxx/anaconda/envs/py3/lib/python3.4/site-packages/pandas/core/internals.py", line 2823, in apply applied = getattr(b, f)(**kwargs) File "/home/xxx/anaconda/envs/py3/lib/python3.4/site-packages/pandas/core/internals.py", line 607, in replace if not mask.any(): UnboundLocalError: local variable 'mask' referenced before assignment

同样的代码在 pandas 版本 0.16.2 上运行良好。
这是已确认的错误吗?

最佳答案

如评论所述,这已在 master 中修复并将包含在 0.18 中(即将于 2016 年 1 月推出):https://github.com/pydata/pandas/issues/11868 , 并且只出现在 0.17.1 中。


作为解决方法(假设您没有重复命名的列),Series 替换在 0.17.1 中仍然可以正常工作:

for c in df.select_dtypes(include=["object"]).columns:
    df[c] = df[c].replace('a', 'A')

关于python - pandas DataFrame.replace 函数因日期时间而损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34368887/

相关文章:

python - 优化 python、numpy 中的矩阵运算

python-3.x - 读取文件时删除子进程缓冲区

python - 对列表进行排序,其中某些值保持在固定位置

python - 按列分组并找到每组的最小值和最大值

python - 无法制作所需的 pandas 数据框

python - 在 OpenCV python 中打开未压缩的 TIFF 文件

python - pandas 中index.name 和index.names 的区别

python - Stop Piston 的错误捕获

python - Python 3 的 Pickle 错误

python - 如何替换 Pandas.DataFrame 上的整个列