Skip to content

Commit 20b5ebb

Browse files
found a first working solution
1 parent 5c290c9 commit 20b5ebb

File tree

13 files changed

+44
-23
lines changed

13 files changed

+44
-23
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/extension/base/missing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_isna(self, data_missing):
2020
# GH 21189
2121
result = pd.Series(data_missing).drop([0, 1]).isna()
2222
expected = pd.Series([], dtype=bool)
23-
self.assert_series_equal(result, expected)
23+
self.assert_series_equal(result, expected, check_index_type=False)
2424

2525
def test_dropna_array(self, data_missing):
2626
result = data_missing.dropna()

pandas/tests/extension/decimal/test_decimal.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def convert(x):
8686
else:
8787
right_na = right.isna()
8888

89-
tm.assert_series_equal(left_na, right_na)
89+
tm.assert_series_equal(left_na, right_na, *args, **kwargs)
9090
return tm.assert_series_equal(left[~left_na], right[~right_na], *args, **kwargs)
9191

9292
@classmethod

pandas/tests/extension/test_sparse.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def test_isna(self, data_missing):
186186
# GH 21189
187187
result = pd.Series(data_missing).drop([0, 1]).isna()
188188
expected = pd.Series([], dtype=expected_dtype)
189-
self.assert_series_equal(result, expected)
189+
self.assert_series_equal(result, expected, check_index_type=False)
190190

191191
def test_fillna_limit_pad(self, data_missing):
192192
with tm.assert_produces_warning(PerformanceWarning):

pandas/tests/groupby/test_grouping.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ def test_groupby_empty(self):
641641
gr = s.groupby([])
642642

643643
result = gr.mean()
644-
tm.assert_series_equal(result, s)
644+
tm.assert_series_equal(result, s, check_index_type=False)
645645

646646
# check group properties
647647
assert len(gr.grouper.groupings) == 1

pandas/tests/io/json/test_pandas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ def test_series_roundtrip_empty(self, orient, numpy):
678678
else:
679679
expected.index = expected.index.astype(float)
680680

681-
tm.assert_series_equal(result, expected)
681+
tm.assert_series_equal(result, expected, check_index_type=False)
682682

683683
@pytest.mark.parametrize("numpy", [True, False])
684684
def test_series_roundtrip_timeseries(self, orient, numpy):

pandas/tests/reductions/test_reductions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ class TestSeriesMode:
10851085
def test_mode_empty(self, dropna, expected):
10861086
s = Series([], dtype=np.float64)
10871087
result = s.mode(dropna)
1088-
tm.assert_series_equal(result, expected)
1088+
tm.assert_series_equal(result, expected, check_index_type=False)
10891089

10901090
@pytest.mark.parametrize(
10911091
"dropna, data, expected",

pandas/tests/reshape/test_concat.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -736,17 +736,28 @@ def test_concat_categorical_empty(self):
736736
s1 = pd.Series([], dtype="category")
737737
s2 = pd.Series([], dtype="category")
738738

739-
tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), s2)
740-
tm.assert_series_equal(s1.append(s2, ignore_index=True), s2)
739+
tm.assert_series_equal(
740+
pd.concat([s1, s2], ignore_index=True), s2, check_index_type=False
741+
)
742+
tm.assert_series_equal(
743+
s1.append(s2, ignore_index=True), s2, check_index_type=False
744+
)
741745

742746
s1 = pd.Series([], dtype="category")
743747
s2 = pd.Series([], dtype="object")
744748

745749
# different dtype => not-category
746-
tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), s2)
747-
tm.assert_series_equal(s1.append(s2, ignore_index=True), s2)
748-
tm.assert_series_equal(pd.concat([s2, s1], ignore_index=True), s2)
749-
tm.assert_series_equal(s2.append(s1, ignore_index=True), s2)
750+
result = pd.concat([s1, s2], ignore_index=True)
751+
tm.assert_series_equal(result, s2, check_index_type=False)
752+
753+
result = s1.append(s2, ignore_index=True)
754+
tm.assert_series_equal(result, s2, check_index_type=False)
755+
756+
result = pd.concat([s1, s2], ignore_index=True)
757+
tm.assert_series_equal(result, s2, check_index_type=False)
758+
759+
result = s2.append(s1, ignore_index=True)
760+
tm.assert_series_equal(result, s2, check_index_type=False)
750761

751762
s1 = pd.Series([], dtype="category")
752763
s2 = pd.Series([np.nan, np.nan])
@@ -2202,7 +2213,7 @@ def test_concat_empty_series_timelike(self, tz, values):
22022213
}
22032214
)
22042215
result = concat([first, second], axis=1)
2205-
tm.assert_frame_equal(result, expected)
2216+
tm.assert_frame_equal(result, expected, check_index_type=False)
22062217

22072218
def test_default_index(self):
22082219
# is_series and ignore_index

pandas/tests/series/test_api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ async def test_tab_complete_warning(self, ip):
497497
pytest.importorskip("IPython", minversion="6.0.0")
498498
from IPython.core.completer import provisionalcompleter
499499

500-
code = "import pandas as pd; s = pd.Series()"
500+
code = "import pandas as pd; s = pd.Series(dtype=object)"
501501
await ip.run_code(code)
502502
with tm.assert_produces_warning(None):
503503
with provisionalcompleter("ignore"):

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):

pandas/tests/test_algos.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2163,7 +2163,7 @@ def test_int64_add_overflow():
21632163
class TestMode:
21642164
def test_no_mode(self):
21652165
exp = Series([], dtype=np.float64)
2166-
tm.assert_series_equal(algos.mode([]), exp)
2166+
tm.assert_series_equal(algos.mode([]), exp, check_index_type=False)
21672167

21682168
def test_mode_single(self):
21692169
# GH 15714

0 commit comments

Comments
 (0)