Skip to content

Commit 9015013

Browse files
phoflmroeschke
andauthored
BUG: Series constructor raising with NA and bools and Int dtype (#50328)
* BUG: Series constructor raising with NA and bools and Int dtype * Fix test * Split tests * Update doc/source/whatsnew/v2.0.0.rst Co-authored-by: Matthew Roeschke <[email protected]>
1 parent edd401b commit 9015013

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ Numeric
815815
Conversion
816816
^^^^^^^^^^
817817
- Bug in constructing :class:`Series` with ``int64`` dtype from a string list raising instead of casting (:issue:`44923`)
818+
- Bug in constructing :class:`Series` with masked dtype and boolean values with ``NA`` raising (:issue:`42137`)
818819
- Bug in :meth:`DataFrame.eval` incorrectly raising an ``AttributeError`` when there are negative values in function call (:issue:`46471`)
819820
- Bug in :meth:`Series.convert_dtypes` not converting dtype to nullable dtype when :class:`Series` contains ``NA`` and has dtype ``object`` (:issue:`48791`)
820821
- Bug where any :class:`ExtensionDtype` subclass with ``kind="M"`` would be interpreted as a timezone type (:issue:`34986`)

pandas/core/arrays/numeric.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,7 @@ def _coerce_to_data_and_mask(values, mask, dtype, copy, dtype_cls, default_dtype
172172
inferred_type = None
173173
if is_object_dtype(values.dtype) or is_string_dtype(values.dtype):
174174
inferred_type = lib.infer_dtype(values, skipna=True)
175-
if inferred_type == "empty":
176-
pass
177-
elif inferred_type == "boolean":
175+
if inferred_type == "boolean" and dtype is None:
178176
name = dtype_cls.__name__.strip("_")
179177
raise TypeError(f"{values.dtype} cannot be converted to {name}")
180178

pandas/tests/series/test_constructors.py

+15
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,21 @@ def test_constructor_mismatched_null_nullable_dtype(
19871987
with pytest.raises(TypeError, match=msg):
19881988
func([null, 1.0, 3.0], dtype=any_numeric_ea_dtype)
19891989

1990+
def test_series_constructor_ea_int_from_bool(self):
1991+
# GH#42137
1992+
result = Series([True, False, True, pd.NA], dtype="Int64")
1993+
expected = Series([1, 0, 1, pd.NA], dtype="Int64")
1994+
tm.assert_series_equal(result, expected)
1995+
1996+
result = Series([True, False, True], dtype="Int64")
1997+
expected = Series([1, 0, 1], dtype="Int64")
1998+
tm.assert_series_equal(result, expected)
1999+
2000+
def test_series_constructor_ea_int_from_string_bool(self):
2001+
# GH#42137
2002+
with pytest.raises(ValueError, match="invalid literal"):
2003+
Series(["True", "False", "True", pd.NA], dtype="Int64")
2004+
19902005

19912006
class TestSeriesConstructorIndexCoercion:
19922007
def test_series_constructor_datetimelike_index_coercion(self):

0 commit comments

Comments
 (0)