系列.fillna多索引数据帧中的()未填充;这是一个错误吗?

2024-09-30 22:26:43 发布

您现在位置:Python中文网/ 问答频道 /正文

对于我来说,下面的代码片段将NaN值保留为NaN:

import pandas
a = [12, 23]
b = [123, None]
c = [1234, 2345]
d = [12345, 23456]
tuples = [('eyes', 'left'), ('eyes', 'right'), ('ears', 'left'), ('ears', 'right')]
events = {('eyes', 'left'): a, ('eyes', 'right'): b, ('ears', 'left'): c, ('ears', 'right'): d}
multiind = pandas.MultiIndex.from_tuples(tuples, names=['part', 'side'])
zed = pandas.DataFrame(events, index=['a', 'b'], columns=multiind)
zed['eyes']['right'].fillna(value=555, inplace=True)

我得到:

^{pr2}$

如果我在inplace设置为False的情况下运行此函数,则返回的序列已将NaN替换为555。我可以使用这种变通方法,但一方面,如果是一个bug,我想报告它;另一方面,即使是变通方法也不适用于我的实际应用程序。在

所以问题是我是否误解了fillna()还是这是一个bug。谢谢!在

编辑: 我在opensuse13.1上使用pandas 0.12.0、numpy 1.8.0和python2.7.5。在


Tags: 代码rightpandasnaneventsleftbugeyes
1条回答
网友
1楼 · 发布于 2024-09-30 22:26:43

^{}的意思是用其他东西替换{}值,而不是将NaN插入空数据槽中。有关详细信息,请参见this example

In [23]: df2

        one       two     three four   five           timestamp
a       NaN  1.138469 -2.400634  bar   True                 NaT
c       NaN  0.025653 -1.386071  bar  False                 NaT
e  0.863937  0.252462  1.500571  bar   True 2012-01-01 00:00:00
f  1.053202 -2.338595 -0.374279  bar   True 2012-01-01 00:00:00
h       NaN -1.157886 -0.551865  bar  False                 NaT
[5 rows x 6 columns]

In [24]: df2.fillna(0)

        one       two     three four   five           timestamp
a  0.000000  1.138469 -2.400634  bar   True 1970-01-01 00:00:00
c  0.000000  0.025653 -1.386071  bar  False 1970-01-01 00:00:00
e  0.863937  0.252462  1.500571  bar   True 2012-01-01 00:00:00
f  1.053202 -2.338595 -0.374279  bar   True 2012-01-01 00:00:00
h  0.000000 -1.157886 -0.551865  bar  False 1970-01-01 00:00:00
[5 rows x 6 columns]

注意NaT不是时间。在

相关问题 更多 >