Skip to content

Commit 4237e62

Browse files
Terji PetersenTerji Petersen
authored andcommitted
BUG/API: ndexes on empty frames/series should be RangeIndex, are Index[object]
1 parent 3c72d6f commit 4237e62

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+129
-109
lines changed

pandas/core/frame.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,6 @@ def __init__(
632632
copy: bool | None = None,
633633
) -> None:
634634

635-
if data is None:
636-
data = {}
637635
if dtype is not None:
638636
dtype = self._validate_dtype(dtype)
639637

@@ -671,6 +669,12 @@ def __init__(
671669
else:
672670
copy = False
673671

672+
if data is None:
673+
index = index if index is not None else default_index(0)
674+
columns = columns if columns is not None else default_index(0)
675+
dtype = dtype if dtype is not None else pandas_dtype(object)
676+
data = []
677+
674678
if isinstance(data, (BlockManager, ArrayManager)):
675679
mgr = self._init_mgr(
676680
data, axes={"index": index, "columns": columns}, dtype=dtype, copy=copy
@@ -777,7 +781,7 @@ def __init__(
777781
mgr = dict_to_mgr(
778782
{},
779783
index,
780-
columns,
784+
columns if columns is not None else default_index(0),
781785
dtype=dtype,
782786
typ=manager,
783787
)
@@ -2310,7 +2314,7 @@ def maybe_reorder(
23102314
result_index = None
23112315
if len(arrays) == 0 and index is None and length == 0:
23122316
# for backward compat use an object Index instead of RangeIndex
2313-
result_index = Index([])
2317+
result_index = default_index(0)
23142318

23152319
arrays, arr_columns = reorder_arrays(arrays, arr_columns, columns, length)
23162320
return arrays, arr_columns, result_index

pandas/core/internals/construction.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,9 +580,9 @@ def _extract_index(data) -> Index:
580580
"""
581581
Try to infer an Index from the passed data, raise ValueError on failure.
582582
"""
583-
index = None
583+
index: Index | None = None
584584
if len(data) == 0:
585-
index = Index([])
585+
index = default_index(0)
586586
else:
587587
raw_lengths = []
588588
indexes: list[list[Hashable] | Index] = []

pandas/core/reshape/merge.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,8 +1063,8 @@ def _get_join_info(
10631063
else:
10641064
join_index = default_index(len(left_indexer))
10651065

1066-
if len(join_index) == 0:
1067-
join_index = join_index.astype(object)
1066+
if len(join_index) == 0 and not isinstance(join_index, MultiIndex):
1067+
join_index = default_index(0).set_names(join_index.name)
10681068
return join_index, left_indexer, right_indexer
10691069

10701070
def _create_join_index(

pandas/core/series.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,16 @@ def __init__(
385385
if index is not None:
386386
index = ensure_index(index)
387387

388-
if data is None:
389-
data = {}
390388
if dtype is not None:
391389
dtype = self._validate_dtype(dtype)
392390

391+
if data is None:
392+
index = index if index is not None else default_index(0)
393+
if len(index) or dtype is not None:
394+
data = na_value_for_dtype(pandas_dtype(dtype), compat=False)
395+
else:
396+
data = []
397+
393398
if isinstance(data, MultiIndex):
394399
raise NotImplementedError(
395400
"initializing a Series from a MultiIndex is not supported"

pandas/io/parsers/base_parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
from pandas.core.indexes.api import (
8181
Index,
8282
MultiIndex,
83+
default_index,
8384
ensure_index_from_sequences,
8485
)
8586
from pandas.core.series import Series
@@ -1085,8 +1086,9 @@ def _get_empty_meta(
10851086
#
10861087
# Both must be non-null to ensure a successful construction. Otherwise,
10871088
# we have to create a generic empty Index.
1089+
index: Index
10881090
if (index_col is None or index_col is False) or index_names is None:
1089-
index = Index([])
1091+
index = default_index(0)
10901092
else:
10911093
data = [Series([], dtype=dtype_dict[name]) for name in index_names]
10921094
index = ensure_index_from_sequences(data, names=index_names)

pandas/tests/apply/test_frame_apply.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,14 @@ def test_apply_with_reduce_empty():
114114
result = empty_frame.apply(x.append, axis=1, result_type="expand")
115115
tm.assert_frame_equal(result, empty_frame)
116116
result = empty_frame.apply(x.append, axis=1, result_type="reduce")
117-
expected = Series([], index=pd.Index([], dtype=object), dtype=np.float64)
117+
expected = Series([], dtype=np.float64)
118118
tm.assert_series_equal(result, expected)
119119

120120
empty_with_cols = DataFrame(columns=["a", "b", "c"])
121121
result = empty_with_cols.apply(x.append, axis=1, result_type="expand")
122122
tm.assert_frame_equal(result, empty_with_cols)
123123
result = empty_with_cols.apply(x.append, axis=1, result_type="reduce")
124-
expected = Series([], index=pd.Index([], dtype=object), dtype=np.float64)
124+
expected = Series([], dtype=np.float64)
125125
tm.assert_series_equal(result, expected)
126126

127127
# Ensure that x.append hasn't been called
@@ -147,7 +147,7 @@ def test_nunique_empty():
147147
tm.assert_series_equal(result, expected)
148148

149149
result = df.T.nunique()
150-
expected = Series([], index=pd.Index([]), dtype=np.float64)
150+
expected = Series([], dtype=np.float64)
151151
tm.assert_series_equal(result, expected)
152152

153153

pandas/tests/apply/test_str.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
from pandas import (
1010
DataFrame,
11-
Index,
1211
Series,
1312
)
1413
import pandas._testing as tm
@@ -149,8 +148,8 @@ def test_agg_cython_table_series(series, func, expected):
149148
tm.get_cython_table_params(
150149
Series(dtype=np.float64),
151150
[
152-
("cumprod", Series([], Index([]), dtype=np.float64)),
153-
("cumsum", Series([], Index([]), dtype=np.float64)),
151+
("cumprod", Series([], dtype=np.float64)),
152+
("cumsum", Series([], dtype=np.float64)),
154153
],
155154
),
156155
tm.get_cython_table_params(

pandas/tests/extension/base/constructors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def test_construct_empty_dataframe(self, dtype):
119119
# GH 33623
120120
result = pd.DataFrame(columns=["a"], dtype=dtype)
121121
expected = pd.DataFrame(
122-
{"a": pd.array([], dtype=dtype)}, index=pd.Index([], dtype="object")
122+
{"a": pd.array([], dtype=dtype)}, index=pd.RangeIndex(0)
123123
)
124124
self.assert_frame_equal(result, expected)
125125

pandas/tests/extension/base/missing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_dropna_frame(self, data_missing):
5555

5656
# axis = 1
5757
result = df.dropna(axis="columns")
58-
expected = pd.DataFrame(index=[0, 1])
58+
expected = pd.DataFrame(index=pd.RangeIndex(2), columns=pd.Index([]))
5959
self.assert_frame_equal(result, expected)
6060

6161
# multiple

pandas/tests/frame/indexing/test_xs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_xs_corner(self):
8484
# no columns but Index(dtype=object)
8585
df = DataFrame(index=["a", "b", "c"])
8686
result = df.xs("a")
87-
expected = Series([], name="a", index=Index([]), dtype=np.float64)
87+
expected = Series([], name="a", dtype=np.float64)
8888
tm.assert_series_equal(result, expected)
8989

9090
def test_xs_duplicates(self):

0 commit comments

Comments
 (0)