Skip to content

Commit cf8c047

Browse files
committed
Handle dtype correctly
1 parent f31cb6b commit cf8c047

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

pandas/core/series.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@
4141
maybe_cast_to_datetime, maybe_castable,
4242
construct_1d_arraylike_from_scalar,
4343
construct_1d_object_array_from_listlike)
44-
from pandas.core.dtypes.missing import isna, notna, remove_na_arraylike
44+
from pandas.core.dtypes.missing import (
45+
isna,
46+
notna,
47+
remove_na_arraylike,
48+
na_value_for_dtype)
4549

4650
from pandas.core.index import (Index, MultiIndex, InvalidIndexError,
4751
Float64Index, _ensure_index)
@@ -219,7 +223,7 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
219223
elif isinstance(data, dict):
220224
# Same as previous block, but special cased for data=None,
221225
# for performance when creating empty arrays.
222-
data = np.nan
226+
data = na_value_for_dtype(dtype)
223227

224228
elif isinstance(data, SingleBlockManager):
225229
if index is None:

pandas/tests/series/test_constructors.py

+11
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ def test_constructor_nan(self, input_arg):
122122

123123
assert_series_equal(empty, empty2, check_index_type=False)
124124

125+
@pytest.mark.parametrize('dtype', [
126+
'f8', 'i8', 'M8[ns]', 'm8[ns]', 'category', 'object',
127+
'datetime64[ns, UTC]',
128+
])
129+
@pytest.mark.parametrize('index', [None, pd.Index([])])
130+
def test_constructor_dtype_only(self, dtype, index):
131+
# GH-20865
132+
result = pd.Series(dtype=dtype, index=index)
133+
assert result.dtype == dtype
134+
assert len(result) == 0
135+
125136
def test_constructor_series(self):
126137
index1 = ['d', 'b', 'a', 'c']
127138
index2 = sorted(index1)

0 commit comments

Comments
 (0)