Skip to content

Commit 6ff0416

Browse files
found a first working solution
1 parent 5c290c9 commit 6ff0416

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Other API changes
5656
- :meth:`Series.describe` will now show distribution percentiles for ``datetime`` dtypes, statistics ``first`` and ``last``
5757
will now be ``min`` and ``max`` to match with numeric dtypes in :meth:`DataFrame.describe` (:issue:`30164`)
5858
- :meth:`Groupby.groups` now returns an abbreviated representation when called on large dataframes (:issue:`1135`)
59+
- :class:`Series` constructor will default to construct an :class:`Index`, rather than an :class:`RangeIndex` when constructed with empty data, matching the behaviour of ``data=None``.
5960

6061
Backwards incompatible API changes
6162
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pandas/core/series.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,12 @@ def __init__(
299299
if index is None:
300300
if not is_list_like(data):
301301
data = [data]
302-
index = ibase.default_index(len(data))
302+
303+
if is_empty_data(data):
304+
index = Index([])
305+
else:
306+
index = ibase.default_index(len(data))
307+
303308
elif is_list_like(data):
304309

305310
# a scalar numpy array is list-like but doesn't

pandas/tests/series/test_constructors.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -127,37 +127,40 @@ def test_constructor_empty(self, input_class):
127127
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
128128
empty = Series()
129129
empty2 = Series(input_class())
130-
131-
# these are Index() and RangeIndex() which don't compare type equal
132-
# but are just .equals
133-
tm.assert_series_equal(empty, empty2, check_index_type=False)
130+
tm.assert_series_equal(empty, empty2)
131+
assert type(empty.index) is Index
134132

135133
# With explicit dtype:
136134
empty = Series(dtype="float64")
137135
empty2 = Series(input_class(), dtype="float64")
138-
tm.assert_series_equal(empty, empty2, check_index_type=False)
136+
tm.assert_series_equal(empty, empty2)
137+
assert type(empty.index) is Index
139138

140139
# GH 18515 : with dtype=category:
141140
empty = Series(dtype="category")
142141
empty2 = Series(input_class(), dtype="category")
143-
tm.assert_series_equal(empty, empty2, check_index_type=False)
142+
tm.assert_series_equal(empty, empty2)
143+
assert type(empty.index) is Index
144144

145145
if input_class is not list:
146146
# With index:
147147
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
148148
empty = Series(index=range(10))
149149
empty2 = Series(input_class(), index=range(10))
150150
tm.assert_series_equal(empty, empty2)
151+
assert type(empty.index) is pd.RangeIndex
151152

152153
# With index and dtype float64:
153154
empty = Series(np.nan, index=range(10))
154155
empty2 = Series(input_class(), index=range(10), dtype="float64")
155156
tm.assert_series_equal(empty, empty2)
157+
assert type(empty.index) is pd.RangeIndex
156158

157159
# GH 19853 : with empty string, index and dtype str
158160
empty = Series("", dtype=str, index=range(3))
159161
empty2 = Series("", index=range(3))
160162
tm.assert_series_equal(empty, empty2)
163+
assert type(empty.index) is pd.RangeIndex
161164

162165
@pytest.mark.parametrize("input_arg", [np.nan, float("nan")])
163166
def test_constructor_nan(self, input_arg):

pandas/tests/series/test_missing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,8 @@ def test_interpolate_corners(self, kwargs):
11511151
s = Series([np.nan, np.nan])
11521152
tm.assert_series_equal(s.interpolate(**kwargs), s)
11531153

1154-
s = Series([], dtype=object).interpolate()
1154+
index = pd.RangeIndex.from_range(range(0, 0))
1155+
s = Series([], dtype=object, index=index).interpolate()
11551156
tm.assert_series_equal(s.interpolate(**kwargs), s)
11561157

11571158
def test_interpolate_index_values(self):

0 commit comments

Comments
 (0)