Skip to content

[#16737] Index type for Series with empty data #32053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Other API changes
- :meth:`Series.describe` will now show distribution percentiles for ``datetime`` dtypes, statistics ``first`` and ``last``
will now be ``min`` and ``max`` to match with numeric dtypes in :meth:`DataFrame.describe` (:issue:`30164`)
- :meth:`Groupby.groups` now returns an abbreviated representation when called on large dataframes (:issue:`1135`)
- :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``.

Backwards incompatible API changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5563,7 +5563,9 @@ def _validate_join_method(method):
def default_index(n):
from pandas.core.indexes.range import RangeIndex

return RangeIndex(0, n, name=None)
if n == 0:
return Index([])
return RangeIndex(0, n)


def maybe_extract_name(name, obj, cls) -> Optional[Hashable]:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/base/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_isna(self, data_missing):

# GH 21189
result = pd.Series(data_missing).drop([0, 1]).isna()
expected = pd.Series([], dtype=bool)
expected = pd.Series([], dtype=bool, index=pd.RangeIndex(0))
self.assert_series_equal(result, expected)

def test_dropna_array(self, data_missing):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/decimal/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def convert(x):
else:
right_na = right.isna()

tm.assert_series_equal(left_na, right_na)
tm.assert_series_equal(left_na, right_na, *args, **kwargs)
return tm.assert_series_equal(left[~left_na], right[~right_na], *args, **kwargs)

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def test_isna(self, data_missing):

# GH 21189
result = pd.Series(data_missing).drop([0, 1]).isna()
expected = pd.Series([], dtype=expected_dtype)
expected = pd.Series([], dtype=expected_dtype, index=pd.RangeIndex(0))
self.assert_series_equal(result, expected)

def test_fillna_limit_pad(self, data_missing):
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def test_empty_constructor(self, constructor):
@pytest.mark.parametrize(
"emptylike,expected_index,expected_columns",
[
([[]], RangeIndex(1), RangeIndex(0)),
([[], []], RangeIndex(2), RangeIndex(0)),
([(_ for _ in [])], RangeIndex(1), RangeIndex(0)),
([[]], RangeIndex(1), Index([])),
([[], []], RangeIndex(2), Index([])),
([(_ for _ in [])], RangeIndex(1), Index([])),
],
)
def test_emptylike_constructor(self, emptylike, expected_index, expected_columns):
Expand Down Expand Up @@ -337,7 +337,7 @@ def test_constructor_dict(self):

# with dict of empty list and Series
frame = DataFrame({"A": [], "B": []}, columns=["A", "B"])
tm.assert_index_equal(frame.index, Index([], dtype=np.int64))
tm.assert_index_equal(frame.index, Index([]))

# GH 14381
# Dict with None value
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_filter_out_all_groups_in_df():
df = pd.DataFrame({"a": [1, 1, 2], "b": [1, 2, 0]})
res = df.groupby("a")
res = res.filter(lambda x: x["b"].sum() > 5, dropna=True)
expected = pd.DataFrame({"a": [], "b": []}, dtype="int64")
expected = pd.DataFrame({"a": [], "b": []}, dtype="int64", index=pd.RangeIndex(0))
tm.assert_frame_equal(expected, res)


Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/groupby/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,10 +611,7 @@ def test_list_grouper_with_nat(self):
@pytest.mark.parametrize(
"func,expected",
[
(
"transform",
pd.Series(name=2, dtype=np.float64, index=pd.RangeIndex(0, 0, 1)),
),
("transform", pd.Series(name=2, dtype=np.float64)),
(
"agg",
pd.Series(name=2, dtype=np.float64, index=pd.Float64Index([], name=1)),
Expand All @@ -638,10 +635,13 @@ def test_evaluate_with_empty_groups(self, func, expected):
def test_groupby_empty(self):
# https://github.com/pandas-dev/pandas/issues/27190
s = pd.Series([], name="name", dtype="float64")
gr = s.groupby([])

expected = s.copy()
expected.index = pd.RangeIndex(0)

gr = s.groupby([])
result = gr.mean()
tm.assert_series_equal(result, s)
tm.assert_series_equal(result, expected)

# check group properties
assert len(gr.grouper.groupings) == 1
Expand Down
10 changes: 6 additions & 4 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,16 @@ def test_roundtrip_empty(self, orient, convert_axes, numpy):
)
expected = self.empty_frame.copy()

# TODO: both conditions below are probably bugs
# TODO: conditions below are probably bugs
if convert_axes:
expected.index = expected.index.astype(float)
expected.columns = expected.columns.astype(float)

if numpy and orient == "values":
expected = expected.reindex([0], axis=1).reset_index(drop=True)

if convert_axes:
expected.index = expected.index.astype(float)

tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("convert_axes", [True, False])
Expand Down Expand Up @@ -675,8 +678,7 @@ def test_series_roundtrip_empty(self, orient, numpy):
# TODO: see what causes inconsistency
if orient in ("values", "records"):
expected = expected.reset_index(drop=True)
else:
expected.index = expected.index.astype(float)
expected.index = expected.index.astype(float)

tm.assert_series_equal(result, expected)

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/reshape/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2199,7 +2199,8 @@ def test_concat_empty_series_timelike(self, tz, values):
{
0: pd.Series([pd.NaT] * len(values), dtype="M8[ns]").dt.tz_localize(tz),
1: values,
}
},
index=pd.Index(list(range(len(values))), dtype="object"),
)
result = concat([first, second], axis=1)
tm.assert_frame_equal(result, expected)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/methods/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def test_interpolate_corners(self, kwargs):
s = Series([np.nan, np.nan])
tm.assert_series_equal(s.interpolate(**kwargs), s)

s = Series([], dtype=object).interpolate()
s = Series([], dtype=object, index=pd.RangeIndex(0)).interpolate()
tm.assert_series_equal(s.interpolate(**kwargs), s)

def test_interpolate_index_values(self):
Expand Down
15 changes: 9 additions & 6 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,37 +127,40 @@ def test_constructor_empty(self, input_class):
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
empty = Series()
empty2 = Series(input_class())

# these are Index() and RangeIndex() which don't compare type equal
# but are just .equals
tm.assert_series_equal(empty, empty2, check_index_type=False)
tm.assert_series_equal(empty, empty2)
assert type(empty.index) is Index

# With explicit dtype:
empty = Series(dtype="float64")
empty2 = Series(input_class(), dtype="float64")
tm.assert_series_equal(empty, empty2, check_index_type=False)
tm.assert_series_equal(empty, empty2)
assert type(empty.index) is Index

# GH 18515 : with dtype=category:
empty = Series(dtype="category")
empty2 = Series(input_class(), dtype="category")
tm.assert_series_equal(empty, empty2, check_index_type=False)
tm.assert_series_equal(empty, empty2)
assert type(empty.index) is Index

if input_class is not list:
# With index:
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=False):
empty = Series(index=range(10))
empty2 = Series(input_class(), index=range(10))
tm.assert_series_equal(empty, empty2)
assert type(empty.index) is pd.RangeIndex

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

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

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