Skip to content

PERF: Allow ensure_index_from_sequence to return RangeIndex #57786

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

Merged
merged 26 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
aa5589c
PERF: RangeIndex.take with 1 value return RangeIndex
mroeschke Mar 6, 2024
47af1ce
add issue number
mroeschke Mar 6, 2024
e7e0fcb
Move to _shallow_copy, support empty join as well
mroeschke Mar 6, 2024
67f1998
Fix self.name
mroeschke Mar 6, 2024
676a1f4
Merge remote-tracking branch 'upstream/main' into perf/ri/take_1
mroeschke Mar 6, 2024
e37500e
Merge remote-tracking branch 'upstream/main' into perf/ri/take_1
mroeschke Mar 6, 2024
2cc41fc
FIx error message
mroeschke Mar 6, 2024
7e1dac7
Merge remote-tracking branch 'upstream/main' into perf/ri/take_1
mroeschke Mar 6, 2024
c7da6ac
Fix hdf test
mroeschke Mar 6, 2024
90ff39c
PERF: Allow ensure_index_from_sequence to return RangeIndex
mroeschke Mar 8, 2024
66d3456
Ignore Index and Series objects
mroeschke Mar 9, 2024
5c01d6a
Merge remote-tracking branch 'upstream/main' into perf/ri/ensure_inde…
mroeschke Mar 9, 2024
7e6fcea
Fix doctest
mroeschke Mar 9, 2024
1ab4c1e
Merge remote-tracking branch 'upstream/main' into perf/ri/ensure_inde…
mroeschke Mar 13, 2024
b5144f4
More specific check
mroeschke Mar 13, 2024
64918cf
Merge remote-tracking branch 'upstream/main' into perf/ri/ensure_inde…
mroeschke Mar 13, 2024
08da810
Only allow int64, fix indexing
mroeschke Mar 13, 2024
8957995
Merge remote-tracking branch 'upstream/main' into perf/ri/ensure_inde…
mroeschke Mar 13, 2024
b7e5dc1
use Index
mroeschke Mar 13, 2024
f0592c5
Use np_values
mroeschke Mar 13, 2024
33fa1f4
Add back int32
mroeschke Mar 13, 2024
4015ac6
Merge remote-tracking branch 'upstream/main' into perf/ri/ensure_inde…
mroeschke Mar 14, 2024
5fb9516
Ignore Series and Index objects
mroeschke Mar 14, 2024
ff186c1
Merge remote-tracking branch 'upstream/main' into perf/ri/ensure_inde…
mroeschke Mar 14, 2024
4e7cf98
Wrong condition
mroeschke Mar 14, 2024
b5b9a89
Merge remote-tracking branch 'upstream/main' into perf/ri/ensure_inde…
mroeschke Mar 19, 2024
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
6 changes: 3 additions & 3 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@ Performance improvements
- Performance improvement in :meth:`MultiIndex.equals` for equal length indexes (:issue:`56990`)
- Performance improvement in :meth:`RangeIndex.__getitem__` with a boolean mask returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57588`)
- Performance improvement in :meth:`RangeIndex.append` when appending the same index (:issue:`57252`)
- Performance improvement in :meth:`RangeIndex.join` returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57651`)
- Performance improvement in :meth:`RangeIndex.reindex` returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57647`)
- Performance improvement in :meth:`RangeIndex.take` returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57445`)
- Performance improvement in :meth:`RangeIndex.join` returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57651`, :issue:`57752`)
- Performance improvement in :meth:`RangeIndex.reindex` returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57647`, :issue:`57752`)
- Performance improvement in :meth:`RangeIndex.take` returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57445`, :issue:`57752`)
- Performance improvement in ``DataFrameGroupBy.__len__`` and ``SeriesGroupBy.__len__`` (:issue:`57595`)
- Performance improvement in indexing operations for string dtypes (:issue:`56997`)

Expand Down
45 changes: 41 additions & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4235,7 +4235,6 @@ def join(

return self._join_via_get_indexer(other, how, sort)

@final
def _join_empty(
self, other: Index, how: JoinHow, sort: bool
) -> tuple[Index, npt.NDArray[np.intp] | None, npt.NDArray[np.intp] | None]:
Expand Down Expand Up @@ -7156,6 +7155,43 @@ def shape(self) -> Shape:
return (len(self),)


def maybe_sequence_to_range(sequence) -> Any | range:
"""
Convert a 1D sequence to a range if possible.

Returns the input if not possible.

Parameters
----------
sequence : 1D sequence
names : sequence of str

Returns
-------
Any : input or range
"""
if hasattr(sequence, "dtype") and not isinstance(sequence, np.ndarray):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the duck typing here somewhat non-obvious - maybe comments will help? Is this first branch supposed to be for extension types?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this first branch supposed to be for extension types?

Essentially yes, I think I can use a more specific check here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the check here to be more explicit (exclude Series and Index objects)

return sequence
np_sequence = np.asarray(sequence)
if np_sequence.dtype.kind != "i" or len(sequence) == 1:
return sequence
elif len(sequence) == 0:
return range(0)
diff = np_sequence[1] - np_sequence[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are only the first two elements important here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're taking an example diff here (just between the first 2 elements) and later seeing if the rest of the diffs between the rest of the elements match

if isna(diff) or diff == 0:
return sequence
elif len(sequence) == 2:
return range(sequence[0], sequence[1] + diff, diff)
maybe_range_indexer, remainder = np.divmod(np_sequence - np_sequence[0], diff)
if (
lib.is_range_indexer(maybe_range_indexer, len(maybe_range_indexer))
and not remainder.any()
):
return range(sequence[0], sequence[-1] + diff, diff)
else:
return sequence


def ensure_index_from_sequences(sequences, names=None) -> Index:
"""
Construct an index from sequences of data.
Expand All @@ -7174,8 +7210,8 @@ def ensure_index_from_sequences(sequences, names=None) -> Index:

Examples
--------
>>> ensure_index_from_sequences([[1, 2, 3]], names=["name"])
Index([1, 2, 3], dtype='int64', name='name')
>>> ensure_index_from_sequences([[1, 2, 4]], names=["name"])
Index([1, 2, 4], dtype='int64', name='name')

>>> ensure_index_from_sequences([["a", "a"], ["a", "b"]], names=["L1", "L2"])
MultiIndex([('a', 'a'),
Expand All @@ -7191,8 +7227,9 @@ def ensure_index_from_sequences(sequences, names=None) -> Index:
if len(sequences) == 1:
if names is not None:
names = names[0]
return Index(sequences[0], name=names)
return Index(maybe_sequence_to_range(sequences[0]), name=names)
else:
# TODO: Apply maybe_sequence_to_range to sequences?
return MultiIndex.from_arrays(sequences, names=names)


Expand Down
28 changes: 16 additions & 12 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
doc,
)

from pandas.core.dtypes import missing
from pandas.core.dtypes.base import ExtensionDtype
from pandas.core.dtypes.common import (
ensure_platform_int,
Expand Down Expand Up @@ -472,18 +471,16 @@ def _shallow_copy(self, values, name: Hashable = no_default):

if values.dtype.kind == "f":
return Index(values, name=name, dtype=np.float64)
if values.dtype.kind == "i" and values.ndim == 1 and len(values) > 1:
if values.dtype.kind == "i" and values.ndim == 1:
# GH 46675 & 43885: If values is equally spaced, return a
# more memory-compact RangeIndex instead of Index with 64-bit dtype
diff = values[1] - values[0]
if not missing.isna(diff) and diff != 0:
maybe_range_indexer, remainder = np.divmod(values - values[0], diff)
if (
lib.is_range_indexer(maybe_range_indexer, len(maybe_range_indexer))
and not remainder.any()
):
new_range = range(values[0], values[-1] + diff, diff)
return type(self)._simple_new(new_range, name=name)
if len(values) == 1:
start = values[0]
new_range = range(start, start + self.step, self.step)
return type(self)._simple_new(new_range, name=name)
maybe_range = ibase.maybe_sequence_to_range(values)
if isinstance(maybe_range, range):
return type(self)._simple_new(maybe_range, name=name)
return self._constructor._simple_new(values, name=name)

def _view(self) -> Self:
Expand Down Expand Up @@ -894,12 +891,19 @@ def symmetric_difference(
result = result.rename(result_name)
return result

def _join_empty(
self, other: Index, how: JoinHow, sort: bool
) -> tuple[Index, npt.NDArray[np.intp] | None, npt.NDArray[np.intp] | None]:
if other.dtype.kind == "i":
other = self._shallow_copy(other._values, name=other.name)
return super()._join_empty(other, how=how, sort=sort)

def _join_monotonic(
self, other: Index, how: JoinHow = "left"
) -> tuple[Index, npt.NDArray[np.intp] | None, npt.NDArray[np.intp] | None]:
# This currently only gets called for the monotonic increasing case
if not isinstance(other, type(self)):
maybe_ri = self._shallow_copy(other._values)
maybe_ri = self._shallow_copy(other._values, name=other.name)
if not isinstance(maybe_ri, type(self)):
return super()._join_monotonic(other, how=how)
other = maybe_ri
Expand Down
8 changes: 7 additions & 1 deletion pandas/tests/indexes/ranges/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,15 @@ def test_join_self(self, join_type):
[-1, -1, 0, 1],
"outer",
],
[RangeIndex(2), RangeIndex(0), RangeIndex(2), None, [-1, -1], "left"],
[RangeIndex(2), RangeIndex(0), RangeIndex(0), [], None, "right"],
[RangeIndex(2), RangeIndex(0), RangeIndex(0), [], None, "inner"],
[RangeIndex(2), RangeIndex(0), RangeIndex(2), None, [-1, -1], "outer"],
],
)
@pytest.mark.parametrize("right_type", [RangeIndex, lambda x: Index(list(x))])
@pytest.mark.parametrize(
"right_type", [RangeIndex, lambda x: Index(list(x), dtype=x.dtype)]
)
def test_join_preserves_rangeindex(
left, right, expected, expected_lidx, expected_ridx, how, right_type
):
Expand Down
35 changes: 35 additions & 0 deletions pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,26 @@ def test_range_index_rsub_by_const(self):
tm.assert_index_equal(result, expected)


def test_reindex_1_value_returns_rangeindex():
ri = RangeIndex(0, 10, 2, name="foo")
result, result_indexer = ri.reindex([2])
expected = RangeIndex(2, 4, 2, name="foo")
tm.assert_index_equal(result, expected, exact=True)

expected_indexer = np.array([1], dtype=np.intp)
tm.assert_numpy_array_equal(result_indexer, expected_indexer)


def test_reindex_empty_returns_rangeindex():
ri = RangeIndex(0, 10, 2, name="foo")
result, result_indexer = ri.reindex([])
expected = RangeIndex(0, 0, 2, name="foo")
tm.assert_index_equal(result, expected, exact=True)

expected_indexer = np.array([], dtype=np.intp)
tm.assert_numpy_array_equal(result_indexer, expected_indexer)


def test_append_non_rangeindex_return_rangeindex():
ri = RangeIndex(1)
result = ri.append(Index([1]))
Expand Down Expand Up @@ -653,6 +673,21 @@ def test_take_return_rangeindex():
tm.assert_index_equal(result, expected, exact=True)


@pytest.mark.parametrize(
"rng, exp_rng",
[
[range(5), range(3, 4)],
[range(0, -10, -2), range(-6, -8, -2)],
[range(0, 10, 2), range(6, 8, 2)],
],
)
def test_take_1_value_returns_rangeindex(rng, exp_rng):
ri = RangeIndex(rng, name="foo")
result = ri.take([3])
expected = RangeIndex(exp_rng, name="foo")
tm.assert_index_equal(result, expected, exact=True)


def test_append_one_nonempty_preserve_step():
expected = RangeIndex(0, -1, -1)
result = RangeIndex(0).append([expected])
Expand Down
8 changes: 5 additions & 3 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1514,8 +1514,10 @@ class TestIndexUtils:
@pytest.mark.parametrize(
"data, names, expected",
[
([[1, 2, 3]], None, Index([1, 2, 3])),
([[1, 2, 3]], ["name"], Index([1, 2, 3], name="name")),
([[1, 2, 4]], None, Index([1, 2, 4])),
([[1, 2, 4]], ["name"], Index([1, 2, 4], name="name")),
([[1, 2, 3]], None, RangeIndex(1, 4)),
([[1, 2, 3]], ["name"], RangeIndex(1, 4, name="name")),
(
[["a", "a"], ["c", "d"]],
None,
Expand All @@ -1530,7 +1532,7 @@ class TestIndexUtils:
)
def test_ensure_index_from_sequences(self, data, names, expected):
result = ensure_index_from_sequences(data, names)
tm.assert_index_equal(result, expected)
tm.assert_index_equal(result, expected, exact=True)

def test_ensure_index_mixed_closed_intervals(self):
# GH27172
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ def test_loc_getitem_list_with_fail(self):

s.loc[[2]]

msg = f"\"None of [Index([3], dtype='{np.dtype(int)}')] are in the [index]"
msg = "None of [RangeIndex(start=3, stop=4, step=1)] are in the [index]"
with pytest.raises(KeyError, match=re.escape(msg)):
s.loc[[3]]

Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/io/pytables/test_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,8 @@ def test_append_to_multiple_min_itemsize(setup_path):
}
)
expected = df.iloc[[0]]
# Reading/writing RangeIndex info is not supported yet
expected.index = Index(list(range(len(expected.index))))

with ensure_clean_store(setup_path) as store:
store.append_to_multiple(
Expand Down