Skip to content

Commit 3c26e55

Browse files
changed dtype conversion to warning to not break existing behaviour
1 parent 12d342a commit 3c26e55

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

pandas/core/series.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,17 @@ def __init__(
319319
data = data.copy()
320320
else:
321321
if isinstance(data, (list, tuple)) and not data and dtype is None:
322-
# makes sure that empty Series has dtype object
323-
# while this inconsistent with numpy, it is consistent
322+
# Empty Series should have dtype object to be consistent
324323
# with the behaviour of DataFrame and Index
325-
dtype = np.dtype(object)
324+
warnings.warn(
325+
"The default dtype for empty Series will be 'object' instead"
326+
" of 'float64' in the next version. Specify a dtype explicitly"
327+
" to silence this warning.",
328+
FutureWarning,
329+
stacklevel=7,
330+
)
331+
# uncomment the line below when removing the FutureWarning
332+
# dtype = np.dtype(object)
326333

327334
data = sanitize_array(data, index, dtype, copy, raise_cast_failure=True)
328335
data = SingleBlockManager(data, index, fastpath=True)

pandas/tests/series/test_constructors.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,19 @@ class TestSeriesConstructors:
5353
],
5454
)
5555
def test_empty_constructor(self, constructor, check_index_type):
56-
expected = Series()
57-
result = constructor()
56+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
57+
expected = Series()
58+
result = constructor()
59+
5860
assert len(result.index) == 0
5961
tm.assert_series_equal(result, expected, check_index_type=check_index_type)
6062

6163
def test_dtype_of_empty_series(self):
62-
assert Series().dtype == object
64+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
65+
Series()
66+
67+
# no warning when dtype is specified explicitly
68+
Series(dtype="object")
6369

6470
def test_invalid_dtype(self):
6571
# GH15520
@@ -117,8 +123,9 @@ def test_constructor(self, datetime_series):
117123

118124
@pytest.mark.parametrize("input_class", [list, dict, OrderedDict])
119125
def test_constructor_empty(self, input_class):
120-
empty = Series()
121-
empty2 = Series(input_class())
126+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
127+
empty = Series()
128+
empty2 = Series(input_class())
122129

123130
# these are Index() and RangeIndex() which don't compare type equal
124131
# but are just .equals

0 commit comments

Comments
 (0)