Skip to content

Commit 169a35e

Browse files
committed
PERF: RangeIndex.format perf regression
1 parent 06f1718 commit 169a35e

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pandas/core/indexes/range.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import timedelta
22
import operator
33
from sys import getsizeof
4-
from typing import Any
4+
from typing import Any, List
55
import warnings
66

77
import numpy as np
@@ -187,6 +187,11 @@ def _format_data(self, name=None):
187187
# we are formatting thru the attributes
188188
return None
189189

190+
def _format_with_header(self, header, na_rep="NaN") -> List[str]:
191+
start_str, stop_str = str(self._range.start), str(self._range.stop)
192+
max_length = max(len(start_str), len(stop_str))
193+
return header + [f"{x:<{max_length}}" for x in self._range]
194+
190195
# --------------------------------------------------------------------
191196
_deprecation_message = (
192197
"RangeIndex.{} is deprecated and will be "

pandas/tests/indexes/ranges/test_range.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,14 @@ def test_cache(self):
171171
pass
172172
assert idx._cache == {}
173173

174+
idx.format()
175+
assert idx._cache == {}
176+
174177
df = pd.DataFrame({"a": range(10)}, index=idx)
175178

179+
str(df)
180+
assert idx._cache == {}
181+
176182
df.loc[50]
177183
assert idx._cache == {}
178184

0 commit comments

Comments
 (0)