From 55fc8af74d6950aa5931379fde8da44b6634a9c3 Mon Sep 17 00:00:00 2001 From: phofl Date: Fri, 13 Aug 2021 22:26:28 +0200 Subject: [PATCH 1/2] Bug in Series constructor returning wrong missing values --- doc/source/whatsnew/v1.4.0.rst | 1 + pandas/core/series.py | 2 +- pandas/tests/series/methods/test_astype.py | 7 +++++++ pandas/tests/series/test_constructors.py | 12 ++++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index f0af60f80edd5..0e8f0569fd702 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 ``boo`` (: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): From 46a622be0e964e75ce8e2ca91422e38ac73808e2 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Fri, 13 Aug 2021 17:43:26 -0400 Subject: [PATCH 2/2] Update doc/source/whatsnew/v1.4.0.rst --- doc/source/whatsnew/v1.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 0e8f0569fd702..98ecff94773e0 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -264,7 +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 ``boo`` (:issue:`43017`, :issue:`43018`) +- Bug in :class:`Series` constructor returning 0 for missing values with dtype ``int64`` and ``False`` for dtype ``bool`` (:issue:`43017`, :issue:`43018`) - Strings