可为空的整型数据类型#

备注

整型数组目前还处于实验阶段。其API或实现可能会在没有警告的情况下发生更改。

在 1.0.0 版更改: 现在使用 pandas.NA 作为缺失的值,而不是 numpy.nan

在……里面 处理丢失的数据 ,我们看到Pandas主要用来 NaN 来表示丢失的数据。因为 NaN 是浮点数,这将强制具有任何缺失值的整数数组变为浮点数。在某些情况下,这可能并不重要。但是,如果您的整型列是一个标识符,则转换为浮点型可能会有问题。有些整数甚至不能表示为浮点数。

施工#

Pandas可以使用以下命令表示可能缺少值的整数数据 arrays.IntegerArray 。这是一个 extension type 在大Pandas体内实施。

In [1]: arr = pd.array([1, 2, None], dtype=pd.Int64Dtype())

In [2]: arr
Out[2]: 
<IntegerArray>
[1, 2, <NA>]
Length: 3, dtype: Int64

或字符串别名 "Int64" (请注意大写 "I" ,以区别于NumPy的 'int64' 数据类型:

In [3]: pd.array([1, 2, np.nan], dtype="Int64")
Out[3]: 
<IntegerArray>
[1, 2, <NA>]
Length: 3, dtype: Int64

所有类似NA的值都被替换为 pandas.NA

In [4]: pd.array([1, 2, np.nan, None, pd.NA], dtype="Int64")
Out[4]: 
<IntegerArray>
[1, 2, <NA>, <NA>, <NA>]
Length: 5, dtype: Int64

此数组可以存储在 DataFrameSeries 就像任何NumPy数组一样。

In [5]: pd.Series(arr)
Out[5]: 
0       1
1       2
2    <NA>
dtype: Int64

您还可以将类似列表的对象传递给 Series 具有数据类型的构造函数。

警告

目前 pandas.array()pandas.Series() 使用不同的规则进行数据类型推理。 pandas.array() 将推断出可为空的整型数据类型

In [6]: pd.array([1, None])
Out[6]: 
<IntegerArray>
[1, <NA>]
Length: 2, dtype: Int64

In [7]: pd.array([1, 2])
Out[7]: 
<IntegerArray>
[1, 2]
Length: 2, dtype: Int64

为了向后兼容, Series 将它们推断为整型或浮点型

In [8]: pd.Series([1, None])
Out[8]: 
0    1.0
1    NaN
dtype: float64

In [9]: pd.Series([1, 2])
Out[9]: 
0    1
1    2
dtype: int64

我们建议显式提供dtype以避免混淆。

In [10]: pd.array([1, None], dtype="Int64")
Out[10]: 
<IntegerArray>
[1, <NA>]
Length: 2, dtype: Int64

In [11]: pd.Series([1, None], dtype="Int64")
Out[11]: 
0       1
1    <NA>
dtype: Int64

在未来,我们可能会提供以下选项 Series 推断出可为空的整型数据类型。

运营#

涉及整数数组的操作的行为类似于NumPy数组。丢失的值将被传播,如果需要,数据将被强制为另一种数据类型。

In [12]: s = pd.Series([1, 2, None], dtype="Int64")

# arithmetic
In [13]: s + 1
Out[13]: 
0       2
1       3
2    <NA>
dtype: Int64

# comparison
In [14]: s == 1
Out[14]: 
0     True
1    False
2     <NA>
dtype: boolean

# indexing
In [15]: s.iloc[1:3]
Out[15]: 
1       2
2    <NA>
dtype: Int64

# operate with other dtypes
In [16]: s + s.iloc[1:3].astype("Int8")
Out[16]: 
0    <NA>
1       4
2    <NA>
dtype: Int64

# coerce when needed
In [17]: s + 0.01
Out[17]: 
0    1.01
1    2.01
2    <NA>
dtype: Float64

这些数据类型可以作为 DataFrame

In [18]: df = pd.DataFrame({"A": s, "B": [1, 1, 3], "C": list("aab")})

In [19]: df
Out[19]: 
      A  B  C
0     1  1  a
1     2  1  a
2  <NA>  3  b

In [20]: df.dtypes
Out[20]: 
A     Int64
B     int64
C    object
dtype: object

这些数据类型可以合并、重塑和转换。

In [21]: pd.concat([df[["A"]], df[["B", "C"]]], axis=1).dtypes
Out[21]: 
A     Int64
B     int64
C    object
dtype: object

In [22]: df["A"].astype(float)
Out[22]: 
0    1.0
1    2.0
2    NaN
Name: A, dtype: float64

减法和分组操作(如‘SUM’)也适用。

In [23]: df.sum()
Out[23]: 
A      3
B      5
C    aab
dtype: object

In [24]: df.groupby("B").A.sum()
Out[24]: 
B
1    3
3    0
Name: A, dtype: Int64

标量NA值#

arrays.IntegerArray uses pandas.NA as its scalar missing value. Slicing a single element that's missing will return pandas.NA

In [25]: a = pd.array([1, None], dtype="Int64")

In [26]: a[1]
Out[26]: <NA>