Skip to content

Commit 65d8e9a

Browse files
author
Santhosh18
committed
Added support for '0' and '1' in BooleanArray._from_sequence_of_strings method
1 parent 867aea2 commit 65d8e9a

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ Other enhancements
328328
- :meth:`DataFrame.to_html` and :meth:`DataFrame.to_string`'s ``col_space`` parameter now accepts a list or dict to change only some specific columns' width (:issue:`28917`).
329329
- :meth:`DataFrame.to_excel` can now also write OpenOffice spreadsheet (.ods) files (:issue:`27222`)
330330
- :meth:`~Series.explode` now accepts ``ignore_index`` to reset the index, similarly to :meth:`pd.concat` or :meth:`DataFrame.sort_values` (:issue:`34932`).
331+
- :meth:`BooleanArray._from_sequence_of_strings` now accepts "0", "0.0", "1", "1.0" (:issue:`34859`)
331332

332333
.. ---------------------------------------------------------------------------
333334

pandas/core/arrays/boolean.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,9 @@ def _from_sequence_of_strings(
286286
def map_string(s):
287287
if isna(s):
288288
return s
289-
elif s in ["True", "TRUE", "true"]:
289+
elif s in ["True", "TRUE", "true", "1", "1.0"]:
290290
return True
291-
elif s in ["False", "FALSE", "false"]:
291+
elif s in ["False", "FALSE", "false", "0", "0.0"]:
292292
return False
293293
else:
294294
raise ValueError(f"{s} cannot be cast to bool")

pandas/tests/arrays/boolean/test_construction.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,11 @@ def test_coerce_to_numpy_array():
247247

248248
def test_to_boolean_array_from_strings():
249249
result = BooleanArray._from_sequence_of_strings(
250-
np.array(["True", "False", np.nan], dtype=object)
250+
np.array(["True", "False", "1.0", "0", np.nan], dtype=object)
251251
)
252252
expected = BooleanArray(
253-
np.array([True, False, False]), np.array([False, False, True])
253+
np.array([True, False, True, False, False]),
254+
np.array([False, False, False, False, True]),
254255
)
255256

256257
tm.assert_extension_array_equal(result, expected)

0 commit comments

Comments
 (0)