Skip to content

Commit 5789ee2

Browse files
Empty series now has the dtype object instead of float64
1 parent aa0f138 commit 5789ee2

File tree

5 files changed

+12
-4
lines changed

5 files changed

+12
-4
lines changed

pandas/core/series.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,13 @@ def __init__(
319319
elif copy:
320320
data = data.copy()
321321
else:
322-
data = sanitize_array(data, index, dtype, copy, raise_cast_failure=True)
322+
if isinstance(data, (list, tuple)) and not data and dtype is None:
323+
# makes sure that empty Series has dtype object
324+
# while this inconsistent with numpy, it is consistent
325+
# with the behaviour of DataFrame and Index
326+
dtype = np.dtype(object)
323327

328+
data = sanitize_array(data, index, dtype, copy, raise_cast_failure=True)
324329
data = SingleBlockManager(data, index, fastpath=True)
325330

326331
generic.NDFrame.__init__(self, data, fastpath=True)

pandas/tests/series/test_apply.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ def test_map_empty(self, index):
513513
s = Series(index)
514514
result = s.map({})
515515

516-
expected = pd.Series(np.nan, index=s.index)
516+
expected = pd.Series(np.nan, index=s.index, dtype="object")
517517
tm.assert_series_equal(result, expected)
518518

519519
def test_map_compat(self):

pandas/tests/series/test_constructors.py

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ def test_empty_constructor(self, constructor, check_index_type):
5858
assert len(result.index) == 0
5959
tm.assert_series_equal(result, expected, check_index_type=check_index_type)
6060

61+
def test_dtype_of_empty_series(self):
62+
assert Series().dtype == object
63+
6164
def test_invalid_dtype(self):
6265
# GH15520
6366
msg = "not understood"

pandas/tests/series/test_operators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ def test_ops_datetimelike_align(self):
761761
tm.assert_series_equal(result, expected)
762762

763763
def test_operators_corner(self, datetime_series):
764-
empty = Series([], index=Index([]))
764+
empty = Series([], index=Index([]), dtype="float64")
765765

766766
result = datetime_series + empty
767767
assert np.isnan(result).all()

pandas/tests/series/test_quantile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_quantile_multi(self, datetime_series):
6767

6868
result = datetime_series.quantile([])
6969
expected = pd.Series(
70-
[], name=datetime_series.name, index=Index([], dtype=float)
70+
[], name=datetime_series.name, index=Index([], dtype=float), dtype="float64"
7171
)
7272
tm.assert_series_equal(result, expected)
7373

0 commit comments

Comments
 (0)