-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
689db12
DEP: Enforce deprecation of mangle_dup cols and convert_float in read…
phofl 5d9a954
Remove test
phofl bc81cd8
ENH: Add use nullable dtypes to read_excel
phofl 27978e2
Merge branch 'dep_excel' into use_nullable_in_excel
phofl fc15503
Move parameters
phofl c835cfe
Merge remote-tracking branch 'upstream/main' into use_nullable_in_excel
phofl 29607af
Fix docstring
phofl 41555c4
Merge remote-tracking branch 'upstream/main' into use_nullable_in_excel
phofl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = [ | ||
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done