Skip to content
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

DEPR: Remove NumericIndex name in repr output #51068

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,8 +769,7 @@ def total_seconds(self) -> npt.NDArray[np.float64]:
dtype='timedelta64[ns]', freq=None)

>>> idx.total_seconds()
NumericIndex([0.0, 86400.0, 172800.0, 259200.0, 345600.0],
dtype='float64')
Index([0.0, 86400.0, 172800.0, 259200.0, 345600.0], dtype='float64')
"""
pps = periods_per_second(self._creso)
return self._maybe_mask_results(self.asi8 / pps, fill_value=None)
Expand Down
48 changes: 26 additions & 22 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ class Index(IndexOpsMixin, PandasObject):
Examples
--------
>>> pd.Index([1, 2, 3])
NumericIndex([1, 2, 3], dtype='int64')
Index([1, 2, 3], dtype='int64')

>>> pd.Index(list('abc'))
Index(['a', 'b', 'c'], dtype='object')
Expand Down Expand Up @@ -1194,6 +1194,10 @@ def __repr__(self) -> str_t:
Return a string representation for this object.
"""
klass_name = type(self).__name__
from pandas.core.indexes.numeric import NumericIndex

if type(self) is NumericIndex:
klass_name = "Index"
data = self._format_data()
attrs = self._format_attrs()
space = self._format_space()
Expand Down Expand Up @@ -1716,9 +1720,9 @@ def set_names(
--------
>>> idx = pd.Index([1, 2, 3, 4])
>>> idx
NumericIndex([1, 2, 3, 4], dtype='int64')
Index([1, 2, 3, 4], dtype='int64')
>>> idx.set_names('quarter')
NumericIndex([1, 2, 3, 4], dtype='int64', name='quarter')
Index([1, 2, 3, 4], dtype='int64', name='quarter')

>>> idx = pd.MultiIndex.from_product([['python', 'cobra'],
... [2018, 2019]])
Expand Down Expand Up @@ -2004,7 +2008,7 @@ def droplevel(self, level: IndexLabel = 0):
names=['x', 'y'])

>>> mi.droplevel(['x', 'y'])
NumericIndex([5, 6], dtype='int64', name='z')
Index([5, 6], dtype='int64', name='z')
"""
if not isinstance(level, (tuple, list)):
level = [level]
Expand Down Expand Up @@ -2713,7 +2717,7 @@ def isna(self) -> npt.NDArray[np.bool_]:

>>> idx = pd.Index([5.2, 6.0, np.NaN])
>>> idx
NumericIndex([5.2, 6.0, nan], dtype='float64')
Index([5.2, 6.0, nan], dtype='float64')
>>> idx.isna()
array([False, False, True])

Expand Down Expand Up @@ -2770,7 +2774,7 @@ def notna(self) -> npt.NDArray[np.bool_]:

>>> idx = pd.Index([5.2, 6.0, np.NaN])
>>> idx
NumericIndex([5.2, 6.0, nan], dtype='float64')
Index([5.2, 6.0, nan], dtype='float64')
>>> idx.notna()
array([ True, True, False])

Expand Down Expand Up @@ -3081,7 +3085,7 @@ def union(self, other, sort=None):
>>> idx1 = pd.Index([1, 2, 3, 4])
>>> idx2 = pd.Index([3, 4, 5, 6])
>>> idx1.union(idx2)
NumericIndex([1, 2, 3, 4, 5, 6], dtype='int64')
Index([1, 2, 3, 4, 5, 6], dtype='int64')

Union mismatched dtypes

Expand Down Expand Up @@ -3273,7 +3277,7 @@ def intersection(self, other, sort: bool = False):
>>> idx1 = pd.Index([1, 2, 3, 4])
>>> idx2 = pd.Index([3, 4, 5, 6])
>>> idx1.intersection(idx2)
NumericIndex([3, 4], dtype='int64')
Index([3, 4], dtype='int64')
"""
self._validate_sort_keyword(sort)
self._assert_can_do_setop(other)
Expand Down Expand Up @@ -3420,9 +3424,9 @@ def difference(self, other, sort=None):
>>> idx1 = pd.Index([2, 1, 3, 4])
>>> idx2 = pd.Index([3, 4, 5, 6])
>>> idx1.difference(idx2)
NumericIndex([1, 2], dtype='int64')
Index([1, 2], dtype='int64')
>>> idx1.difference(idx2, sort=False)
NumericIndex([2, 1], dtype='int64')
Index([2, 1], dtype='int64')
"""
self._validate_sort_keyword(sort)
self._assert_can_do_setop(other)
Expand Down Expand Up @@ -3503,7 +3507,7 @@ def symmetric_difference(self, other, result_name=None, sort=None):
>>> idx1 = pd.Index([1, 2, 3, 4])
>>> idx2 = pd.Index([2, 3, 4, 5])
>>> idx1.symmetric_difference(idx2)
NumericIndex([1, 5], dtype='int64')
Index([1, 5], dtype='int64')
"""
self._validate_sort_keyword(sort)
self._assert_can_do_setop(other)
Expand Down Expand Up @@ -5068,7 +5072,7 @@ def __contains__(self, key: Any) -> bool:
--------
>>> idx = pd.Index([1, 2, 3, 4])
>>> idx
NumericIndex([1, 2, 3, 4], dtype='int64')
Index([1, 2, 3, 4], dtype='int64')

>>> 2 in idx
True
Expand Down Expand Up @@ -5266,7 +5270,7 @@ def equals(self, other: Any) -> bool:
--------
>>> idx1 = pd.Index([1, 2, 3])
>>> idx1
NumericIndex([1, 2, 3], dtype='int64')
Index([1, 2, 3], dtype='int64')
>>> idx1.equals(pd.Index([1, 2, 3]))
True

Expand All @@ -5283,21 +5287,21 @@ def equals(self, other: Any) -> bool:

>>> ascending_idx = pd.Index([1, 2, 3])
>>> ascending_idx
NumericIndex([1, 2, 3], dtype='int64')
Index([1, 2, 3], dtype='int64')
>>> descending_idx = pd.Index([3, 2, 1])
>>> descending_idx
NumericIndex([3, 2, 1], dtype='int64')
Index([3, 2, 1], dtype='int64')
>>> ascending_idx.equals(descending_idx)
False

The dtype is *not* compared

>>> int64_idx = pd.Index([1, 2, 3], dtype='int64')
>>> int64_idx
NumericIndex([1, 2, 3], dtype='int64')
Index([1, 2, 3], dtype='int64')
>>> uint64_idx = pd.Index([1, 2, 3], dtype='uint64')
>>> uint64_idx
NumericIndex([1, 2, 3], dtype='uint64')
Index([1, 2, 3], dtype='uint64')
>>> int64_idx.equals(uint64_idx)
True
"""
Expand Down Expand Up @@ -5520,18 +5524,18 @@ def sort_values(
--------
>>> idx = pd.Index([10, 100, 1, 1000])
>>> idx
NumericIndex([10, 100, 1, 1000], dtype='int64')
Index([10, 100, 1, 1000], dtype='int64')

Sort values in ascending order (default behavior).

>>> idx.sort_values()
NumericIndex([1, 10, 100, 1000], dtype='int64')
Index([1, 10, 100, 1000], dtype='int64')

Sort values in descending order, and also get the indices `idx` was
sorted by.

>>> idx.sort_values(ascending=False, return_indexer=True)
(NumericIndex([1000, 100, 10, 1], dtype='int64'), array([3, 1, 0, 2]))
(Index([1000, 100, 10, 1], dtype='int64'), array([3, 1, 0, 2]))
"""
idx = ensure_key_mapped(self, key)

Expand Down Expand Up @@ -6178,7 +6182,7 @@ def isin(self, values, level=None) -> npt.NDArray[np.bool_]:
--------
>>> idx = pd.Index([1,2,3])
>>> idx
NumericIndex([1, 2, 3], dtype='int64')
Index([1, 2, 3], dtype='int64')

Check whether each index value in a list of values.

Expand Down Expand Up @@ -6975,7 +6979,7 @@ def ensure_index_from_sequences(sequences, names=None) -> Index:
Examples
--------
>>> ensure_index_from_sequences([[1, 2, 3]], names=["name"])
NumericIndex([1, 2, 3], dtype='int64', name='name')
Index([1, 2, 3], dtype='int64', name='name')

>>> ensure_index_from_sequences([["a", "a"], ["a", "b"]], names=["L1", "L2"])
MultiIndex([('a', 'a'),
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1642,7 +1642,7 @@ def get_level_values(self, level):
level_1 int64
dtype: object
>>> pd.MultiIndex.from_arrays([[1, None, 2], [3, 4, 5]]).get_level_values(0)
NumericIndex([1.0, nan, 2.0], dtype='float64')
Index([1.0, nan, 2.0], dtype='float64')
"""
level = self._get_level_number(level)
values = self._get_level_values(level)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2247,7 +2247,7 @@ def count(self, pat, flags: int = 0):
This is also available on Index

>>> pd.Index(['A', 'A', 'Aaba', 'cat']).str.count('a')
NumericIndex([0, 0, 2, 1], dtype='int64')
Index([0, 0, 2, 1], dtype='int64')
"""
result = self._data.array._str_count(pat, flags)
return self._wrap_result(result, returns_string=False)
Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/indexes/test_any_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ def test_str(self, index):
# test the string repr
index.name = "foo"
assert "'foo'" in str(index)
if type(index).__name__ == "NumericIndex": # TODO: remove NumericIndex
return
assert type(index).__name__ in str(index)


Expand Down
15 changes: 6 additions & 9 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def test_loc_to_fail(self):
)

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

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

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

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

s.loc[[2]]

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

Expand Down Expand Up @@ -1201,7 +1198,7 @@ def test_loc_setitem_empty_append_raises(self):
df = DataFrame(columns=["x", "y"])
df.index = df.index.astype(np.int64)
msg = (
rf"None of \[NumericIndex\(\[0, 1\], dtype='{np.int_().dtype}'\)\] "
rf"None of \[Index\(\[0, 1\], dtype='{np.int_().dtype}'\)\] "
r"are in the \[index\]"
)
with pytest.raises(KeyError, match=msg):
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexing/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def test_series_partial_set(self):

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

# raises as nothing is in the index
msg = (
rf"\"None of \[NumericIndex\(\[3, 3, 3\], dtype='{np.int_().dtype}', "
rf"\"None of \[Index\(\[3, 3, 3\], dtype='{np.int_().dtype}', "
r"name='idx'\)\] are in the \[index\]\""
)
with pytest.raises(KeyError, match=msg):
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/util/test_assert_categorical_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def test_categorical_equal_order_mismatch(check_category_order):
msg = """Categorical\\.categories are different

Categorical\\.categories values are different \\(100\\.0 %\\)
\\[left\\]: NumericIndex\\(\\[1, 2, 3, 4\\], dtype='int64'\\)
\\[right\\]: NumericIndex\\(\\[4, 3, 2, 1\\], dtype='int64'\\)"""
\\[left\\]: Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)
\\[right\\]: Index\\(\\[4, 3, 2, 1\\], dtype='int64'\\)"""
with pytest.raises(AssertionError, match=msg):
tm.assert_categorical_equal(c1, c2, **kwargs)
else:
Expand All @@ -34,8 +34,8 @@ def test_categorical_equal_categories_mismatch():
msg = """Categorical\\.categories are different

Categorical\\.categories values are different \\(25\\.0 %\\)
\\[left\\]: NumericIndex\\(\\[1, 2, 3, 4\\], dtype='int64'\\)
\\[right\\]: NumericIndex\\(\\[1, 2, 3, 5\\], dtype='int64'\\)"""
\\[left\\]: Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)
\\[right\\]: Index\\(\\[1, 2, 3, 5\\], dtype='int64'\\)"""

c1 = Categorical([1, 2, 3, 4])
c2 = Categorical([1, 2, 3, 5])
Expand Down