Arrays 使用混合元素(字符串和浮点)比较Numpy/Pandas数组
我有一些Numpy数组(或者相当于数据帧,因为它可以很容易地转换成)希望进行比较。这些数组/数据帧包含数字和字符串 对于纯数字,我可以做以下操作Arrays 使用混合元素(字符串和浮点)比较Numpy/Pandas数组,arrays,pandas,numpy,comparison,Arrays,Pandas,Numpy,Comparison,我有一些Numpy数组(或者相当于数据帧,因为它可以很容易地转换成)希望进行比较。这些数组/数据帧包含数字和字符串 对于纯数字,我可以做以下操作 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
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.
但这不起作用。是否有一种方法可以比较数组,其中数字元素与指定公差进行比较,而字符串元素则进行精确比较
编辑:公差仅适用于浮动图元。对于字符串,需要精确匹配
def myequal(i,j):
# scalar comparison function of your own design
if isinstance(i,str):
return i==j
else:
return 1e04>abs(i-j)
示例数组,如对象数据类型:
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)
使用frompyfunc
将myequal
应用于to数组的元素。基本上,它负责广播迭代
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')
np.isfinite
适用于数字数组,而不是字符串数组。查看c
或d
(即打印
)。请注意dtype
。我们可能应该关闭此操作,因为您未能描述allclose
错误,和/或尝试任何后续操作。但最基本的问题是allclose
,它使用的是isclose
,它设计用于数字数组,而不是字符串数组。核心测试是abs(x-y)
是否足够小。这不适用于具有字符串数据类型的数组。
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''