Skip to content

BUG: read_csv with dtype=bool[pyarrow] #53391

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 6 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/v2.0.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Bug fixes
- Bug in :func:`api.interchange.from_dataframe` was unnecessarily raising on bitmasks (:issue:`49888`)
- Bug in :func:`merge` when merging on datetime columns on different resolutions (:issue:`53200`)
- Bug in :func:`read_csv` raising ``OverflowError`` for ``engine="pyarrow"`` and ``parse_dates`` set (:issue:`53295`)
- Bug in :func:`read_csv` when defining ``dtype`` with ``bool[pyarrow]`` (:issue:`53390`)
Copy link
Member

Choose a reason for hiding this comment

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

This is only for "c"/"python" parsers, right?

Might want to clarify that here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah correct, updated.

- Bug in :func:`to_datetime` was inferring format to contain ``"%H"`` instead of ``"%I"`` if date contained "AM" / "PM" tokens (:issue:`53147`)
- Bug in :func:`to_timedelta` was raising ``ValueError`` with ``pandas.NA`` (:issue:`52909`)
- Bug in :meth:`DataFrame.__getitem__` not preserving dtypes for :class:`MultiIndex` partial keys (:issue:`51895`)
Expand Down
6 changes: 5 additions & 1 deletion pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ from pandas.core.dtypes.dtypes import (
)
from pandas.core.dtypes.inference import is_dict_like

from pandas.core.arrays.boolean import BooleanDtype

cdef:
float64_t INF = <float64_t>np.inf
float64_t NEGINF = -INF
Expand Down Expand Up @@ -1194,7 +1196,9 @@ cdef class TextReader:
array_type = dtype.construct_array_type()
try:
# use _from_sequence_of_strings if the class defines it
if dtype.kind == "b":
if isinstance(dtype, BooleanDtype):
# xref GH 47534: BooleanArray._from_sequence_of_strings has extra
# kwargs
true_values = [x.decode() for x in self.true_values]
false_values = [x.decode() for x in self.false_values]
result = array_type._from_sequence_of_strings(
Expand Down
3 changes: 2 additions & 1 deletion pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
FloatingArray,
IntegerArray,
)
from pandas.core.arrays.boolean import BooleanDtype
from pandas.core.indexes.api import (
Index,
MultiIndex,
Expand Down Expand Up @@ -809,7 +810,7 @@ def _cast_types(self, values: ArrayLike, cast_type: DtypeObj, column) -> ArrayLi
elif isinstance(cast_type, ExtensionDtype):
array_type = cast_type.construct_array_type()
try:
if is_bool_dtype(cast_type):
if isinstance(cast_type, BooleanDtype):
# error: Unexpected keyword argument "true_values" for
# "_from_sequence_of_strings" of "ExtensionArray"
return array_type._from_sequence_of_strings( # type: ignore[call-arg] # noqa: E501
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/io/parser/dtypes/test_dtypes_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,3 +538,17 @@ def test_ea_int_avoid_overflow(all_parsers):
}
)
tm.assert_frame_equal(result, expected)


def test_dtype_bool_pyarrow(all_parsers):
# GH 53390
pytest.importorskip("pyarrow")
parser = all_parsers
data = """col
True
False
True
"""
result = parser.read_csv(StringIO(data), dtype={"col": "bool[pyarrow]"})
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a test for dtype_backend=pyarrow as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I modified test_EA_types to test this instead

expected = DataFrame({"col": [True, False, True]}, dtype="bool[pyarrow]")
tm.assert_frame_equal(result, expected)