Skip to content

Commit 981e3c9

Browse files
committed
Merge remote-tracking branch 'upstream/main' into ref/is_range_indexer/step
2 parents 0b484bd + 3132971 commit 981e3c9

File tree

6 files changed

+72
-5
lines changed

6 files changed

+72
-5
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ Performance improvements
274274
- Performance improvement in :meth:`MultiIndex.equals` for equal length indexes (:issue:`56990`)
275275
- Performance improvement in :meth:`RangeIndex.__getitem__` with a boolean mask or integers returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57588`)
276276
- Performance improvement in :meth:`RangeIndex.append` when appending the same index (:issue:`57252`)
277+
- Performance improvement in :meth:`RangeIndex.round` returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57824`)
277278
- Performance improvement in :meth:`RangeIndex.join` returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57651`, :issue:`57752`)
278279
- Performance improvement in :meth:`RangeIndex.reindex` returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57647`, :issue:`57752`)
279280
- Performance improvement in :meth:`RangeIndex.take` returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57445`, :issue:`57752`)

environment.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ dependencies:
6060
- zstandard>=0.19.0
6161

6262
# downstream packages
63-
- dask-core
63+
- dask-core<=2024.2.1
6464
- seaborn-base
65-
- dask-expr
65+
- dask-expr<=0.5.3
6666

6767
# local testing dependencies
6868
- moto

pandas/core/indexes/base.py

-1
Original file line numberDiff line numberDiff line change
@@ -6737,7 +6737,6 @@ def diff(self, periods: int = 1) -> Index:
67376737
"""
67386738
return Index(self.to_series().diff(periods))
67396739

6740-
@final
67416740
def round(self, decimals: int = 0) -> Self:
67426741
"""
67436742
Round each value in the Index to the given number of decimals.

pandas/core/indexes/range.py

+36
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,42 @@ def any(self, *args, **kwargs) -> bool:
11531153

11541154
# --------------------------------------------------------------------
11551155

1156+
# error: Return type "RangeIndex | Index" of "round" incompatible with
1157+
# return type "RangeIndex" in supertype "Index"
1158+
def round(self, decimals: int = 0) -> Self | Index: # type: ignore[override]
1159+
"""
1160+
Round each value in the Index to the given number of decimals.
1161+
1162+
Parameters
1163+
----------
1164+
decimals : int, optional
1165+
Number of decimal places to round to. If decimals is negative,
1166+
it specifies the number of positions to the left of the decimal point
1167+
e.g. ``round(11.0, -1) == 10.0``.
1168+
1169+
Returns
1170+
-------
1171+
Index or RangeIndex
1172+
A new Index with the rounded values.
1173+
1174+
Examples
1175+
--------
1176+
>>> import pandas as pd
1177+
>>> idx = pd.RangeIndex(10, 30, 10)
1178+
>>> idx.round(decimals=-1)
1179+
RangeIndex(start=10, stop=30, step=10)
1180+
>>> idx = pd.RangeIndex(10, 15, 1)
1181+
>>> idx.round(decimals=-1)
1182+
Index([10, 10, 10, 10, 10], dtype='int64')
1183+
"""
1184+
if decimals >= 0:
1185+
return self.copy()
1186+
elif self.start % 10**-decimals == 0 and self.step % 10**-decimals == 0:
1187+
# e.g. RangeIndex(10, 30, 10).round(-1) doesn't need rounding
1188+
return self.copy()
1189+
else:
1190+
return super().round(decimals=decimals)
1191+
11561192
def _cmp_method(self, other, op):
11571193
if isinstance(other, RangeIndex) and self._range == other._range:
11581194
# Both are immutable so if ._range attr. are equal, shortcut is possible

pandas/tests/indexes/ranges/test_range.py

+31
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,37 @@ def test_range_index_rsub_by_const(self):
608608
tm.assert_index_equal(result, expected)
609609

610610

611+
@pytest.mark.parametrize(
612+
"rng, decimals",
613+
[
614+
[range(5), 0],
615+
[range(5), 2],
616+
[range(10, 30, 10), -1],
617+
[range(30, 10, -10), -1],
618+
],
619+
)
620+
def test_range_round_returns_rangeindex(rng, decimals):
621+
ri = RangeIndex(rng)
622+
expected = ri.copy()
623+
result = ri.round(decimals=decimals)
624+
tm.assert_index_equal(result, expected, exact=True)
625+
626+
627+
@pytest.mark.parametrize(
628+
"rng, decimals",
629+
[
630+
[range(10, 30, 1), -1],
631+
[range(30, 10, -1), -1],
632+
[range(11, 14), -10],
633+
],
634+
)
635+
def test_range_round_returns_index(rng, decimals):
636+
ri = RangeIndex(rng)
637+
expected = Index(list(rng)).round(decimals=decimals)
638+
result = ri.round(decimals=decimals)
639+
tm.assert_index_equal(result, expected, exact=True)
640+
641+
611642
def test_reindex_1_value_returns_rangeindex():
612643
ri = RangeIndex(0, 10, 2, name="foo")
613644
result, result_indexer = ri.reindex([2])

requirements-dev.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ xarray>=2022.12.0
4747
xlrd>=2.0.1
4848
xlsxwriter>=3.0.5
4949
zstandard>=0.19.0
50-
dask
50+
dask<=2024.2.1
5151
seaborn
52-
dask-expr
52+
dask-expr<=0.5.3
5353
moto
5454
flask
5555
asv>=0.6.1

0 commit comments

Comments
 (0)