From 3971d02b9a49dfb591af20ead8aac76716df3628 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Fri, 8 Nov 2019 22:58:35 +0100 Subject: [PATCH] changed dtype conversion to warning to not break existing behaviour --- pandas/core/series.py | 13 ++++++++++--- pandas/tests/series/test_constructors.py | 17 ++++++++++++----- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 4fae3db938b738..552c9ba0750130 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -319,10 +319,17 @@ def __init__( data = data.copy() else: if isinstance(data, (list, tuple)) and not data and dtype is None: - # makes sure that empty Series has dtype object - # while this inconsistent with numpy, it is consistent + # Empty Series should have dtype object to be consistent # with the behaviour of DataFrame and Index - dtype = np.dtype(object) + warnings.warn( + "The default dtype for empty Series will be 'object' instead" + " of 'float64' in the next version. Specify a dtype explicitly" + " to silence this warning.", + FutureWarning, + stacklevel=7, + ) + # uncomment the line below when removing the FutureWarning + # dtype = np.dtype(object) data = sanitize_array(data, index, dtype, copy, raise_cast_failure=True) data = SingleBlockManager(data, index, fastpath=True) diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index a5817ac4b3a171..17a4a1beb4e859 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -53,13 +53,19 @@ class TestSeriesConstructors: ], ) def test_empty_constructor(self, constructor, check_index_type): - expected = Series() - result = constructor() + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + expected = Series() + result = constructor() + assert len(result.index) == 0 tm.assert_series_equal(result, expected, check_index_type=check_index_type) def test_dtype_of_empty_series(self): - assert Series().dtype == object + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + Series() + + # no warning when dtype is specified explicitly + Series(dtype="object") def test_invalid_dtype(self): # GH15520 @@ -117,8 +123,9 @@ def test_constructor(self, datetime_series): @pytest.mark.parametrize("input_class", [list, dict, OrderedDict]) def test_constructor_empty(self, input_class): - empty = Series() - empty2 = Series(input_class()) + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + empty = Series() + empty2 = Series(input_class()) # these are Index() and RangeIndex() which don't compare type equal # but are just .equals