Skip to content

Commit b34da22

Browse files
committed
DOC: NumericIndex
1 parent eaee348 commit b34da22

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

doc/source/reference/indexing.rst

Lines changed: 1 addition & 0 deletions
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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,13 @@ values **not** in the categories, similarly to how you can reindex **any** panda
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

@@ -862,6 +869,13 @@ implementing an ordered, sliceable set.
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+
.. _indexing.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. See :ref:`here <advanced.numericindex>` for more.
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+
index = pd.NumericIndex([1, 2, 4, 5], dtype="int8")
993+
index
994+
ser = pd.Series(range(5), index=index)
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/whatsnew/v1.4.0.rst

Lines changed: 46 additions & 3 deletions
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+
but not with lower bit sizes (int32, int8, uint32, uint8 etc). It is now possible to create
25+
an index of any numpy dtype using the new :class:`NumericIndex` (:issue:`41153`):
26+
27+
.. ipython:: python
28+
29+
pd.NumericIndex([1, 2, 3], dtype="int8")
30+
pd.NumericIndex([1, 2, 3], dtype="uint32")
31+
pd.NumericIndex([1, 2, 3], dtype="float32")
32+
33+
In order to maintain backwards compatibility, calls to the base :class:`Index` will in
34+
pandas 1.x. return :class:`Int64Index`, :class:`UInt64Index` and :class:`Float64Index`.
35+
For example, notice that the code below returns an ``Int64Index`` with dtype ``int64``:
36+
37+
.. code-block:: ipython
38+
39+
In [1]: pd.Index([1, 2, 3], dtype="int8")
40+
Int64Index([1, 2, 3], dtype='int64')
41+
42+
For the duration of Pandas 1.x, in order to maintain backwards compatibility, all
43+
operations that until now have returned :class:`Int64Index`, :class:`UInt64Index` and
44+
:class:`Float64Index` will continue to so. This means, that in order to use
45+
``NumericIndex``, you will have to call it explicitly. For example:
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+
.. code-block:: ipython
56+
57+
In [2]: ser = pd.Series([1, 2, 3], index=pd.NumericIndex([1, 2, 3], dtype="int8"))
58+
In [3]: ser.index
59+
NumericIndex([1, 2, 3], dtype='int8')
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

0 commit comments

Comments
 (0)