Skip to content

ENH: nullable Float32/64 ExtensionArray #34307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
cddc939
ENH: nullable Float32/64 ExtensionArray
jorisvandenbossche May 15, 2020
6a1d822
fix doctest
jorisvandenbossche May 22, 2020
f43f021
add basic whatsnew note
jorisvandenbossche May 22, 2020
ffdd65c
typo
jorisvandenbossche May 22, 2020
3d189f9
Merge remote-tracking branch 'upstream/master' into EA-floating
jorisvandenbossche May 29, 2020
ff3b937
fix astype to string
jorisvandenbossche May 29, 2020
13b8281
Merge remote-tracking branch 'upstream/master' into EA-floating
jorisvandenbossche Jun 15, 2020
4c1d06c
Merge remote-tracking branch 'upstream/master' into EA-floating
jorisvandenbossche Jun 19, 2020
ebbc64d
clean-up arithmetic tests to align with integer/boolean tests
jorisvandenbossche Jun 19, 2020
94be5f2
Merge remote-tracking branch 'upstream/master' into EA-floating
jorisvandenbossche Jun 22, 2020
8cf0d47
updates for feedback
jorisvandenbossche Jun 22, 2020
f7cc1be
fix string array construction
jorisvandenbossche Jun 22, 2020
107b083
fix mypy
jorisvandenbossche Jun 22, 2020
ba1e62c
Merge remote-tracking branch 'upstream/master' into EA-floating
jorisvandenbossche Jul 3, 2020
879d3e0
Merge remote-tracking branch 'upstream/master' into EA-floating
jorisvandenbossche Jul 11, 2020
ed9a14b
rename _FloatingDtype -> FloatingDtype
jorisvandenbossche Jul 11, 2020
c16ca4c
update astype implementation to follow IntegerArray changes
jorisvandenbossche Jul 11, 2020
aa45aac
clean-up tests
jorisvandenbossche Jul 11, 2020
25eb1ba
remove usage of deprecated check_less_precise
jorisvandenbossche Jul 12, 2020
b78c041
Merge remote-tracking branch 'upstream/master' into EA-floating
jorisvandenbossche Sep 18, 2020
45b98f2
fixup merge + skip astype(string) for float32
jorisvandenbossche Sep 18, 2020
314b6a9
linting
jorisvandenbossche Sep 18, 2020
a157806
add back type ignore
jorisvandenbossche Sep 18, 2020
81456f9
whatsnew 1.2
jorisvandenbossche Sep 19, 2020
71009c3
update whatsnew
jorisvandenbossche Sep 19, 2020
56d2311
add fixture
jorisvandenbossche Sep 19, 2020
65a2060
share some dtype properties
jorisvandenbossche Sep 19, 2020
8b36098
update ensure_float
jorisvandenbossche Sep 19, 2020
7f3e965
Merge remote-tracking branch 'upstream/master' into EA-floating
jorisvandenbossche Sep 23, 2020
66d6939
remove skip for float32 conversion to string
jorisvandenbossche Sep 23, 2020
e0c9d9a
Merge remote-tracking branch 'upstream/master' into EA-floating
jorisvandenbossche Sep 29, 2020
d37b815
code formatting
jorisvandenbossche Sep 29, 2020
44e699a
Update doc/source/whatsnew/v1.2.0.rst
jorisvandenbossche Sep 30, 2020
b42b61d
Update pandas/core/arrays/floating.py
jorisvandenbossche Sep 30, 2020
edf9618
ignore mypy bug
jorisvandenbossche Sep 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,66 @@ If needed you can adjust the bins with the argument ``offset`` (a Timedelta) tha

For a full example, see: :ref:`timeseries.adjust-the-start-of-the-bins`.

.. _whatsnew_110.floating:

Experimental nullable data types for float data
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

We've added :class:`Float32Dtype` / :class:`Float64Dtype` and :class:`~arrays.FloatingArray`,
an extension data type dedicated to floating point data that can hold the
``pd.NA`` missing value indicator (:issue:`32265`, :issue:`34307`).

While the default float data type already supports missing values using ``np.nan``,
this new data type uses ``pd.NA`` (and its corresponding behaviour) as missing
value indicator, in line with the already existing nullable :ref:`integer <integer_na>`
and :ref:`boolean <boolean>` data types.

One example where the behaviour of ``np.nan`` and ``pd.NA`` is different is
comparison operations:

.. code-block:: python

# the default numpy float64 dtype
>>> s1 = pd.Series([1.5, None])
>>> s1
0 1.5
1 NaN
dtype: float64

>>> s1 > 1
0 True
1 False
dtype: bool

# the new nullable float64 dtype
>>> s2 = pd.Series([1.5, None], dtype="Float64")
>>> s2
0 1.5
1 <NA>
dtype: Float64

>>> s2 > 1
0 True
1 <NA>
dtype: boolean

See the :ref:`missing_data.NA` doc section for more details on the behaviour
when using the ``pd.NA`` missing value indicator.

As shown above, the dtype can be specified using the "Float64" or "Float32"
string (capitalized to distinguish it from the default "float64" data type).
Alternatively, you can also use the dtype object:

.. ipython:: python

pd.Series([1.5, None], dtype=pd.Float32Dtype())

.. warning::

Experimental: the new floating data types are currently experimental, and its
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a doc section can link to?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a doc section can link to?

Not yet, can probably put more or less the same content as in this whatsnew somewhere in the user guide

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, we should consolidate https://pandas.pydata.org/pandas-docs/dev/user_guide/integer_na.html and https://pandas.pydata.org/pandas-docs/dev/user_guide/boolean.html into a single "nullable data types" page and add it there (not in this PR).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, that has been on my personal to do list, but never got to it. In general we should have a page about data types (and can still have some subpages if needed)

behaviour or API may still change without warning. Expecially the behaviour
regarding NaN (distinct from NA missing values) is subject to change.

fsspec now used for filesystem handling
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
2 changes: 2 additions & 0 deletions pandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
UInt16Dtype,
UInt32Dtype,
UInt64Dtype,
Float32Dtype,
Float64Dtype,
CategoricalDtype,
PeriodDtype,
IntervalDtype,
Expand Down
1 change: 1 addition & 0 deletions pandas/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
ALL_EA_INT_DTYPES = UNSIGNED_EA_INT_DTYPES + SIGNED_EA_INT_DTYPES

FLOAT_DTYPES: List[Dtype] = [float, "float32", "float64"]
FLOAT_EA_DTYPES: List[Dtype] = ["Float32", "Float64"]
COMPLEX_DTYPES: List[Dtype] = [complex, "complex64", "complex128"]
STRING_DTYPES: List[Dtype] = [str, "str", "U"]

Expand Down
2 changes: 2 additions & 0 deletions pandas/arrays/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
BooleanArray,
Categorical,
DatetimeArray,
FloatingArray,
IntegerArray,
IntervalArray,
PandasArray,
Expand All @@ -20,6 +21,7 @@
"BooleanArray",
"Categorical",
"DatetimeArray",
"FloatingArray",
"IntegerArray",
"IntervalArray",
"PandasArray",
Expand Down
1 change: 1 addition & 0 deletions pandas/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from pandas.core.algorithms import factorize, unique, value_counts
from pandas.core.arrays import Categorical
from pandas.core.arrays.boolean import BooleanDtype
from pandas.core.arrays.floating import Float32Dtype, Float64Dtype
from pandas.core.arrays.integer import (
Int8Dtype,
Int16Dtype,
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/arrays/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
from pandas.core.arrays.boolean import BooleanArray
from pandas.core.arrays.categorical import Categorical
from pandas.core.arrays.datetimes import DatetimeArray
from pandas.core.arrays.floating import FloatingArray
from pandas.core.arrays.integer import IntegerArray, integer_array
from pandas.core.arrays.interval import IntervalArray
from pandas.core.arrays.masked import BaseMaskedArray
from pandas.core.arrays.numpy_ import PandasArray, PandasDtype
from pandas.core.arrays.period import PeriodArray, period_array
from pandas.core.arrays.sparse import SparseArray
Expand All @@ -18,9 +20,11 @@
"ExtensionArray",
"ExtensionOpsMixin",
"ExtensionScalarOpsMixin",
"BaseMaskedArray",
"BooleanArray",
"Categorical",
"DatetimeArray",
"FloatingArray",
"IntegerArray",
"integer_array",
"IntervalArray",
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,10 +606,11 @@ def logical_method(self, other):
@classmethod
def _create_comparison_method(cls, op):
def cmp_method(self, other):
from pandas.arrays import IntegerArray
from pandas.arrays import IntegerArray, FloatingArray

if isinstance(
other, (ABCDataFrame, ABCSeries, ABCIndexClass, IntegerArray)
other,
(ABCDataFrame, ABCSeries, ABCIndexClass, IntegerArray, FloatingArray),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should create a superclass NumericArray I think for Integer / Floating

):
# Rely on pandas to unbox and dispatch to us.
return NotImplemented
Expand Down
Loading