Skip to content

Commit 7db129e

Browse files
authored
DOC: NumericIndex (#42706)
1 parent c03ee85 commit 7db129e

File tree

15 files changed

+110
-18
lines changed

15 files changed

+110
-18
lines changed

doc/source/reference/indexing.rst

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Numeric Index
170170
:toctree: api/
171171
:template: autosummary/class_without_autosummary.rst
172172

173+
NumericIndex
173174
RangeIndex
174175
Int64Index
175176
UInt64Index

doc/source/user_guide/advanced.rst

+51-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ MultiIndex / advanced indexing
77
******************************
88

99
This section covers :ref:`indexing with a MultiIndex <advanced.hierarchical>`
10-
and :ref:`other advanced indexing features <indexing.index_types>`.
10+
and :ref:`other advanced indexing features <advanced.index_types>`.
1111

1212
See the :ref:`Indexing and Selecting Data <indexing>` for general indexing documentation.
1313

@@ -738,7 +738,7 @@ faster than fancy indexing.
738738
%timeit ser.iloc[indexer]
739739
%timeit ser.take(indexer)
740740
741-
.. _indexing.index_types:
741+
.. _advanced.index_types:
742742

743743
Index types
744744
-----------
@@ -749,7 +749,7 @@ and documentation about ``TimedeltaIndex`` is found :ref:`here <timedeltas.index
749749

750750
In the following sub-sections we will highlight some other index types.
751751

752-
.. _indexing.categoricalindex:
752+
.. _advanced.categoricalindex:
753753

754754
CategoricalIndex
755755
~~~~~~~~~~~~~~~~
@@ -846,22 +846,36 @@ values **not** in the categories, similarly to how you can reindex **any** panda
846846
In [1]: pd.concat([df4, df5])
847847
TypeError: categories must match existing categories when appending
848848
849-
.. _indexing.rangeindex:
849+
.. _advanced.rangeindex:
850850

851851
Int64Index and RangeIndex
852852
~~~~~~~~~~~~~~~~~~~~~~~~~
853853

854+
.. note::
855+
856+
In pandas 2.0, :class:`NumericIndex` will become the default index type for numeric types
857+
instead of ``Int64Index``, ``Float64Index`` and ``UInt64Index`` and those index types
858+
will be removed. See :ref:`here <advanced.numericindex>` for more.
859+
``RangeIndex`` however, will not be removed, as it represents an optimized version of an integer index.
860+
854861
:class:`Int64Index` is a fundamental basic index in pandas. This is an immutable array
855862
implementing an ordered, sliceable set.
856863

857864
:class:`RangeIndex` is a sub-class of ``Int64Index`` that provides the default index for all ``NDFrame`` objects.
858865
``RangeIndex`` is an optimized version of ``Int64Index`` that can represent a monotonic ordered set. These are analogous to Python `range types <https://docs.python.org/3/library/stdtypes.html#typesseq-range>`__.
859866

860-
.. _indexing.float64index:
867+
.. _advanced.float64index:
861868

862869
Float64Index
863870
~~~~~~~~~~~~
864871

872+
.. note::
873+
874+
In pandas 2.0, :class:`NumericIndex` will become the default index type for numeric types
875+
instead of ``Int64Index``, ``Float64Index`` and ``UInt64Index`` and those index types
876+
will be removed. See :ref:`here <advanced.numericindex>` for more.
877+
``RangeIndex`` however, will not be removed, as it represents an optimized version of an integer index.
878+
865879
By default a :class:`Float64Index` will be automatically created when passing floating, or mixed-integer-floating values in index creation.
866880
This enables a pure label-based slicing paradigm that makes ``[],ix,loc`` for scalar indexing and slicing work exactly the
867881
same.
@@ -956,6 +970,38 @@ If you need integer based selection, you should use ``iloc``:
956970
957971
dfir.iloc[0:5]
958972
973+
974+
.. _advanced.numericindex:
975+
976+
NumericIndex
977+
~~~~~~~~~~~~
978+
979+
.. versionadded:: 1.4.0
980+
981+
.. note::
982+
983+
In pandas 2.0, :class:`NumericIndex` will become the default index type for numeric types
984+
instead of ``Int64Index``, ``Float64Index`` and ``UInt64Index`` and those index types
985+
will be removed.
986+
``RangeIndex`` however, will not be removed, as it represents an optimized version of an integer index.
987+
988+
:class:`NumericIndex` is an index type that can hold data of any numpy int/uint/float dtype. For example:
989+
990+
.. ipython:: python
991+
992+
idx = pd.NumericIndex([1, 2, 4, 5], dtype="int8")
993+
idx
994+
ser = pd.Series(range(4), index=idx)
995+
ser
996+
997+
``NumericIndex`` works the same way as the existing ``Int64Index``, ``Float64Index`` and
998+
``UInt64Index`` except that it can hold any numpy int, uint or float dtype.
999+
1000+
Until Pandas 2.0, you will have to call ``NumericIndex`` explicitly in order to use it, like in the example above.
1001+
In Pandas 2.0, ``NumericIndex`` will become the default pandas numeric index type and will automatically be used where appropriate.
1002+
1003+
Please notice that ``NumericIndex`` *can not* hold Pandas numeric dtypes (:class:`Int64Dtype`, :class:`Int32Dtype` etc.).
1004+
9591005
.. _advanced.intervalindex:
9601006

9611007
IntervalIndex

doc/source/user_guide/categorical.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,7 @@ Categorical index
11411141
``CategoricalIndex`` is a type of index that is useful for supporting
11421142
indexing with duplicates. This is a container around a ``Categorical``
11431143
and allows efficient indexing and storage of an index with a large number of duplicated elements.
1144-
See the :ref:`advanced indexing docs <indexing.categoricalindex>` for a more detailed
1144+
See the :ref:`advanced indexing docs <advanced.categoricalindex>` for a more detailed
11451145
explanation.
11461146

11471147
Setting the index will create a ``CategoricalIndex``:

doc/source/whatsnew/v0.13.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ Float64Index API change
310310

311311
- Added a new index type, ``Float64Index``. This will be automatically created when passing floating values in index creation.
312312
This enables a pure label-based slicing paradigm that makes ``[],ix,loc`` for scalar indexing and slicing work exactly the
313-
same. See :ref:`the docs<indexing.float64index>`, (:issue:`263`)
313+
same. See :ref:`the docs<advanced.float64index>`, (:issue:`263`)
314314

315315
Construction is by default for floating type values.
316316

doc/source/whatsnew/v0.16.1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ values NOT in the categories, similarly to how you can reindex ANY pandas index.
168168
ordered=False, name='B',
169169
dtype='category')
170170
171-
See the :ref:`documentation <indexing.categoricalindex>` for more. (:issue:`7629`, :issue:`10038`, :issue:`10039`)
171+
See the :ref:`documentation <advanced.categoricalindex>` for more. (:issue:`7629`, :issue:`10038`, :issue:`10039`)
172172

173173
.. _whatsnew_0161.enhancements.sample:
174174

doc/source/whatsnew/v1.4.0.rst

+46-3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,53 @@ including other versions of pandas.
1515
Enhancements
1616
~~~~~~~~~~~~
1717

18-
.. _whatsnew_140.enhancements.enhancement1:
18+
.. _whatsnew_140.enhancements.numeric_index:
1919

20-
enhancement1
21-
^^^^^^^^^^^^
20+
More flexible numeric dtypes for indexes
21+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
23+
Until now, it has only been possible to create numeric indexes with int64/float64/uint64 dtypes.
24+
It is now possible to create an index of any numpy int/uint/float dtype using the new :class:`NumericIndex` index type (:issue:`41153`):
25+
26+
.. ipython:: python
27+
28+
pd.NumericIndex([1, 2, 3], dtype="int8")
29+
pd.NumericIndex([1, 2, 3], dtype="uint32")
30+
pd.NumericIndex([1, 2, 3], dtype="float32")
31+
32+
In order to maintain backwards compatibility, calls to the base :class:`Index` will in
33+
pandas 1.x. return :class:`Int64Index`, :class:`UInt64Index` and :class:`Float64Index`.
34+
For example, the code below returns an ``Int64Index`` with dtype ``int64``:
35+
36+
.. code-block:: ipython
37+
38+
In [1]: pd.Index([1, 2, 3], dtype="int8")
39+
Int64Index([1, 2, 3], dtype='int64')
40+
41+
For the duration of Pandas 1.x, in order to maintain backwards compatibility, all
42+
operations that until now have returned :class:`Int64Index`, :class:`UInt64Index` and
43+
:class:`Float64Index` will continue to so. This means, that in order to use
44+
``NumericIndex``, you will have to call ``NumericIndex`` explicitly. For example the below series
45+
will have an ``Int64Index``:
46+
47+
.. code-block:: ipython
48+
49+
In [2]: ser = pd.Series([1, 2, 3], index=[1, 2, 3])
50+
In [3]: ser.index
51+
Int64Index([1, 2, 3], dtype='int64')
52+
53+
Instead if you want to use a ``NumericIndex``, you should do:
54+
55+
.. ipython:: python
56+
57+
idx = pd.NumericIndex([1, 2, 3], dtype="int8")
58+
ser = pd.Series([1, 2, 3], index=idx)
59+
ser.index
60+
61+
In Pandas 2.0, :class:`NumericIndex` will become the default numeric index type and
62+
``Int64Index``, ``UInt64Index`` and ``Float64Index`` will be removed.
63+
64+
See :ref:`here <advanced.numericindex>` for more.
2265

2366
.. _whatsnew_140.enhancements.enhancement2:
2467

pandas/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
UInt64Index,
7676
RangeIndex,
7777
Float64Index,
78+
NumericIndex,
7879
MultiIndex,
7980
IntervalIndex,
8081
TimedeltaIndex,

pandas/_testing/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
Int64Index,
5151
IntervalIndex,
5252
MultiIndex,
53+
NumericIndex,
5354
RangeIndex,
5455
Series,
5556
UInt64Index,
@@ -105,7 +106,6 @@
105106
use_numexpr,
106107
with_csv_dialect,
107108
)
108-
from pandas.core.api import NumericIndex
109109
from pandas.core.arrays import (
110110
DatetimeArray,
111111
PandasArray,

pandas/core/indexes/category.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def _is_dtype_compat(self, other) -> Categorical:
283283

284284
@doc(Index.astype)
285285
def astype(self, dtype: Dtype, copy: bool = True) -> Index:
286-
from pandas.core.api import NumericIndex
286+
from pandas import NumericIndex
287287

288288
dtype = pandas_dtype(dtype)
289289

pandas/tests/api/test_api.py

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class TestPDApi(Base):
6868
"Index",
6969
"Int64Index",
7070
"MultiIndex",
71+
"NumericIndex",
7172
"Period",
7273
"PeriodIndex",
7374
"RangeIndex",

pandas/tests/base/test_unique.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
)
1010

1111
import pandas as pd
12+
from pandas import NumericIndex
1213
import pandas._testing as tm
13-
from pandas.core.api import NumericIndex
1414
from pandas.tests.base.common import allow_na_ops
1515

1616

pandas/tests/indexes/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
Int64Index,
2727
IntervalIndex,
2828
MultiIndex,
29+
NumericIndex,
2930
PeriodIndex,
3031
RangeIndex,
3132
Series,
@@ -34,7 +35,6 @@
3435
)
3536
from pandas import UInt64Index # noqa:F401
3637
import pandas._testing as tm
37-
from pandas.core.api import NumericIndex
3838
from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin
3939

4040

pandas/tests/indexes/numeric/test_numeric.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
Float64Index,
99
Index,
1010
Int64Index,
11+
NumericIndex,
1112
Series,
1213
UInt64Index,
1314
)
1415
import pandas._testing as tm
15-
from pandas.core.api import NumericIndex
1616
from pandas.tests.indexes.common import NumericBase
1717

1818

pandas/tests/indexes/test_common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
CategoricalIndex,
2222
DatetimeIndex,
2323
MultiIndex,
24+
NumericIndex,
2425
PeriodIndex,
2526
RangeIndex,
2627
TimedeltaIndex,
2728
)
2829
import pandas._testing as tm
29-
from pandas.core.api import NumericIndex
3030

3131

3232
class TestCommon:

pandas/tests/indexes/test_numpy_compat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
DatetimeIndex,
66
Float64Index,
77
Index,
8+
NumericIndex,
89
PeriodIndex,
910
TimedeltaIndex,
1011
)
1112
import pandas._testing as tm
12-
from pandas.core.api import NumericIndex
1313
from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin
1414

1515

0 commit comments

Comments
 (0)