Skip to content

Commit 2d75ffc

Browse files
committed
TST: test and doc for #18515
1 parent 9c6d3dc commit 2d75ffc

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

doc/source/whatsnew/v0.22.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Other API Changes
6868

6969
- :func:`Series.astype` and :func:`Index.astype` with an incompatible dtype will now raise a ``TypeError`` rather than a ``ValueError`` (:issue:`18231`)
7070
- ``Series`` construction with an ``object`` dtyped tz-aware datetime and ``dtype=object`` specified, will now return an ``object`` dtyped ``Series``, previously this would infer the datetime dtype (:issue:`18231`)
71+
- A :class:`Series` of ``dtype=category`` constructed from an empty ``dict`` will now have categories of ``dtype=object`` rather than ``dtype=float64``, consistently with the case in which an empty list is passed (:issue:`18515`)
7172
- ``NaT`` division with :class:`datetime.timedelta` will now return ``NaN`` instead of raising (:issue:`17876`)
7273
- All-NaN levels in a ``MultiIndex`` are now assigned ``float`` rather than ``object`` dtype, promoting consistency with ``Index`` (:issue:`17929`).
7374
- :class:`Timestamp` will no longer silently ignore unused or invalid ``tz`` or ``tzinfo`` keyword arguments (:issue:`17690`)

pandas/tests/series/test_constructors.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from datetime import datetime, timedelta
7+
from collections import OrderedDict
78

89
from numpy import nan
910
import numpy as np
@@ -79,17 +80,34 @@ def test_constructor(self):
7980
m = MultiIndex.from_arrays([[1, 2], [3, 4]])
8081
pytest.raises(NotImplementedError, Series, m)
8182

82-
def test_constructor_empty(self):
83+
@pytest.mark.parametrize('input_class', [list, dict, OrderedDict])
84+
def test_constructor_empty(self, input_class):
8385
empty = Series()
84-
empty2 = Series([])
85-
86-
# the are Index() and RangeIndex() which don't compare type equal
86+
empty2 = Series(input_class())
87+
# these are Index() and RangeIndex() which don't compare type equal
8788
# but are just .equals
8889
assert_series_equal(empty, empty2, check_index_type=False)
8990

90-
empty = Series(index=lrange(10))
91-
empty2 = Series(np.nan, index=lrange(10))
92-
assert_series_equal(empty, empty2)
91+
# With explicit dtype:
92+
empty = Series(dtype='float64')
93+
empty2 = Series(input_class(), dtype='float64')
94+
assert_series_equal(empty, empty2, check_index_type=False)
95+
96+
# GH 18515 : with dtype=category:
97+
empty = Series(dtype='category')
98+
empty2 = Series(input_class(), dtype='category')
99+
assert_series_equal(empty, empty2, check_index_type=False)
100+
101+
if input_class is not list:
102+
# With index:
103+
empty = Series(index=lrange(10))
104+
empty2 = Series(input_class(), index=lrange(10))
105+
assert_series_equal(empty, empty2)
106+
107+
# With index and dtype float64:
108+
empty = Series(np.nan, index=lrange(10))
109+
empty2 = Series(input_class(), index=lrange(10), dtype='float64')
110+
assert_series_equal(empty, empty2)
93111

94112
def test_constructor_series(self):
95113
index1 = ['d', 'b', 'a', 'c']

0 commit comments

Comments
 (0)