2013-03-11 71 views
1

我遇到一个奇怪的问题(或故意的吗?),其中combine_firstupdate是造成存储为bool被upcasted值到float64■如果提供的参数不提供布尔列。大熊猫数据框中combine_first和更新方法有奇怪的行为

工作流程示例中的IPython:

In [144]: test = pd.DataFrame([[1,2,False,True],[4,5,True,False]], columns=['a','b','isBool', 'isBool2']) 

In [145]: test 
Out[145]: 
    a b isBool isBool2 
0 1 2 False True 
1 4 5 True False 


In [147]: b = pd.DataFrame([[45,45]], index=[0], columns=['a','b']) 

In [148]: b 
Out[148]: 
    a b 
0 45 45 

In [149]: test.update(b) 

In [150]: test 
Out[150]: 
    a b isBool isBool2 
0 45 45  0  1 
1 4 5  1  0 

当时这意味着是对update函数的行为?我会认为,如果没有指定update不会与其他列混乱。


编辑:我身边开始多一点修修补补。情节变浓了。如果我在运行test.update(b)之前再插入一个命令:test.update([]),则布尔行为的工作原理是以objects的数字为代价。这也适用于DSM的简化示例。

基于panda's source code,它看起来像reindex_like方法是创建D型object的数据帧,而reindex_like b创建D型float64的数据帧。由于object更一般,随后的操作与布尔工作。不幸的是,在数字列上运行np.log将失败,并显示AttributeError

+0

简单的例子:'DF = pd.DataFrame([真,假] ); df.update({})'。 – DSM 2013-03-12 02:58:05

回答

1

这个这个问题是一个bug,更新不应该接触未指定的列,这里固定https://github.com/pydata/pandas/pull/3021

+0

希望它会合并回主。谢谢! – Reservedegotist 2013-03-13 00:31:03

+0

这已被合并在一个尝试 – Jeff 2013-03-13 11:19:21

+0

看起来不错,我认为 - 但是,我也看到这种意想不到的行为在其他功能。我在方法'combine_first'和'groupby.first/last'中看到它。 – Reservedegotist 2013-03-13 13:31:14

1

更新之前,dateframe b是被填充由reindex_link,所以将b变得

In [5]: b.reindex_like(a) 
Out[5]: 
    a b isBool isBool2 
0 45 45  NaN  NaN 
1 NaN NaN  NaN  NaN 

然后用numpy.where来更新数据帧。

悲剧是对于numpy.where,如果两个数据有不同的类型,则会使用更普通的数据。例如

In [20]: np.where(True, [True], [0]) 
Out[20]: array([1]) 

In [21]: np.where(True, [True], [1.0]) 
Out[21]: array([ 1.]) 

由于NaNnumpy是浮动类型,它也将返回一个浮点类型。

In [22]: np.where(True, [True], [np.nan]) 
Out[22]: array([ 1.]) 

因此,更新后,'isBool'和'isBool2'列变为浮动类型。

我已经添加上the issue tracker for pandas

+0

有道理 - 我认为那里根本就没有防范措施,以防止其他列的意外编辑 – Reservedegotist 2013-03-13 00:31:39

相关问题