Skip to content

Commit 03f8a0c

Browse files
Terji PetersenTerji Petersen
Terji Petersen
authored and
Terji Petersen
committed
BUG: Series(index=[]) should have dtype=object
1 parent bb7dd2c commit 03f8a0c

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

pandas/core/series.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,10 @@ def _init_dict(
511511
elif index is not None:
512512
# fastpath for Series(data=None). Just use broadcasting a scalar
513513
# instead of reindexing.
514-
values = na_value_for_dtype(pandas_dtype(dtype), compat=False)
514+
if len(index):
515+
values = na_value_for_dtype(pandas_dtype(dtype), compat=False)
516+
else:
517+
values = []
515518
keys = index
516519
else:
517520
keys, values = (), []

pandas/tests/series/test_constructors.py

+30
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,36 @@ def test_empty_constructor(self, constructor, check_index_type):
101101
expected = Series()
102102
result = constructor()
103103

104+
assert result.dtype == object
105+
assert len(result.index) == 0
106+
tm.assert_series_equal(result, expected, check_index_type=check_index_type)
107+
108+
@pytest.mark.parametrize(
109+
"constructor,check_index_type",
110+
[
111+
# NOTE: some overlap with test_constructor_empty but that test does not
112+
# test for None or an empty generator.
113+
# test_constructor_pass_none tests None but only with the index also
114+
# passed.
115+
(lambda: Series(index=[]), True),
116+
(lambda: Series(None, index=[]), True),
117+
(lambda: Series({}, index=[]), True),
118+
(lambda: Series((), index=[]), False), # creates a RangeIndex
119+
(lambda: Series([], index=[]), False), # creates a RangeIndex
120+
(lambda: Series((_ for _ in []), index=[]), False), # creates a RangeIndex
121+
(lambda: Series(data=None, index=[]), True),
122+
(lambda: Series(data={}, index=[]), True),
123+
(lambda: Series(data=(), index=[]), False), # creates a RangeIndex
124+
(lambda: Series(data=[], index=[]), False), # creates a RangeIndex
125+
(lambda: Series(data=(_ for _ in []), index=[]), False), # RangeIndex
126+
],
127+
)
128+
def test_empty_constructor_with_index(self, constructor, check_index_type):
129+
# GH 49573
130+
expected = Series()
131+
result = constructor()
132+
133+
assert result.dtype == object
104134
assert len(result.index) == 0
105135
tm.assert_series_equal(result, expected, check_index_type=check_index_type)
106136

0 commit comments

Comments
 (0)