比较Numpy / Pandas数组与混合元素(字符串和浮点数)

时间:2018-05-27 17:33:06

标签: arrays pandas numpy comparison

我想要比较一些Numpy数组(或等效的Pandas数据帧,因为它可以很容易地转换为)。这些数组/数据帧包含数字和字符串。

对于纯数字,我可以执行以下操作。

import numpy as np
a = np.array([[1.0, 2.0], [1.00001, 2.00001]])
b = np.array([[1.000001, 2.00001], [1.00001, 2.00001]])
print(np.allclose(a, b, 1e-9))
# output: False
print(np.allclose(a, b, 1e-4))
# output: True

使用以下混合数组,我收到错误。

c = np.array([[1.0, "Cat"], [1.00001, 2.00001]])
d = np.array([[1.000001, "Dog"], [1.00001, 2.00001]])
e = np.array([[1.000001, "Cat"], [1.00001, 2.00001]])
print(np.allclose(c, d, 1e-4))
# expected output: False on account of the string difference
print(np.allclose(c, e, 1e-4))
# expected output: True

我尝试将其转换为Pandas数据框,希望内置测试模块可以解决问题。

import pandas as pd
from pandas.util import testing as pdtest
df_c = pd.DataFrame(c)
df_d = pd.DataFrame(d)
df_e = pd.DataFrame(e)
print(pdtest.assert_almost_equal(df_c, df_e, check_exact=False, check_less_precise=4))
# expected output: True as the strings match and numbers agree within tolerance.

但这不起作用。有没有办法比较数字元素与指定容差进行比较的数组,而字符串元素是否完全比较?

编辑:公差纯粹适用于浮动元素。对于字符串,需要完全匹配。

1 个答案:

答案 0 :(得分:1)

def myequal(i,j):
    # scalar comparison function of your own design
    if isinstance(i,str):
        return i==j
    else:
        return 1e04>abs(i-j)

示例数组,作为对象dtype:

In [74]: c = np.array([[1.0, "Cat"], [1.00001, 2.00001]],object)
    ...: d = np.array([[1.000001, "Dog"], [1.00001, 2.00001]],object)
    ...: e = np.array([[1.000001, "Cat"], [1.00001, 2.00001]],object)

In [75]: c
Out[75]: 
array([[1.0, 'Cat'],
       [1.00001, 2.00001]], dtype=object)
In [76]: d
Out[76]: 
array([[1.000001, 'Dog'],
       [1.00001, 2.00001]], dtype=object)
In [77]: e
Out[77]: 
array([[1.000001, 'Cat'],
       [1.00001, 2.00001]], dtype=object)

使用frompyfuncmyequal应用于数组元素。基本上它负责广播迭代

In [78]: f = np.frompyfunc(myequal,2,1)
In [79]: f(c,d)
Out[79]: 
array([[True, False],
       [True, True]], dtype=object)
In [80]: f(c,e)
Out[80]: 
array([[True, True],
       [True, True]], dtype=object)

没有object dtype,您的数组是字符串dtype,唯一常见的dtype:

In [81]: np.array([[1.0, "Cat"], [1.00001, 2.00001]])
Out[81]: 
array([['1.0', 'Cat'],
       ['1.00001', '2.00001']], dtype='<U32')

这会在allclose/isclose中引发错误,因为字符串无法针对np.inf进行测试:

In [82]: np.isclose(_,_)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-82-c2e4de5fe672> in <module>()
----> 1 np.isclose(_,_)

/usr/local/lib/python3.6/dist-packages/numpy/core/numeric.py in isclose(a, b, rtol, atol, equal_nan)
   2330     y = array(y, dtype=dt, copy=False, subok=True)
   2331 
-> 2332     xfin = isfinite(x)
   2333     yfin = isfinite(y)
   2334     if all(xfin) and all(yfin):

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

np.isfinite适用于数字数组,而不适用于字符串数组。