Skip to content

BUG:error in reading a boolean column with blanks from excel file #45903 #46374

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ I/O
- Bug in :func:`DataFrame.to_excel` and :class:`ExcelWriter` would raise when writing an empty DataFrame to a ``.ods`` file (:issue:`45793`)
- Bug in Parquet roundtrip for Interval dtype with ``datetime64[ns]`` subtype (:issue:`45881`)
- Bug in :func:`read_excel` when reading a ``.ods`` file with newlines between xml elements(:issue:`45598`)
- Bug in :func:`read_excel` when reading a ``.xlsx`` file with a boolean column that contains blank lines(:issue:`45903`)

Period
^^^^^^
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 @@ -294,6 +294,7 @@ class BooleanArray(BaseMaskedArray):
# Fill values used for any/all
_truthy_value = True
_falsey_value = False
_NONE_VALUES = {"nan", "NaN", "None", "NA", "null", "NULL"}
Copy link
Member

Choose a reason for hiding this comment

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

I fail to understand how this is related to having blanks. In the current version of pandas (without the changes in this PR), what's the value of the blank value in the spreadsheet when constructing the extension array? Feels like it should be None or an empty string, not one of those.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@datapythonista Hello! Thanks for the review! The point here is that I added this list because the isna() fails. The None values are converted into strings so without this list, the "None" values will raise a ValueError. Of course if you have any suggestion on how to implement this better please tell me.

_TRUE_VALUES = {"True", "TRUE", "true", "1", "1.0"}
_FALSE_VALUES = {"False", "FALSE", "false", "0", "0.0"}

Expand Down Expand Up @@ -326,8 +327,8 @@ def _from_sequence_of_strings(
false_values_union = cls._FALSE_VALUES.union(false_values or [])

def map_string(s):
if isna(s):
return s
if isna(s) or s in cls._NONE_VALUES:
return None
elif s in true_values_union:
return True
elif s in false_values_union:
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ def _cast_types(self, values, cast_type, column):
try:
if is_bool_dtype(cast_type):
return array_type._from_sequence_of_strings(
values,
values.astype(str),
dtype=cast_type,
true_values=self.true_values,
false_values=self.false_values,
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
10 changes: 10 additions & 0 deletions pandas/tests/io/excel/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,16 @@ def test_euro_decimal_format(self, read_ext):
)
tm.assert_frame_equal(result, expected)

def test_read_boolean_column_with_blanks(self, read_ext):
# GH 45903
result = pd.read_excel(
"boolean_with_blanks" + read_ext, dtype={"var1": "boolean"}
)
expected = DataFrame(
[[True], [False], [None], [True]], columns=["var1"], dtype="boolean"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
[[True], [False], [None], [True]], columns=["var1"], dtype="boolean"
{'var1': [True, False, None, True}, dtype="boolean"

This seems simpler and easier to read and maintain.

)
tm.assert_frame_equal(result, expected)


class TestExcelFileRead:
@pytest.fixture(autouse=True)
Expand Down