Skip to content

Commit

Permalink
Creating a more clean implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rmhowe425 committed Jun 15, 2024
1 parent ffb65d4 commit 14e565e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
9 changes: 7 additions & 2 deletions pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,9 @@ class BooleanArray(BaseMaskedArray):
Length: 3, dtype: boolean
"""

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

@classmethod
def _simple_new(cls, values: np.ndarray, mask: npt.NDArray[np.bool_]) -> Self:
Expand Down Expand Up @@ -329,15 +330,19 @@ def _from_sequence_of_strings(
copy: bool = False,
true_values: list[str] | None = None,
false_values: list[str] | None = None,
none_values: list[str] | None = None,
) -> BooleanArray:
true_values_union = cls._TRUE_VALUES.union(true_values or [])
false_values_union = cls._FALSE_VALUES.union(false_values or [])
none_values_union = cls._NONE_VALUES.union(none_values or [])

def map_string(s) -> bool:
if s in true_values_union:
return True
elif s in false_values_union:
return False
elif s in none_values_union:
return None
else:
raise ValueError(f"{s} cannot be cast to bool")

Expand Down
15 changes: 4 additions & 11 deletions pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,18 +753,11 @@ def _cast_types(self, values: ArrayLike, cast_type: DtypeObj, column) -> ArrayLi
true_values=self.true_values,
false_values=self.false_values,
)
elif isinstance(cast_type, BooleanDtype) and all(
isinstance(value, bool) for value in values
):
values = [str(val) for val in values]
return array_type._from_sequence_of_strings( # type: ignore[call-arg]
values,
dtype=cast_type,
true_values=self.true_values,
false_values=self.false_values,
)
else:
return array_type._from_sequence_of_strings(values, dtype=cast_type)
values_str = [str(val) for val in values]
return array_type._from_sequence_of_strings(
values_str, dtype=cast_type
)
except NotImplementedError as err:
raise NotImplementedError(
f"Extension Array: {array_type} must implement "
Expand Down

0 comments on commit 14e565e

Please sign in to comment.