Skip to content

Commit a254bcf

Browse files
authored
DEPR: Remove NumericIndex name in repr output (#51068)
* DEPR: Remove NumericIndex name in repr output * fix error * fix error II
1 parent 5467d5b commit a254bcf

File tree

9 files changed

+60
-58
lines changed

9 files changed

+60
-58
lines changed

pandas/core/arrays/timedeltas.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -769,8 +769,7 @@ def total_seconds(self) -> npt.NDArray[np.float64]:
769769
dtype='timedelta64[ns]', freq=None)
770770
771771
>>> idx.total_seconds()
772-
NumericIndex([0.0, 86400.0, 172800.0, 259200.0, 345600.0],
773-
dtype='float64')
772+
Index([0.0, 86400.0, 172800.0, 259200.0, 345600.0], dtype='float64')
774773
"""
775774
pps = periods_per_second(self._creso)
776775
return self._maybe_mask_results(self.asi8 / pps, fill_value=None)

pandas/core/indexes/base.py

+26-22
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ class Index(IndexOpsMixin, PandasObject):
320320
Examples
321321
--------
322322
>>> pd.Index([1, 2, 3])
323-
NumericIndex([1, 2, 3], dtype='int64')
323+
Index([1, 2, 3], dtype='int64')
324324
325325
>>> pd.Index(list('abc'))
326326
Index(['a', 'b', 'c'], dtype='object')
@@ -1194,6 +1194,10 @@ def __repr__(self) -> str_t:
11941194
Return a string representation for this object.
11951195
"""
11961196
klass_name = type(self).__name__
1197+
from pandas.core.indexes.numeric import NumericIndex
1198+
1199+
if type(self) is NumericIndex:
1200+
klass_name = "Index"
11971201
data = self._format_data()
11981202
attrs = self._format_attrs()
11991203
space = self._format_space()
@@ -1717,9 +1721,9 @@ def set_names(
17171721
--------
17181722
>>> idx = pd.Index([1, 2, 3, 4])
17191723
>>> idx
1720-
NumericIndex([1, 2, 3, 4], dtype='int64')
1724+
Index([1, 2, 3, 4], dtype='int64')
17211725
>>> idx.set_names('quarter')
1722-
NumericIndex([1, 2, 3, 4], dtype='int64', name='quarter')
1726+
Index([1, 2, 3, 4], dtype='int64', name='quarter')
17231727
17241728
>>> idx = pd.MultiIndex.from_product([['python', 'cobra'],
17251729
... [2018, 2019]])
@@ -2005,7 +2009,7 @@ def droplevel(self, level: IndexLabel = 0):
20052009
names=['x', 'y'])
20062010
20072011
>>> mi.droplevel(['x', 'y'])
2008-
NumericIndex([5, 6], dtype='int64', name='z')
2012+
Index([5, 6], dtype='int64', name='z')
20092013
"""
20102014
if not isinstance(level, (tuple, list)):
20112015
level = [level]
@@ -2714,7 +2718,7 @@ def isna(self) -> npt.NDArray[np.bool_]:
27142718
27152719
>>> idx = pd.Index([5.2, 6.0, np.NaN])
27162720
>>> idx
2717-
NumericIndex([5.2, 6.0, nan], dtype='float64')
2721+
Index([5.2, 6.0, nan], dtype='float64')
27182722
>>> idx.isna()
27192723
array([False, False, True])
27202724
@@ -2771,7 +2775,7 @@ def notna(self) -> npt.NDArray[np.bool_]:
27712775
27722776
>>> idx = pd.Index([5.2, 6.0, np.NaN])
27732777
>>> idx
2774-
NumericIndex([5.2, 6.0, nan], dtype='float64')
2778+
Index([5.2, 6.0, nan], dtype='float64')
27752779
>>> idx.notna()
27762780
array([ True, True, False])
27772781
@@ -3082,7 +3086,7 @@ def union(self, other, sort=None):
30823086
>>> idx1 = pd.Index([1, 2, 3, 4])
30833087
>>> idx2 = pd.Index([3, 4, 5, 6])
30843088
>>> idx1.union(idx2)
3085-
NumericIndex([1, 2, 3, 4, 5, 6], dtype='int64')
3089+
Index([1, 2, 3, 4, 5, 6], dtype='int64')
30863090
30873091
Union mismatched dtypes
30883092
@@ -3274,7 +3278,7 @@ def intersection(self, other, sort: bool = False):
32743278
>>> idx1 = pd.Index([1, 2, 3, 4])
32753279
>>> idx2 = pd.Index([3, 4, 5, 6])
32763280
>>> idx1.intersection(idx2)
3277-
NumericIndex([3, 4], dtype='int64')
3281+
Index([3, 4], dtype='int64')
32783282
"""
32793283
self._validate_sort_keyword(sort)
32803284
self._assert_can_do_setop(other)
@@ -3421,9 +3425,9 @@ def difference(self, other, sort=None):
34213425
>>> idx1 = pd.Index([2, 1, 3, 4])
34223426
>>> idx2 = pd.Index([3, 4, 5, 6])
34233427
>>> idx1.difference(idx2)
3424-
NumericIndex([1, 2], dtype='int64')
3428+
Index([1, 2], dtype='int64')
34253429
>>> idx1.difference(idx2, sort=False)
3426-
NumericIndex([2, 1], dtype='int64')
3430+
Index([2, 1], dtype='int64')
34273431
"""
34283432
self._validate_sort_keyword(sort)
34293433
self._assert_can_do_setop(other)
@@ -3504,7 +3508,7 @@ def symmetric_difference(self, other, result_name=None, sort=None):
35043508
>>> idx1 = pd.Index([1, 2, 3, 4])
35053509
>>> idx2 = pd.Index([2, 3, 4, 5])
35063510
>>> idx1.symmetric_difference(idx2)
3507-
NumericIndex([1, 5], dtype='int64')
3511+
Index([1, 5], dtype='int64')
35083512
"""
35093513
self._validate_sort_keyword(sort)
35103514
self._assert_can_do_setop(other)
@@ -5069,7 +5073,7 @@ def __contains__(self, key: Any) -> bool:
50695073
--------
50705074
>>> idx = pd.Index([1, 2, 3, 4])
50715075
>>> idx
5072-
NumericIndex([1, 2, 3, 4], dtype='int64')
5076+
Index([1, 2, 3, 4], dtype='int64')
50735077
50745078
>>> 2 in idx
50755079
True
@@ -5268,7 +5272,7 @@ def equals(self, other: Any) -> bool:
52685272
--------
52695273
>>> idx1 = pd.Index([1, 2, 3])
52705274
>>> idx1
5271-
NumericIndex([1, 2, 3], dtype='int64')
5275+
Index([1, 2, 3], dtype='int64')
52725276
>>> idx1.equals(pd.Index([1, 2, 3]))
52735277
True
52745278
@@ -5285,21 +5289,21 @@ def equals(self, other: Any) -> bool:
52855289
52865290
>>> ascending_idx = pd.Index([1, 2, 3])
52875291
>>> ascending_idx
5288-
NumericIndex([1, 2, 3], dtype='int64')
5292+
Index([1, 2, 3], dtype='int64')
52895293
>>> descending_idx = pd.Index([3, 2, 1])
52905294
>>> descending_idx
5291-
NumericIndex([3, 2, 1], dtype='int64')
5295+
Index([3, 2, 1], dtype='int64')
52925296
>>> ascending_idx.equals(descending_idx)
52935297
False
52945298
52955299
The dtype is *not* compared
52965300
52975301
>>> int64_idx = pd.Index([1, 2, 3], dtype='int64')
52985302
>>> int64_idx
5299-
NumericIndex([1, 2, 3], dtype='int64')
5303+
Index([1, 2, 3], dtype='int64')
53005304
>>> uint64_idx = pd.Index([1, 2, 3], dtype='uint64')
53015305
>>> uint64_idx
5302-
NumericIndex([1, 2, 3], dtype='uint64')
5306+
Index([1, 2, 3], dtype='uint64')
53035307
>>> int64_idx.equals(uint64_idx)
53045308
True
53055309
"""
@@ -5522,18 +5526,18 @@ def sort_values(
55225526
--------
55235527
>>> idx = pd.Index([10, 100, 1, 1000])
55245528
>>> idx
5525-
NumericIndex([10, 100, 1, 1000], dtype='int64')
5529+
Index([10, 100, 1, 1000], dtype='int64')
55265530
55275531
Sort values in ascending order (default behavior).
55285532
55295533
>>> idx.sort_values()
5530-
NumericIndex([1, 10, 100, 1000], dtype='int64')
5534+
Index([1, 10, 100, 1000], dtype='int64')
55315535
55325536
Sort values in descending order, and also get the indices `idx` was
55335537
sorted by.
55345538
55355539
>>> idx.sort_values(ascending=False, return_indexer=True)
5536-
(NumericIndex([1000, 100, 10, 1], dtype='int64'), array([3, 1, 0, 2]))
5540+
(Index([1000, 100, 10, 1], dtype='int64'), array([3, 1, 0, 2]))
55375541
"""
55385542
idx = ensure_key_mapped(self, key)
55395543

@@ -6180,7 +6184,7 @@ def isin(self, values, level=None) -> npt.NDArray[np.bool_]:
61806184
--------
61816185
>>> idx = pd.Index([1,2,3])
61826186
>>> idx
6183-
NumericIndex([1, 2, 3], dtype='int64')
6187+
Index([1, 2, 3], dtype='int64')
61846188
61856189
Check whether each index value in a list of values.
61866190
@@ -6977,7 +6981,7 @@ def ensure_index_from_sequences(sequences, names=None) -> Index:
69776981
Examples
69786982
--------
69796983
>>> ensure_index_from_sequences([[1, 2, 3]], names=["name"])
6980-
NumericIndex([1, 2, 3], dtype='int64', name='name')
6984+
Index([1, 2, 3], dtype='int64', name='name')
69816985
69826986
>>> ensure_index_from_sequences([["a", "a"], ["a", "b"]], names=["L1", "L2"])
69836987
MultiIndex([('a', 'a'),

pandas/core/indexes/multi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ def get_level_values(self, level):
16421642
level_1 int64
16431643
dtype: object
16441644
>>> pd.MultiIndex.from_arrays([[1, None, 2], [3, 4, 5]]).get_level_values(0)
1645-
NumericIndex([1.0, nan, 2.0], dtype='float64')
1645+
Index([1.0, nan, 2.0], dtype='float64')
16461646
"""
16471647
level = self._get_level_number(level)
16481648
values = self._get_level_values(level)

pandas/core/strings/accessor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2247,7 +2247,7 @@ def count(self, pat, flags: int = 0):
22472247
This is also available on Index
22482248
22492249
>>> pd.Index(['A', 'A', 'Aaba', 'cat']).str.count('a')
2250-
NumericIndex([0, 0, 2, 1], dtype='int64')
2250+
Index([0, 0, 2, 1], dtype='int64')
22512251
"""
22522252
result = self._data.array._str_count(pat, flags)
22532253
return self._wrap_result(result, returns_string=False)

pandas/tests/indexes/test_any_index.py

+2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ def test_str(self, index):
154154
# test the string repr
155155
index.name = "foo"
156156
assert "'foo'" in str(index)
157+
if type(index).__name__ == "NumericIndex": # TODO: remove NumericIndex
158+
return
157159
assert type(index).__name__ in str(index)
158160

159161

pandas/tests/indexing/test_loc.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def test_loc_to_fail(self):
439439
)
440440

441441
msg = (
442-
rf"\"None of \[NumericIndex\(\[1, 2\], dtype='{np.int_().dtype}'\)\] are "
442+
rf"\"None of \[Index\(\[1, 2\], dtype='{np.int_().dtype}'\)\] are "
443443
r"in the \[index\]\""
444444
)
445445
with pytest.raises(KeyError, match=msg):
@@ -457,7 +457,7 @@ def test_loc_to_fail2(self):
457457
s.loc[-1]
458458

459459
msg = (
460-
rf"\"None of \[NumericIndex\(\[-1, -2\], dtype='{np.int_().dtype}'\)\] are "
460+
rf"\"None of \[Index\(\[-1, -2\], dtype='{np.int_().dtype}'\)\] are "
461461
r"in the \[index\]\""
462462
)
463463
with pytest.raises(KeyError, match=msg):
@@ -473,7 +473,7 @@ def test_loc_to_fail2(self):
473473

474474
s["a"] = 2
475475
msg = (
476-
rf"\"None of \[NumericIndex\(\[-2\], dtype='{np.int_().dtype}'\)\] are "
476+
rf"\"None of \[Index\(\[-2\], dtype='{np.int_().dtype}'\)\] are "
477477
r"in the \[index\]\""
478478
)
479479
with pytest.raises(KeyError, match=msg):
@@ -490,7 +490,7 @@ def test_loc_to_fail3(self):
490490
df = DataFrame([["a"], ["b"]], index=[1, 2], columns=["value"])
491491

492492
msg = (
493-
rf"\"None of \[NumericIndex\(\[3\], dtype='{np.int_().dtype}'\)\] are "
493+
rf"\"None of \[Index\(\[3\], dtype='{np.int_().dtype}'\)\] are "
494494
r"in the \[index\]\""
495495
)
496496
with pytest.raises(KeyError, match=msg):
@@ -507,10 +507,7 @@ def test_loc_getitem_list_with_fail(self):
507507

508508
s.loc[[2]]
509509

510-
msg = (
511-
f"\"None of [NumericIndex([3], dtype='{np.int_().dtype}')] "
512-
'are in the [index]"'
513-
)
510+
msg = f"\"None of [Index([3], dtype='{np.int_().dtype}')] are in the [index]"
514511
with pytest.raises(KeyError, match=re.escape(msg)):
515512
s.loc[[3]]
516513

@@ -1201,7 +1198,7 @@ def test_loc_setitem_empty_append_raises(self):
12011198
df = DataFrame(columns=["x", "y"])
12021199
df.index = df.index.astype(np.int64)
12031200
msg = (
1204-
rf"None of \[NumericIndex\(\[0, 1\], dtype='{np.int_().dtype}'\)\] "
1201+
rf"None of \[Index\(\[0, 1\], dtype='{np.int_().dtype}'\)\] "
12051202
r"are in the \[index\]"
12061203
)
12071204
with pytest.raises(KeyError, match=msg):

pandas/tests/indexing/test_partial.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def test_series_partial_set(self):
403403

404404
# raises as nothing is in the index
405405
msg = (
406-
rf"\"None of \[NumericIndex\(\[3, 3, 3\], dtype='{np.int_().dtype}'\)\] "
406+
rf"\"None of \[Index\(\[3, 3, 3\], dtype='{np.int_().dtype}'\)\] "
407407
r"are in the \[index\]\""
408408
)
409409
with pytest.raises(KeyError, match=msg):
@@ -484,7 +484,7 @@ def test_series_partial_set_with_name(self):
484484

485485
# raises as nothing is in the index
486486
msg = (
487-
rf"\"None of \[NumericIndex\(\[3, 3, 3\], dtype='{np.int_().dtype}', "
487+
rf"\"None of \[Index\(\[3, 3, 3\], dtype='{np.int_().dtype}', "
488488
r"name='idx'\)\] are in the \[index\]\""
489489
)
490490
with pytest.raises(KeyError, match=msg):

pandas/tests/util/test_assert_categorical_equal.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def test_categorical_equal_order_mismatch(check_category_order):
2222
msg = """Categorical\\.categories are different
2323
2424
Categorical\\.categories values are different \\(100\\.0 %\\)
25-
\\[left\\]: NumericIndex\\(\\[1, 2, 3, 4\\], dtype='int64'\\)
26-
\\[right\\]: NumericIndex\\(\\[4, 3, 2, 1\\], dtype='int64'\\)"""
25+
\\[left\\]: Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)
26+
\\[right\\]: Index\\(\\[4, 3, 2, 1\\], dtype='int64'\\)"""
2727
with pytest.raises(AssertionError, match=msg):
2828
tm.assert_categorical_equal(c1, c2, **kwargs)
2929
else:
@@ -34,8 +34,8 @@ def test_categorical_equal_categories_mismatch():
3434
msg = """Categorical\\.categories are different
3535
3636
Categorical\\.categories values are different \\(25\\.0 %\\)
37-
\\[left\\]: NumericIndex\\(\\[1, 2, 3, 4\\], dtype='int64'\\)
38-
\\[right\\]: NumericIndex\\(\\[1, 2, 3, 5\\], dtype='int64'\\)"""
37+
\\[left\\]: Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)
38+
\\[right\\]: Index\\(\\[1, 2, 3, 5\\], dtype='int64'\\)"""
3939

4040
c1 = Categorical([1, 2, 3, 4])
4141
c2 = Categorical([1, 2, 3, 5])

0 commit comments

Comments
 (0)