Skip to content

Commit 4b6be69

Browse files
Integer NA docs (#23617)
* wip * DOC: Integer NA Closes #22003 * subsection * update * fixup * add back construction for docs
1 parent d5283ea commit 4b6be69

File tree

5 files changed

+170
-29
lines changed

5 files changed

+170
-29
lines changed

doc/source/gotchas.rst

+22-2
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,28 @@ arrays. For example:
215215
s2.dtype
216216
217217
This trade-off is made largely for memory and performance reasons, and also so
218-
that the resulting ``Series`` continues to be "numeric". One possibility is to
219-
use ``dtype=object`` arrays instead.
218+
that the resulting ``Series`` continues to be "numeric".
219+
220+
If you need to represent integers with possibly missing values, use one of
221+
the nullable-integer extension dtypes provided by pandas
222+
223+
* :class:`Int8Dtype`
224+
* :class:`Int16Dtype`
225+
* :class:`Int32Dtype`
226+
* :class:`Int64Dtype`
227+
228+
.. ipython:: python
229+
230+
s_int = pd.Series([1, 2, 3, 4, 5], index=list('abcde'),
231+
dtype=pd.Int64Dtype())
232+
s_int
233+
s_int.dtype
234+
235+
s2_int = s_int.reindex(['a', 'b', 'c', 'f', 'u'])
236+
s2_int
237+
s2_int.dtype
238+
239+
See :ref:`integer_na` for more.
220240

221241
``NA`` type promotions
222242
~~~~~~~~~~~~~~~~~~~~~~

doc/source/index.rst.template

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ See the package overview for more detail about what's in the library.
143143
timeseries
144144
timedeltas
145145
categorical
146+
integer_na
146147
visualization
147148
style
148149
io

doc/source/integer_na.rst

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
.. currentmodule:: pandas
2+
3+
{{ header }}
4+
5+
.. _integer_na:
6+
7+
**************************
8+
Nullable Integer Data Type
9+
**************************
10+
11+
.. versionadded:: 0.24.0
12+
13+
In :ref:`missing_data`, we saw that pandas primarily uses ``NaN`` to represent
14+
missing data. Because ``NaN`` is a float, this forces an array of integers with
15+
any missing values to become floating point. In some cases, this may not matter
16+
much. But if your integer column is, say, an identifier, casting to float can
17+
be problematic. Some integers cannot even be represented as floating point
18+
numbers.
19+
20+
Pandas can represent integer data with possibly missing values using
21+
:class:`arrays.IntegerArray`. This is an :ref:`extension types <extending.extension-types>`
22+
implemented within pandas. It is not the default dtype for integers, and will not be inferred;
23+
you must explicitly pass the dtype into :meth:`array` or :class:`Series`:
24+
25+
.. ipython:: python
26+
27+
arr = pd.array([1, 2, np.nan], dtype=pd.Int64Dtype())
28+
arr
29+
30+
Or the string alias ``"Int64"`` (note the capital ``"I"``, to differentiate from
31+
NumPy's ``'int64'`` dtype:
32+
33+
.. ipython:: python
34+
35+
pd.array([1, 2, np.nan], dtype="Int64")
36+
37+
This array can be stored in a :class:`DataFrame` or :class:`Series` like any
38+
NumPy array.
39+
40+
.. ipython:: python
41+
42+
pd.Series(arr)
43+
44+
You can also pass the list-like object to the :class:`Series` constructor
45+
with the dtype.
46+
47+
.. ipython:: python
48+
49+
s = pd.Series([1, 2, np.nan], dtype="Int64")
50+
s
51+
52+
By default (if you don't specify ``dtype``), NumPy is used, and you'll end
53+
up with a ``float64`` dtype Series:
54+
55+
.. ipython:: python
56+
57+
pd.Series([1, 2, np.nan])
58+
59+
Operations involving an integer array will behave similar to NumPy arrays.
60+
Missing values will be propagated, and and the data will be coerced to another
61+
dtype if needed.
62+
63+
.. ipython:: python
64+
65+
# arithmetic
66+
s + 1
67+
68+
# comparison
69+
s == 1
70+
71+
# indexing
72+
s.iloc[1:3]
73+
74+
# operate with other dtypes
75+
s + s.iloc[1:3].astype('Int8')
76+
77+
# coerce when needed
78+
s + 0.01
79+
80+
These dtypes can operate as part of of ``DataFrame``.
81+
82+
.. ipython:: python
83+
84+
df = pd.DataFrame({'A': s, 'B': [1, 1, 3], 'C': list('aab')})
85+
df
86+
df.dtypes
87+
88+
89+
These dtypes can be merged & reshaped & casted.
90+
91+
.. ipython:: python
92+
93+
pd.concat([df[['A']], df[['B', 'C']]], axis=1).dtypes
94+
df['A'].astype(float)
95+
96+
Reduction and groupby operations such as 'sum' work as well.
97+
98+
.. ipython:: python
99+
100+
df.sum()
101+
df.groupby('B').A.sum()

doc/source/missing_data.rst

+43-26
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,6 @@ pandas.
1919

2020
See the :ref:`cookbook<cookbook.missing_data>` for some advanced strategies.
2121

22-
Missing data basics
23-
-------------------
24-
25-
When / why does data become missing?
26-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27-
28-
Some might quibble over our usage of *missing*. By "missing" we simply mean
29-
**NA** ("not available") or "not present for whatever reason". Many data sets simply arrive with
30-
missing data, either because it exists and was not collected or it never
31-
existed. For example, in a collection of financial time series, some of the time
32-
series might start on different dates. Thus, values prior to the start date
33-
would generally be marked as missing.
34-
35-
In pandas, one of the most common ways that missing data is **introduced** into
36-
a data set is by reindexing. For example:
37-
38-
.. ipython:: python
39-
40-
df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f', 'h'],
41-
columns=['one', 'two', 'three'])
42-
df['four'] = 'bar'
43-
df['five'] = df['one'] > 0
44-
df
45-
df2 = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
46-
df2
47-
4822
Values considered "missing"
4923
~~~~~~~~~~~~~~~~~~~~~~~~~~~
5024

@@ -62,6 +36,16 @@ arise and we wish to also consider that "missing" or "not available" or "NA".
6236

6337
.. _missing.isna:
6438

39+
.. ipython:: python
40+
41+
df = pd.DataFrame(np.random.randn(5, 3), index=['a', 'c', 'e', 'f', 'h'],
42+
columns=['one', 'two', 'three'])
43+
df['four'] = 'bar'
44+
df['five'] = df['one'] > 0
45+
df
46+
df2 = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
47+
df2
48+
6549
To make detecting missing values easier (and across different array dtypes),
6650
pandas provides the :func:`isna` and
6751
:func:`notna` functions, which are also methods on
@@ -90,6 +74,23 @@ Series and DataFrame objects:
9074
9175
df2['one'] == np.nan
9276
77+
Integer Dtypes and Missing Data
78+
-------------------------------
79+
80+
Because ``NaN`` is a float, a column of integers with even one missing values
81+
is cast to floating-point dtype (see :ref:`gotchas.intna` for more). Pandas
82+
provides a nullable integer array, which can be used by explicitly requesting
83+
the dtype:
84+
85+
.. ipython:: python
86+
87+
pd.Series([1, 2, np.nan, 4], dtype=pd.Int64Dtype())
88+
89+
Alternatively, the string alias ``dtype='Int64'`` (note the capital ``"I"``) can be
90+
used.
91+
92+
See :ref:`integer_na` for more.
93+
9394
Datetimes
9495
---------
9596

@@ -751,3 +752,19 @@ However, these can be filled in using :meth:`~DataFrame.fillna` and it will work
751752
752753
reindexed[crit.fillna(False)]
753754
reindexed[crit.fillna(True)]
755+
756+
Pandas provides a nullable integer dtype, but you must explicitly request it
757+
when creating the series or column. Notice that we use a capital "I" in
758+
the ``dtype="Int64"``.
759+
760+
.. ipython:: python
761+
762+
s = pd.Series(np.random.randn(5), index=[0, 2, 4, 6, 7],
763+
dtype="Int64")
764+
s > 0
765+
(s > 0).dtype
766+
crit = (s > 0).reindex(list(range(8)))
767+
crit
768+
crit.dtype
769+
770+
See :ref:`integer_na` for more.

doc/source/whatsnew/v0.24.0.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ Reduction and groupby operations such as 'sum' work.
159159
160160
.. warning::
161161

162-
The Integer NA support currently uses the captilized dtype version, e.g. ``Int8`` as compared to the traditional ``int8``. This may be changed at a future date.
162+
The Integer NA support currently uses the capitalized dtype version, e.g. ``Int8`` as compared to the traditional ``int8``. This may be changed at a future date.
163+
164+
See :ref:`integer_na` for more.
163165

164166
.. _whatsnew_0240.enhancements.array:
165167

0 commit comments

Comments
 (0)