Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2132,6 +2132,9 @@ def sanitize_objects(ndarray[object] values, set na_values) -> int:
if val in na_values:
values[i] = onan
na_count += 1
elif val in [0, 1, True, False]:
Copy link
Member

Choose a reason for hiding this comment

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

is it possible to get np.True_ or np.False_? do we want int 0. or 1.0 do get caught here?

Copy link
Author

Choose a reason for hiding this comment

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

Tested with them, works as expected

Copy link
Member

Choose a reason for hiding this comment

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

would it be more performant to a) create this list once outside the loop, b) use a set, c) use is comparisons?

Copy link
Author

Choose a reason for hiding this comment

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

yes, using a predefined set is marginally better, updated code

# Skip memoization, since 1==True and 0==False
Copy link
Member

Choose a reason for hiding this comment

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

GH ref to the motivating issue pls

Copy link
Author

Choose a reason for hiding this comment

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

Added now

values[i] = val
elif val in memo:
values[i] = memo[val]
else:
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/io/parser/common/test_common_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from pandas._config import using_string_dtype

from pandas._libs import parsers as libparsers
from pandas.compat import HAS_PYARROW
from pandas.errors import (
EmptyDataError,
Expand Down Expand Up @@ -830,3 +831,13 @@ def test_read_seek(all_parsers):
actual = parser.read_csv(file)
expected = parser.read_csv(StringIO(content))
tm.assert_frame_equal(actual, expected)


def test_dtype_conversion_in_sanitization():
Copy link
Member

Choose a reason for hiding this comment

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

is there a user-facing behavior we can test?

Copy link
Author

Choose a reason for hiding this comment

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

yes, added the same test mentioned in the issue under tests/io/excel/test_readers

# GH60088
values = np.array([1, True], dtype=object)
expected = np.array([1, True], dtype=object)
libparsers.sanitize_objects(values, na_values=set())
for v, e in zip(values, expected):
assert v == e
assert type(v) == type(e)
Loading