diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index f0af60f80edd5..98ecff94773e0 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -264,6 +264,7 @@ Numeric Conversion ^^^^^^^^^^ - Bug in :class:`UInt64Index` constructor when passing a list containing both positive integers small enough to cast to int64 and integers too large too hold in int64 (:issue:`42201`) +- Bug in :class:`Series` constructor returning 0 for missing values with dtype ``int64`` and ``False`` for dtype ``bool`` (:issue:`43017`, :issue:`43018`) - Strings diff --git a/pandas/core/series.py b/pandas/core/series.py index 7766a7ea362eb..e5e19373fcea1 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -475,7 +475,7 @@ def _init_dict(self, data, index=None, dtype: Dtype | None = None): elif index is not None: # fastpath for Series(data=None). Just use broadcasting a scalar # instead of reindexing. - values = na_value_for_dtype(pandas_dtype(dtype)) + values = na_value_for_dtype(pandas_dtype(dtype), compat=False) keys = index else: keys, values = (), [] diff --git a/pandas/tests/series/methods/test_astype.py b/pandas/tests/series/methods/test_astype.py index c1fedb14be949..7fbdc455a8dcf 100644 --- a/pandas/tests/series/methods/test_astype.py +++ b/pandas/tests/series/methods/test_astype.py @@ -356,6 +356,13 @@ def test_astype_bytes(self): result = Series(["foo", "bar", "baz"]).astype(bytes) assert result.dtypes == np.dtype("S3") + def test_astype_nan_to_bool(self): + # GH#43018 + ser = Series(np.nan, dtype="object") + result = ser.astype("bool") + expected = Series(True, dtype="bool") + tm.assert_series_equal(result, expected) + class TestAstypeString: @pytest.mark.parametrize( diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 8525edc8dee80..2a6fc81a43f50 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1776,6 +1776,18 @@ def test_constructor_with_pandas_dtype(self): ser2 = Series(vals, dtype=dtype) tm.assert_series_equal(ser, ser2) + def test_constructor_int_dtype_missing_values(self): + # GH#43017 + result = Series(index=[0], dtype="int64") + expected = Series(np.nan, index=[0], dtype="float64") + tm.assert_series_equal(result, expected) + + def test_constructor_bool_dtype_missing_values(self): + # GH#43018 + result = Series(index=[0], dtype="bool") + expected = Series(True, index=[0], dtype="bool") + tm.assert_series_equal(result, expected) + class TestSeriesConstructorIndexCoercion: def test_series_constructor_datetimelike_index_coercion(self):