2013-03-11 73 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

相關問題