Nullable integer データ型#
注意
IntegerArray は現在実験段階です。その API または実装は予告なしに変更される場合があります。欠損値としてpandas.NA
を使用します。
欠損データの操作で見たように、pandas は主に NaN
を欠損データの表現に使用します。NaN
は浮動小数点数であるため、欠損値を持つ整数の配列は浮動小数点型になります。場合によっては、これはあまり問題にならないかもしれません。しかし、整数型の列が、例えば識別子である場合、浮動小数点型にキャストすると問題が発生する可能性があります。一部の整数は浮動小数点数として表現することさえできません。
構築#
pandas は、arrays.IntegerArray
を使用して、欠損値を持つ可能性のある整数データを表現できます。これは 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
または、NumPy の 'int64'
dtype と区別するための文字列エイリアス "Int64"
(大文字の "I"
に注意)
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
この配列は、NumPy 配列と同じように、DataFrame
または Series
に格納できます。
In [5]: pd.Series(arr)
Out[5]:
0 1
1 2
2 <NA>
dtype: Int64
また、dtype を指定して、リストライクオブジェクトをSeries
コンストラクターに渡すこともできます。
警告
現在、pandas.array()
と pandas.Series()
は、dtype 推論に異なるルールを使用しています。pandas.array()
は、nullable-integer dtype を推論します。
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
はこれらを整数型または浮動小数点型の dtype として推論します。
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
が nullable-integer dtype を推論するオプションを提供する可能性があります。
操作#
整数配列を含む操作は、NumPy 配列と同様に動作します。欠損値は伝播され、必要に応じてデータは別の dtype に強制変換されます。
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
# slicing operation
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
これらの dtype は、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
これらの dtype は、マージ、リシェイプ、キャストできます。
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(numeric_only=True)
Out[23]:
A 3
B 5
dtype: Int64
In [24]: df.sum()
Out[24]:
A 3
B 5
C aab
dtype: object
In [25]: df.groupby("B").A.sum()
Out[25]:
B
1 3
3 0
Name: A, dtype: Int64
スカラーNA値#
arrays.IntegerArray
は、スカラー欠損値としてpandas.NA
を使用します。欠損している単一の要素をスライスすると、pandas.NA
が返されます。
In [26]: a = pd.array([1, None], dtype="Int64")
In [27]: a[1]
Out[27]: <NA>