Skip to content

BUG: read_csv with mixed bools and NaNs sometimes reads NaNs as 1.0 (#42808) #42944

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 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ I/O
- Bug in :func:`read_excel` attempting to read chart sheets from .xlsx files (:issue:`41448`)
- Bug in :func:`json_normalize` where ``errors=ignore`` could fail to ignore missing values of ``meta`` when ``record_path`` has a length greater than one (:issue:`41876`)
- Bug in :func:`read_csv` with multi-header input and arguments referencing column names as tuples (:issue:`42446`)
- Bug in :func:`read_csv` where reading a mixed column of booleans and missing values to a float type results in the missing values becoming 1.0 rather than NaN (:issue:`42808`)
-

Period
Expand Down
25 changes: 24 additions & 1 deletion pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1089,8 +1089,31 @@ cdef class TextReader:
break

# we had a fallback parse on the dtype, so now try to cast
# only allow safe casts, eg. with a nan you cannot safely cast to int
if col_res is not None and col_dtype is not None:
# If col_res is bool, it might actually be a bool array mixed with NaNs
# (see _try_bool_flex()). Usually this would be taken care of using
# _maybe_upcast(), but if col_dtype is a floating type we should just
# take care of that cast here.
if col_res.dtype == np.bool_ and is_float_dtype(col_dtype):
mask = col_res.view(np.uint8) == na_values[np.uint8]
col_res = col_res.astype(col_dtype)
np.putmask(col_res, mask, np.nan)
return col_res, na_count

# Similar special case for bool => int.
if col_res.dtype == np.bool_ and is_integer_dtype(col_dtype):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you just do a similar check like on L1125? (e.g. astype then check)?

# Must throw if there were NaNs.
if na_count > 0:
raise ValueError(
f"cannot safely convert passed user dtype of "
f"{col_dtype} for {np.bool_} dtyped data in "
f"column {i} due to NA values"
)

# Falls through to safe cast below.
pass

# only allow safe casts, eg. with a nan you cannot safely cast to int
try:
col_res = col_res.astype(col_dtype, casting='safe')
except TypeError:
Expand Down
37 changes: 37 additions & 0 deletions pandas/tests/io/parser/test_na_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,3 +590,40 @@ def test_nan_multi_index(all_parsers):
)

tm.assert_frame_equal(result, expected)


def test_bool_and_nan_to_bool(all_parsers):
# GH 42808: (bool | NaN) => bool should error.
parser = all_parsers
data = """0
NaN
True
False
"""
with pytest.raises(ValueError, match="NA values"):
parser.read_csv(StringIO(data), dtype="bool")


def test_bool_and_nan_to_int(all_parsers):
# GH 42808: (bool | NaN) => int should error.
parser = all_parsers
data = """0
NaN
True
False
"""
with pytest.raises(ValueError, match="convert"):
print(parser.read_csv(StringIO(data), dtype="int"))


def test_bool_and_nan_to_float(all_parsers):
# GH 42808: (bool | NaN) => float should return 0.0/1.0/NaN.
parser = all_parsers
data = """0
NaN
True
False
"""
result = parser.read_csv(StringIO(data), dtype="float")
expected = DataFrame.from_dict({"0": [np.nan, 1.0, 0.0]})
tm.assert_frame_equal(result, expected)