获取支持NA/的布尔 pandas 列是可以为空的 [英] Getting boolean pandas column that supports NA/ is nullable

查看:89
本文介绍了获取支持NA/的布尔 pandas 列是可以为空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建带有dtype bool(或int)并支持Nan/missing值的pandas数据框列?

How can I create a pandas dataframe column with dtype bool (or int for that matter) with support for Nan/missing values?

当我这样尝试时:

d = {'one' : np.ma.MaskedArray([True, False, True, True], mask = [0,0,1,0]),
'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
print (df.dtypes)
print (df)

one隐式转换为对象.对于ints同样如此:

column one is implicitly converted to object. Likewise similar for ints:

d = {'one' : np.ma.MaskedArray([1,3,2,1], mask = [0,0,1,0]),
'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
print (df.dtypes)
print (df)

one在这里隐式转换为float64,如果我留在int域中并且不处理具有其特质的浮点算术(在比较时始终具有容忍度,舍入误差等),则我更愿意.

one is here implicitly converted to float64, and I'd prefer if I stayed in int domain and not handle floating point arithmetic with its idiosyncrasies (always have tolerance when comparing, rounding errors, etc.)

推荐答案

pandas> = 1.0

从熊猫1.0.0(到2020年1月)开始,有在此版本中,在整数情况下,熊猫还将使用pd.NA而不是np.nan:

In this version, pandas will also use pd.NA instead of np.nan in the integer case:

In [166]: df.astype('Int64')
Out[166]:
    one  two
a     1    1
b     3    2
c  <NA>    3
d     1    4


熊猫> = 0.24

在整数情况下,自熊猫0.24(2019年1月)开始,您可以使用


pandas >= 0.24

In the integer case, as of pandas 0.24 (January 2019), you can use nullable integers to achieve what you want:

In [165]: df
Out[165]:
   one  two
a  1.0  1.0
b  3.0  2.0
c  NaN  3.0
d  1.0  4.0

In [166]: df.astype('Int64')
Out[166]:
   one  two
a    1    1
b    3    2
c  NaN    3
d    1    4

这可以通过将支持数组转换为此GitHub问题此PyData说话.您可以编写自己的扩展类型来也可以解决这种情况,但是如果您可以将布尔值用整数0和1表示,那么一种方法可能是:

This works by converting the backing array to an arrays.IntegerArray, and there is no equivalent thing for booleans, but some work in that direction is discussed in this GitHub issue and this PyData talk. You could write your own extension type to cover this case as well, but if you can live with your booleans being represented by the integers 0 and 1, one approach could be the following:

In [183]: df.one
Out[183]:
a     True
b    False
c      NaN
d     True
Name: one, dtype: object

In [184]: (df.one * 1).astype('Int64')
Out[184]:
a      1
b      0
c    NaN
d      1
Name: one, dtype: Int64

这篇关于获取支持NA/的布尔 pandas 列是可以为空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆