Skip to content

Commit 098661e

Browse files
authored
Bug in Series constructor returning wrong missing values (#43026)
1 parent b759e69 commit 098661e

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ Numeric
266266
Conversion
267267
^^^^^^^^^^
268268
- 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`)
269+
- Bug in :class:`Series` constructor returning 0 for missing values with dtype ``int64`` and ``False`` for dtype ``bool`` (:issue:`43017`, :issue:`43018`)
269270
-
270271

271272
Strings

pandas/core/series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ def _init_dict(self, data, index=None, dtype: Dtype | None = None):
475475
elif index is not None:
476476
# fastpath for Series(data=None). Just use broadcasting a scalar
477477
# instead of reindexing.
478-
values = na_value_for_dtype(pandas_dtype(dtype))
478+
values = na_value_for_dtype(pandas_dtype(dtype), compat=False)
479479
keys = index
480480
else:
481481
keys, values = (), []

pandas/tests/series/methods/test_astype.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,13 @@ def test_astype_bytes(self):
356356
result = Series(["foo", "bar", "baz"]).astype(bytes)
357357
assert result.dtypes == np.dtype("S3")
358358

359+
def test_astype_nan_to_bool(self):
360+
# GH#43018
361+
ser = Series(np.nan, dtype="object")
362+
result = ser.astype("bool")
363+
expected = Series(True, dtype="bool")
364+
tm.assert_series_equal(result, expected)
365+
359366

360367
class TestAstypeString:
361368
@pytest.mark.parametrize(

pandas/tests/series/test_constructors.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,18 @@ def test_constructor_with_pandas_dtype(self):
17761776
ser2 = Series(vals, dtype=dtype)
17771777
tm.assert_series_equal(ser, ser2)
17781778

1779+
def test_constructor_int_dtype_missing_values(self):
1780+
# GH#43017
1781+
result = Series(index=[0], dtype="int64")
1782+
expected = Series(np.nan, index=[0], dtype="float64")
1783+
tm.assert_series_equal(result, expected)
1784+
1785+
def test_constructor_bool_dtype_missing_values(self):
1786+
# GH#43018
1787+
result = Series(index=[0], dtype="bool")
1788+
expected = Series(True, index=[0], dtype="bool")
1789+
tm.assert_series_equal(result, expected)
1790+
17791791

17801792
class TestSeriesConstructorIndexCoercion:
17811793
def test_series_constructor_datetimelike_index_coercion(self):

0 commit comments

Comments
 (0)