pandas dataframe.where行为不端

jwi*_*ner 2 python numpy pandas

我正在尝试实现一个函数,该函数在数据帧或系列的每个位置返回最大值,从而最小化NaN.

In [217]: a
Out[217]: 
   0  1
0  4  1
1  6  0

[2 rows x 2 columns]

In [218]: b
Out[218]: 
    0   1
0 NaN   3
1   3 NaN

[2 rows x 2 columns]


In [219]: do_not_replace = b.isnull() | (a > b)

In [220]: do_not_replace
Out[220]: 
      0      1
0  True  False
1  True   True

[2 rows x 2 columns]


In [221]: a.where(do_not_replace, b)
Out[221]: 
   0  1
0  4  3
1  1  0

[2 rows x 2 columns]


In [222]: expected
Out[222]: 
   0  1
0  4  3
1  6  0

[2 rows x 2 columns]

In [223]: pd.__version__
Out[223]: '0.13.1'
Run Code Online (Sandbox Code Playgroud)

我想还有其他方法来实现这个功能,但我无法弄清楚这种行为.我的意思是,1来自哪里?我认为逻辑是合理的.我是否误解了该功能的工作原理?

Jef*_*eff 5

这基本上是where内部的.我认为这可能是一个转换错误.Bug修复了这里.结果是一个对称的DataFrame和一个需要重现的传递帧.非常微妙.请注意,这种其他形式的索引(下面)使用了一个不同的方法,因为它没问题.

In [56]: a[~do_not_replace] = b

In [57]: a
Out[57]: 
   0  1
0  4  3
1  6  0
Run Code Online (Sandbox Code Playgroud)

注意:这已在master/0.14.1中修复.