Skip to content

ENH: Add use nullable dtypes to read_excel #49091

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 8 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Other enhancements
- :meth:`.DataFrameGroupBy.quantile` and :meth:`.SeriesGroupBy.quantile` now preserve nullable dtypes instead of casting to numpy dtypes (:issue:`37493`)
- :meth:`Series.add_suffix`, :meth:`DataFrame.add_suffix`, :meth:`Series.add_prefix` and :meth:`DataFrame.add_prefix` support an ``axis`` argument. If ``axis`` is set, the default behaviour of which axis to consider can be overwritten (:issue:`47819`)
- :func:`assert_frame_equal` now shows the first element where the DataFrames differ, analogously to ``pytest``'s output (:issue:`47910`)
- Added new argument ``use_nullable_dtypes`` to :func:`read_csv` to enable automatic conversion to nullable dtypes (:issue:`36712`)
- Added new argument ``use_nullable_dtypes`` to :func:`read_csv` and :func:`read_excel` to enable automatic conversion to nullable dtypes (:issue:`36712`)
- Added new global configuration, ``io.nullable_backend`` to allow ``use_nullable_dtypes=True`` to return pyarrow-backed dtypes when set to ``"pyarrow"`` in :func:`read_parquet` (:issue:`48957`)
- Added ``index`` parameter to :meth:`DataFrame.to_dict` (:issue:`46398`)
- Added metadata propagation for binary operators on :class:`DataFrame` (:issue:`28283`)
Expand Down
4 changes: 3 additions & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2370,7 +2370,7 @@ def maybe_convert_numeric(

# This occurs since we disabled float nulls showing as null in anticipation
# of seeing ints that were never seen. So then, we return float
if allow_null_in_int and seen.null_ and not seen.int_:
if allow_null_in_int and seen.null_ and not seen.int_ and not seen.bool_:
seen.float_ = True

if seen.complex_:
Expand All @@ -2390,6 +2390,8 @@ def maybe_convert_numeric(
else:
return (ints, None)
elif seen.bool_:
if allow_null_in_int:
return (bools.view(np.bool_), mask.view(np.bool_))
return (bools.view(np.bool_), None)
elif seen.uint_:
return (uints, None)
Expand Down
15 changes: 15 additions & 0 deletions pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,13 @@

.. versionadded:: 1.2.0

use_nullable_dtypes : bool = False
Copy link
Member

Choose a reason for hiding this comment

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

: bool, default False here I think.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Whether or not to use nullable dtypes as default when reading data. If
set to True, nullable dtypes are used for all dtypes that have a nullable
implementation, even if no nulls are present.

.. versionadded:: 2.0

Returns
-------
DataFrame or dict of DataFrames
Expand Down Expand Up @@ -375,6 +382,7 @@ def read_excel(
comment: str | None = ...,
skipfooter: int = ...,
storage_options: StorageOptions = ...,
use_nullable_dtypes: bool = ...,
) -> DataFrame:
...

Expand Down Expand Up @@ -413,6 +421,7 @@ def read_excel(
comment: str | None = ...,
skipfooter: int = ...,
storage_options: StorageOptions = ...,
use_nullable_dtypes: bool = ...,
) -> dict[IntStrT, DataFrame]:
...

Expand Down Expand Up @@ -451,6 +460,7 @@ def read_excel(
comment: str | None = None,
skipfooter: int = 0,
storage_options: StorageOptions = None,
use_nullable_dtypes: bool = False,
) -> DataFrame | dict[IntStrT, DataFrame]:

should_close = False
Expand Down Expand Up @@ -487,6 +497,7 @@ def read_excel(
decimal=decimal,
comment=comment,
skipfooter=skipfooter,
use_nullable_dtypes=use_nullable_dtypes,
)
finally:
# make sure to close opened file handles
Expand Down Expand Up @@ -690,6 +701,7 @@ def parse(
decimal: str = ".",
comment: str | None = None,
skipfooter: int = 0,
use_nullable_dtypes: bool = False,
**kwds,
):

Expand Down Expand Up @@ -848,6 +860,7 @@ def parse(
comment=comment,
skipfooter=skipfooter,
usecols=usecols,
use_nullable_dtypes=use_nullable_dtypes,
**kwds,
)

Expand Down Expand Up @@ -1680,6 +1693,7 @@ def parse(
thousands: str | None = None,
comment: str | None = None,
skipfooter: int = 0,
use_nullable_dtypes: bool = False,
**kwds,
) -> DataFrame | dict[str, DataFrame] | dict[int, DataFrame]:
"""
Expand Down Expand Up @@ -1711,6 +1725,7 @@ def parse(
thousands=thousands,
comment=comment,
skipfooter=skipfooter,
use_nullable_dtypes=use_nullable_dtypes,
**kwds,
)

Expand Down
5 changes: 4 additions & 1 deletion pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,10 @@ def _infer_types(
bool_mask = np.zeros(result.shape, dtype=np.bool_)
result = BooleanArray(result, bool_mask)
elif result.dtype == np.object_ and use_nullable_dtypes:
result = StringDtype().construct_array_type()._from_sequence(values)
# read_excel sends array of datetime objects
inferred_type, _ = lib.infer_datetimelike_array(result)
if inferred_type != "datetime":
result = StringDtype().construct_array_type()._from_sequence(values)

return result, na_count

Expand Down
82 changes: 82 additions & 0 deletions pandas/tests/io/excel/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
Series,
)
import pandas._testing as tm
from pandas.core.arrays import (
ArrowStringArray,
StringArray,
)

read_ext_params = [".xls", ".xlsx", ".xlsm", ".xlsb", ".ods"]
engine_params = [
Expand Down Expand Up @@ -532,6 +536,84 @@ def test_reader_dtype_str(self, read_ext, dtype, expected):
actual = pd.read_excel(basename + read_ext, dtype=dtype)
tm.assert_frame_equal(actual, expected)

def test_use_nullable_dtypes(self, read_ext):
# GH#36712
if read_ext == ".xlsb":
pytest.skip("No engine for filetype: 'xlsb'")

df = DataFrame(
{
"a": Series([1, 3], dtype="Int64"),
"b": Series([2.5, 4.5], dtype="Float64"),
"c": Series([True, False], dtype="boolean"),
"d": Series(["a", "b"], dtype="string"),
"e": Series([pd.NA, 6], dtype="Int64"),
"f": Series([pd.NA, 7.5], dtype="Float64"),
"g": Series([pd.NA, True], dtype="boolean"),
"h": Series([pd.NA, "a"], dtype="string"),
"i": Series([pd.Timestamp("2019-12-31")] * 2),
"j": Series([pd.NA, pd.NA], dtype="Int64"),
}
)
with tm.ensure_clean(read_ext) as file_path:
df.to_excel(file_path, "test", index=False)
result = pd.read_excel(
file_path, sheet_name="test", use_nullable_dtypes=True
)
tm.assert_frame_equal(result, df)

def test_use_nullabla_dtypes_and_dtype(self, read_ext):
# GH#36712
if read_ext == ".xlsb":
pytest.skip("No engine for filetype: 'xlsb'")

df = DataFrame({"a": [np.nan, 1.0], "b": [2.5, np.nan]})
with tm.ensure_clean(read_ext) as file_path:
df.to_excel(file_path, "test", index=False)
result = pd.read_excel(
file_path, sheet_name="test", use_nullable_dtypes=True, dtype="float64"
Copy link
Member

Choose a reason for hiding this comment

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

Makes sense to me that dtype overrides use_nullable_dtypes - could you add this to the docstring.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

)
tm.assert_frame_equal(result, df)

@td.skip_if_no("pyarrow")
@pytest.mark.parametrize("storage", ["pyarrow", "python"])
def test_use_nullabla_dtypes_string(self, read_ext, storage):
# GH#36712
if read_ext == ".xlsb":
pytest.skip("No engine for filetype: 'xlsb'")

import pyarrow as pa

with pd.option_context("mode.string_storage", storage):

df = DataFrame(
{
"a": np.array(["a", "b"], dtype=np.object_),
"b": np.array(["x", pd.NA], dtype=np.object_),
}
)
with tm.ensure_clean(read_ext) as file_path:
df.to_excel(file_path, "test", index=False)
result = pd.read_excel(
file_path, sheet_name="test", use_nullable_dtypes=True
)

if storage == "python":
expected = DataFrame(
{
"a": StringArray(np.array(["a", "b"], dtype=np.object_)),
"b": StringArray(np.array(["x", pd.NA], dtype=np.object_)),
}
)
else:
expected = DataFrame(
{
"a": ArrowStringArray(pa.array(["a", "b"])),
"b": ArrowStringArray(pa.array(["x", None])),
}
)
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("dtypes, exp_value", [({}, "1"), ({"a.1": "int64"}, 1)])
def test_dtype_mangle_dup_cols(self, read_ext, dtypes, exp_value):
# GH#35211
Expand Down