2017-10-20 88 views
6

不同我有一個array像下面np.isreal行爲pandas.DataFrame和numpy.array

np.array(["hello","world",{"a":5,"b":6,"c":8},"usa","india",{"d":9,"e":10,"f":11}]) 

pandasDataFrame像下面

df = pd.DataFrame({'A': ["hello","world",{"a":5,"b":6,"c":8},"usa","india",{"d":9,"e":10,"f":11}]}) 

當我申請np.isrealDataFrame

df.applymap(np.isreal) 
Out[811]: 
     A 
0 False 
1 False 
2 True 
3 False 
4 False 
5 True 

當我做np.isrealnumpy陣列。

np.isreal(np.array(["hello","world",{"a":5,"b":6,"c":8},"usa","india",{"d":9,"e":10,"f":11}])) 
Out[813]: array([ True, True, True, True, True, True], dtype=bool) 

我必須用在錯誤的使用情況下,np.isreal,但是你能不能幫我約爲什麼結果不同

+0

對於我來說,這比你給出的觸發這個問題的答案更令人困惑! :)。不僅它爲什麼不同,而且它爲什麼區分'pandas'中的字符串和字典? – roganjosh

+1

@roganjosh我只是有時間來測試它,即使我們以錯誤的方式使用它,我們也期望得到同樣的錯誤答案,但是這一個..LOL – Wen

+1

熊貓在這裏是一個紅鯡魚,它只是使用元素行爲例如([「hello」,「world」,{「a」:5,「b」:6,「c」:8},「usa」,「印度的[np.isreal(aa) 「,{」d「:9,」e「:10,」f「:11}])]' –

回答

6

部分答案是isreal僅用於像數組一樣的第一個參數。

你想用isrealobj每個元素得到bahavior你在這裏看到:

In [11]: a = np.array(["hello","world",{"a":5,"b":6,"c":8},"usa","india",{"d":9,"e":10,"f":11}]) 

In [12]: a 
Out[12]: 
array(['hello', 'world', {'a': 5, 'b': 6, 'c': 8}, 'usa', 'india', 
     {'d': 9, 'e': 10, 'f': 11}], dtype=object) 

In [13]: [np.isrealobj(aa) for aa in a] 
Out[13]: [True, True, True, True, True, True] 

In [14]: np.isreal(a) 
Out[14]: array([ True, True, True, True, True, True], dtype=bool) 

這並沒有解決這一問題,有什麼np.isreal做的東西是不是數組類如

In [21]: np.isrealobj("") 
Out[21]: True 

In [22]: np.isreal("") 
Out[22]: False 

In [23]: np.isrealobj({}) 
Out[23]: True 

In [24]: np.isreal({}) 
Out[24]: True 

原來從.imag這源於因爲test that isreal does是:

return imag(x) == 0 # note imag == np.imag 

,就是這樣。

In [31]: np.imag(a) 
Out[31]: array([0, 0, 0, 0, 0, 0], dtype=object) 

In [32]: np.imag("") 
Out[32]: 
array('', 
     dtype='<U1') 

In [33]: np.imag({}) 
Out[33]: array(0, dtype=object) 

此查找該陣列上的.imag屬性。

In [34]: np.asanyarray("").imag 
Out[34]: 
array('', 
     dtype='<U1') 

In [35]: np.asanyarray({}).imag 
Out[35]: array(0, dtype=object) 

我不知道這是爲什麼沒有在字符串的情況下尚且......

+2

這並不能完全回答這裏的真實情況,但文檔清楚地表明第一個參數應該是數組狀態的(所以我認爲所有的投注都是關閉..),我們必須看代碼 –

+0

謝謝,讓我意識到不是所有'numpy'函數都可以借用,而不用考慮到熊貓計算 – Wen

+0

@Wen它發現它來自'.imag' https的有趣行爲://github.com/numpy/numpy/blob/v1.13.0/numpy/lib/type_check.py#L221-L249 –

4

我認爲這是Numpy的一個小錯誤,說實話。這裏Pandas只是在列中的每個項目上循環並調用np.isreal()就可以了。例如: -

>>> np.isreal("a") 
False 
>>> np.isreal({}) 
True 

我認爲這裏的悖論與的dtype=object如何對待np.real()投入去做。我的猜測是它將對象指針視爲int,因此np.isreal(<some object>)當然返回True。在像np.array(["A", {}])這樣的混合類型的數組上,數組的數量爲dtype=object,因此np.isreal()以與dtype=object一樣的方式處理所有元素(包括字符串)。

爲了清楚起見,我認爲該錯誤是np.isreal()如何處理dtype=object數組中的任意對象,但我沒有明確地證實這一點。

+2

我們都很快,你只是打我:) –

+0

這是一個奇怪的問題,但絕對是一個有趣的問題? – Iguananaut

+1

我是一個陌生人..LOL :-),對我來說 – Wen

1

有幾件事情怎麼回事。首先,前面的答案指出np.isreal在通過ojbects時會出現奇怪的行爲。 但是,我認爲你也對applymap正在做什麼感到困惑。 Difference between map, applymap and apply methods in Pandas始終是一個很好的參考。

在這種情況下,你認爲你正在做的實際上是:

df.apply(np.isreal, axis=1) 

基本上調用np.isreal(DF),而df.applymap(np.isreal)基本上呼籲np.isreal每個df的個別元素。例如

np.isreal(df.A) 

array([ True, True, True, True, True, True], dtype=bool) 

np.array([np.isreal(x) for x in df.A]) 

array([False, False, True, False, False, True], dtype=bool) 
+0

啊,很好。 – Iguananaut

+1

與此前的評論相關https://stackoverflow.com/questions/46856988/np-isreal-behavior-different-in-pandas-dataframe-and-numpy-array/46857114#comment80661655_46856988 –