Skip to content

Commit

Permalink
changed dtype conversion to warning to not break existing behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
SaturnFromTitan committed Nov 8, 2019
1 parent af0712d commit 3971d02
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
13 changes: 10 additions & 3 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 12 additions & 5 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3971d02

Please sign in to comment.