简体   繁体   English

不同类型的熊猫系列元素,numpy ints,在列表理解期间

[英]Different type of panda Series elements, numpy ints, during list comprehension

I have noticed that in numpy 1.18.4 (and not in previous numpy versions) the element type during list comprehensions is different than accessing element-wise.我注意到在 numpy 1.18.4(而不是在以前的 numpy 版本中)中,列表推导期间的元素类型不同于按元素访问。 For example:例如:

foo = pd.DataFrame(data={'a': np.array([1, 2, 3]), 'b': np.array([1, 0, 1])})
var = {type(x) == type(foo['a'][i]) for i, x in enumerate(foo['a'])}

I get var = {False} .我得到var = {False} What is the reason for this?这是什么原因? Why was it not the case before?为什么以前不是这样?


Ideally I would like to avoid ZeroDivisionError when dividing by zero but instead get the usual 'inf' produced by numpy.int32, when doing:理想情况下,我希望在除以零时避免 ZeroDivisionError,而是在执行以下操作时获得 numpy.int32 产生的通常的“inf”:

[0 if x == 0 and z == 0 else x / y for x, y, z in zip(foo['a'], foo['b'], c)]

for c another array of int32's.对于c另一个 int32 数组。 Is there any way to do this without re transforming the elements to np.int32 inside the list comprehension?有没有办法做到这一点,而无需在列表理解中将元素重新转换为 np.int32 ?

IIUC what you want, you can use to_numpy on the columns from foo . IIUC 你想要什么,你可以在foo的列上使用to_numpy

foo = pd.DataFrame(data={'a':np.array([0,2,3]), 'b': np.array([1,0,1])})
c = np.array([0,1,1])

[0 if x == 0 and z == 0 else x / y 
 for x, y, z in zip(foo['a'].to_numpy(), foo['b'].to_numpy(), c)]
# [0, inf, 3.0]

It works although it raises this RuntimeWarning: divide by zero encountered in long_scalars尽管它引发了这个RuntimeWarning: divide by zero encountered in long_scalars但它仍然有效

Another alternative is to specify a pandas type like pd.Int32Dtype when creating foo:另一种选择是在创建 foo 时指定 pandas 类型,如pd.Int32Dtype

foo = pd.DataFrame(data={'a':np.array([0,2,3]), 'b': np.array([1,0,1])}, 
                   dtype=pd.Int32Dtype())
# or if foo exsit already you use astype with 
# foo = foo.astype(pd.Int32Dtype())

c = np.array([0,1,1])
[0 if x == 0 and z == 0 else x / y for x, y, z in zip(foo['a'], foo['b'], c)]

same result相同的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM