From 959282e6a93d421dc4cc69579b3b0121ab3f5114 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Wed, 8 Dec 2021 17:06:48 +0100 Subject: [PATCH 1/6] API: hide NumericIndex from public top-level namespace in favor of pd.Index --- doc/source/reference/indexing.rst | 1 - doc/source/whatsnew/v1.4.0.rst | 29 +++++++++++++++++--- pandas/__init__.py | 4 +-- pandas/_testing/__init__.py | 2 +- pandas/core/indexes/category.py | 2 +- pandas/tests/api/test_api.py | 1 - pandas/tests/base/test_unique.py | 2 +- pandas/tests/indexes/common.py | 2 +- pandas/tests/indexes/multi/test_names.py | 2 +- pandas/tests/indexes/numeric/test_numeric.py | 2 +- pandas/tests/indexes/test_base.py | 2 +- pandas/tests/indexes/test_common.py | 2 +- pandas/tests/indexes/test_numpy_compat.py | 6 ++-- 13 files changed, 38 insertions(+), 19 deletions(-) diff --git a/doc/source/reference/indexing.rst b/doc/source/reference/indexing.rst index 1ce75f5aac877..ddfef14036ef3 100644 --- a/doc/source/reference/indexing.rst +++ b/doc/source/reference/indexing.rst @@ -170,7 +170,6 @@ Numeric Index :toctree: api/ :template: autosummary/class_without_autosummary.rst - NumericIndex RangeIndex Int64Index UInt64Index diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 71d5c46b81ea0..282b1cdc75a63 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -477,12 +477,33 @@ Deprecations Deprecated Int64Index, UInt64Index & Float64Index ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + :class:`Int64Index`, :class:`UInt64Index` and :class:`Float64Index` have been deprecated -in favor of the new :class:`NumericIndex` and will be removed in Pandas 2.0 (:issue:`43028`). +in favor of the base :class:`Index` class and will be removed in Pandas 2.0 (:issue:`43028`). + +For constructing a numeric index, you can use the base :class:`Index` class instead +specifying the data type (which will also work on older pandas releases): + +.. code-block:: python + + # replace + pd.Int64Index([1, 2, 3]) + # with + pd.Index([1, 2, 3], dtype="int64") + +For checking the data type of an index object, you can replace ``isinstance`` +checks with checking the ``dtype``: + +.. code-block:: python + + # replace + isinstance(idx, pd.Int64Index) + # with + idx.dtype == "int64" Currently, in order to maintain backward compatibility, calls to :class:`Index` will continue to return :class:`Int64Index`, :class:`UInt64Index` and :class:`Float64Index` -when given numeric data, but in the future, a :class:`NumericIndex` will be returned. +when given numeric data, but in the future, an :class:`Index` will be returned. *Current behavior*: @@ -498,9 +519,9 @@ when given numeric data, but in the future, a :class:`NumericIndex` will be retu .. code-block:: ipython In [3]: pd.Index([1, 2, 3], dtype="int32") - Out [3]: NumericIndex([1, 2, 3], dtype='int32') + Out [3]: Index([1, 2, 3], dtype='int32') In [4]: pd.Index([1, 2, 3], dtype="uint64") - Out [4]: NumericIndex([1, 2, 3], dtype='uint64') + Out [4]: Index([1, 2, 3], dtype='uint64') .. _whatsnew_140.deprecations.other: diff --git a/pandas/__init__.py b/pandas/__init__.py index 9505d0481ee19..c873b16850026 100644 --- a/pandas/__init__.py +++ b/pandas/__init__.py @@ -71,7 +71,6 @@ Index, CategoricalIndex, RangeIndex, - NumericIndex, MultiIndex, IntervalIndex, TimedeltaIndex, @@ -197,7 +196,7 @@ def __getattr__(name): warnings.warn( f"pandas.{name} is deprecated " "and will be removed from pandas in a future version. " - "Use pandas.NumericIndex with the appropriate dtype instead.", + "Use pandas.Index with the appropriate dtype instead.", FutureWarning, stacklevel=2, ) @@ -333,7 +332,6 @@ def __getattr__(name): "NA", "NaT", "NamedAgg", - "NumericIndex", "Period", "PeriodDtype", "PeriodIndex", diff --git a/pandas/_testing/__init__.py b/pandas/_testing/__init__.py index 6248154422252..a22d14b9d5def 100644 --- a/pandas/_testing/__init__.py +++ b/pandas/_testing/__init__.py @@ -48,7 +48,6 @@ Index, IntervalIndex, MultiIndex, - NumericIndex, RangeIndex, Series, bdate_range, @@ -107,6 +106,7 @@ from pandas.core.api import ( Float64Index, Int64Index, + NumericIndex, UInt64Index, ) from pandas.core.arrays import ( diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index f26a24c38b19f..388f69da058a9 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -284,7 +284,7 @@ def _is_dtype_compat(self, other) -> Categorical: @doc(Index.astype) def astype(self, dtype: Dtype, copy: bool = True) -> Index: - from pandas import NumericIndex + from pandas.core.api import NumericIndex dtype = pandas_dtype(dtype) diff --git a/pandas/tests/api/test_api.py b/pandas/tests/api/test_api.py index ec20bc49c8a4b..ed9ff6735c143 100644 --- a/pandas/tests/api/test_api.py +++ b/pandas/tests/api/test_api.py @@ -65,7 +65,6 @@ class TestPDApi(Base): "Index", "Int64Index", "MultiIndex", - "NumericIndex", "Period", "PeriodIndex", "RangeIndex", diff --git a/pandas/tests/base/test_unique.py b/pandas/tests/base/test_unique.py index 31f2aebcba4ba..ae08fd6bb3bcf 100644 --- a/pandas/tests/base/test_unique.py +++ b/pandas/tests/base/test_unique.py @@ -4,8 +4,8 @@ from pandas.core.dtypes.common import is_datetime64tz_dtype import pandas as pd -from pandas import NumericIndex import pandas._testing as tm +from pandas.core.api import NumericIndex from pandas.tests.base.common import allow_na_ops diff --git a/pandas/tests/indexes/common.py b/pandas/tests/indexes/common.py index 2c8873acd8303..185dc0b2ee533 100644 --- a/pandas/tests/indexes/common.py +++ b/pandas/tests/indexes/common.py @@ -21,7 +21,6 @@ Index, IntervalIndex, MultiIndex, - NumericIndex, PeriodIndex, RangeIndex, Series, @@ -32,6 +31,7 @@ from pandas.core.api import ( # noqa:F401 Float64Index, Int64Index, + NumericIndex, UInt64Index, ) diff --git a/pandas/tests/indexes/multi/test_names.py b/pandas/tests/indexes/multi/test_names.py index 63315490cc271..cfbc90d1b36bb 100644 --- a/pandas/tests/indexes/multi/test_names.py +++ b/pandas/tests/indexes/multi/test_names.py @@ -148,7 +148,7 @@ def test_setting_names_from_levels_raises(): new.index.name = "bar" assert pd.Index._no_setting_name is False - assert pd.NumericIndex._no_setting_name is False + assert pd.core.api.NumericIndex._no_setting_name is False assert pd.RangeIndex._no_setting_name is False diff --git a/pandas/tests/indexes/numeric/test_numeric.py b/pandas/tests/indexes/numeric/test_numeric.py index be5ff437f30a9..af308379cba5e 100644 --- a/pandas/tests/indexes/numeric/test_numeric.py +++ b/pandas/tests/indexes/numeric/test_numeric.py @@ -6,13 +6,13 @@ import pandas as pd from pandas import ( Index, - NumericIndex, Series, ) import pandas._testing as tm from pandas.core.indexes.api import ( Float64Index, Int64Index, + NumericIndex, UInt64Index, ) from pandas.tests.indexes.common import NumericBase diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 72d654f6cfa28..24ca655841aa4 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -16,7 +16,6 @@ DataFrame, DatetimeIndex, IntervalIndex, - NumericIndex, PeriodIndex, RangeIndex, Series, @@ -28,6 +27,7 @@ from pandas.core.api import ( Float64Index, Int64Index, + NumericIndex, UInt64Index, ) from pandas.core.indexes.api import ( diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index fff11583e5161..516aff295e181 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -20,12 +20,12 @@ CategoricalIndex, DatetimeIndex, MultiIndex, - NumericIndex, PeriodIndex, RangeIndex, TimedeltaIndex, ) import pandas._testing as tm +from pandas.core.api import NumericIndex class TestCommon: diff --git a/pandas/tests/indexes/test_numpy_compat.py b/pandas/tests/indexes/test_numpy_compat.py index 573ee987ab4c8..dfe2d62467a2d 100644 --- a/pandas/tests/indexes/test_numpy_compat.py +++ b/pandas/tests/indexes/test_numpy_compat.py @@ -5,13 +5,15 @@ CategoricalIndex, DatetimeIndex, Index, - NumericIndex, PeriodIndex, TimedeltaIndex, isna, ) import pandas._testing as tm -from pandas.core.api import Float64Index +from pandas.core.api import ( + Float64Index, + NumericIndex, +) from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin From cf0d6458abf16d64ffacd3c374502306ec0c927f Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 10 Dec 2021 09:25:50 +0100 Subject: [PATCH 2/6] update notes --- doc/source/user_guide/advanced.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/source/user_guide/advanced.rst b/doc/source/user_guide/advanced.rst index fd89e4e896178..59a8b01e28a4d 100644 --- a/doc/source/user_guide/advanced.rst +++ b/doc/source/user_guide/advanced.rst @@ -852,7 +852,7 @@ Int64Index and RangeIndex ~~~~~~~~~~~~~~~~~~~~~~~~~ .. deprecated:: 1.4.0 - In pandas 2.0, :class:`NumericIndex` will become the default index type for numeric types + In pandas 2.0, :class:`Index` will become the default index type for numeric types instead of ``Int64Index``, ``Float64Index`` and ``UInt64Index`` and those index types are therefore deprecated and will be removed in a futire version. See :ref:`here ` for more. @@ -870,7 +870,7 @@ Float64Index ~~~~~~~~~~~~ .. deprecated:: 1.4.0 - :class:`NumericIndex` will become the default index type for numeric types in the future + :class:`Index` will become the default index type for numeric types in the future instead of ``Int64Index``, ``Float64Index`` and ``UInt64Index`` and those index types are therefore deprecated and will be removed in a future version of Pandas. See :ref:`here ` for more. @@ -980,7 +980,7 @@ NumericIndex .. note:: - In pandas 2.0, :class:`NumericIndex` will become the default index type for numeric types + In pandas 2.0, :class:`Index` will become the default index type for numeric types instead of :class:`Int64Index`, :class:`Float64Index` and :class:`UInt64Index` and those index types are therefore deprecated and will be removed in a future version. :class:`RangeIndex` will not be removed as it represents an optimized version of an integer index. From e41f6b7299b3df1dc28b60a3c532931af40bbc17 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Wed, 22 Dec 2021 19:37:05 +0100 Subject: [PATCH 3/6] remove doctest example --- pandas/core/indexes/numeric.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index bb25813e9742b..a8e2ffa1dbf6c 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -82,13 +82,6 @@ class NumericIndex(Index): An NumericIndex instance can **only** contain numpy int64/32/16/8, uint64/32/16/8 or float64/32/16 dtype. In particular, ``NumericIndex`` *can not* hold Pandas numeric dtypes (:class:`Int64Dtype`, :class:`Int32Dtype` etc.). - - Examples - -------- - >>> pd.NumericIndex([1, 2, 3], dtype="int8") - NumericIndex([1, 2, 3], dtype='int8') - >>> pd.NumericIndex([1, 2, 3], dtype="float32") - NumericIndex([1.0, 2.0, 3.0], dtype='float32') """ _typ = "numericindex" From 3f1b5fe6dd17d3745074fe693180a5e0e1105716 Mon Sep 17 00:00:00 2001 From: phofl Date: Fri, 31 Dec 2021 23:40:34 +0100 Subject: [PATCH 4/6] Remove whatsnew --- doc/source/whatsnew/v0.16.2.rst | 1 + doc/source/whatsnew/v1.4.0.rst | 49 --------------------------------- 2 files changed, 1 insertion(+), 49 deletions(-) diff --git a/doc/source/whatsnew/v0.16.2.rst b/doc/source/whatsnew/v0.16.2.rst index 37e8c64ea9ced..40d764e880c9c 100644 --- a/doc/source/whatsnew/v0.16.2.rst +++ b/doc/source/whatsnew/v0.16.2.rst @@ -62,6 +62,7 @@ When the function you wish to apply takes its data anywhere other than the first of ``(function, keyword)`` indicating where the DataFrame should flow. For example: .. ipython:: python + :okwarning: import statsmodels.formula.api as sm diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 3b84e1632be7b..9aa9eb10db023 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -40,55 +40,6 @@ This made it difficult to determine where the warning was being generated from. A value is trying to be set on a copy of a slice from a DataFrame. -.. _whatsnew_140.enhancements.numeric_index: - -More flexible numeric dtypes for indexes -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -Until now, it has only been possible to create numeric indexes with int64/float64/uint64 dtypes. -It is now possible to create an index of any numpy int/uint/float dtype using the new :class:`NumericIndex` index type (:issue:`41153`): - -.. ipython:: python - - pd.NumericIndex([1, 2, 3], dtype="int8") - pd.NumericIndex([1, 2, 3], dtype="uint32") - pd.NumericIndex([1, 2, 3], dtype="float32") - -In order to maintain backwards compatibility, calls to the base :class:`Index` will currently -return :class:`Int64Index`, :class:`UInt64Index` and :class:`Float64Index`, where relevant. -For example, the code below returns an ``Int64Index`` with dtype ``int64``: - -.. code-block:: ipython - - In [1]: pd.Index([1, 2, 3], dtype="int8") - Int64Index([1, 2, 3], dtype='int64') - -but will in a future version return a :class:`NumericIndex` with dtype ``int8``. - -More generally, currently, all operations that until now have -returned :class:`Int64Index`, :class:`UInt64Index` and :class:`Float64Index` will -continue to so. This means, that in order to use ``NumericIndex`` in the current version, you -will have to call ``NumericIndex`` explicitly. For example the below series will have an ``Int64Index``: - -.. code-block:: ipython - - In [2]: ser = pd.Series([1, 2, 3], index=[1, 2, 3]) - In [3]: ser.index - Int64Index([1, 2, 3], dtype='int64') - -Instead, if you want to use a ``NumericIndex``, you should do: - -.. ipython:: python - - idx = pd.NumericIndex([1, 2, 3], dtype="int8") - ser = pd.Series([1, 2, 3], index=idx) - ser.index - -In a future version of Pandas, :class:`NumericIndex` will become the default numeric index type and -``Int64Index``, ``UInt64Index`` and ``Float64Index`` are therefore deprecated and will -be removed in the future, see :ref:`here ` for more. - -See :ref:`here ` for more about :class:`NumericIndex`. .. _whatsnew_140.enhancements.ExtensionIndex: From a09c061b2916f732769f19a7cb43e0f3d10b4210 Mon Sep 17 00:00:00 2001 From: phofl Date: Fri, 31 Dec 2021 23:42:01 +0100 Subject: [PATCH 5/6] Remove from userguide --- doc/source/user_guide/advanced.rst | 31 ------------------------------ 1 file changed, 31 deletions(-) diff --git a/doc/source/user_guide/advanced.rst b/doc/source/user_guide/advanced.rst index 59a8b01e28a4d..4aed63b407357 100644 --- a/doc/source/user_guide/advanced.rst +++ b/doc/source/user_guide/advanced.rst @@ -971,37 +971,6 @@ If you need integer based selection, you should use ``iloc``: dfir.iloc[0:5] -.. _advanced.numericindex: - -NumericIndex -~~~~~~~~~~~~ - -.. versionadded:: 1.4.0 - -.. note:: - - In pandas 2.0, :class:`Index` will become the default index type for numeric types - instead of :class:`Int64Index`, :class:`Float64Index` and :class:`UInt64Index` and those index types - are therefore deprecated and will be removed in a future version. - :class:`RangeIndex` will not be removed as it represents an optimized version of an integer index. - -:class:`NumericIndex` is an index type that can hold data of any numpy int/uint/float dtype. For example: - -.. ipython:: python - - idx = pd.NumericIndex([1, 2, 4, 5], dtype="int8") - idx - ser = pd.Series(range(4), index=idx) - ser - -``NumericIndex`` works the same way as the existing ``Int64Index``, ``Float64Index`` and -``UInt64Index`` except that it can hold any numpy int, uint or float dtype. - -Until Pandas 2.0, you will have to call ``NumericIndex`` explicitly in order to use it, like in the example above. -In the future, ``NumericIndex`` will become the default pandas numeric index type and will automatically be used where appropriate. - -Please notice that ``NumericIndex`` *can not* hold Pandas numeric dtypes (:class:`Int64Dtype`, :class:`Int32Dtype` etc.). - .. _advanced.intervalindex: IntervalIndex From 39fcb125179f1d7200808b71a6f848823810d52e Mon Sep 17 00:00:00 2001 From: phofl Date: Sat, 1 Jan 2022 00:09:43 +0100 Subject: [PATCH 6/6] Remove references --- doc/source/user_guide/advanced.rst | 2 -- 1 file changed, 2 deletions(-) diff --git a/doc/source/user_guide/advanced.rst b/doc/source/user_guide/advanced.rst index 4aed63b407357..45b2e57f52c6c 100644 --- a/doc/source/user_guide/advanced.rst +++ b/doc/source/user_guide/advanced.rst @@ -855,7 +855,6 @@ Int64Index and RangeIndex In pandas 2.0, :class:`Index` will become the default index type for numeric types instead of ``Int64Index``, ``Float64Index`` and ``UInt64Index`` and those index types are therefore deprecated and will be removed in a futire version. - See :ref:`here ` for more. ``RangeIndex`` will not be removed, as it represents an optimized version of an integer index. :class:`Int64Index` is a fundamental basic index in pandas. This is an immutable array @@ -873,7 +872,6 @@ Float64Index :class:`Index` will become the default index type for numeric types in the future instead of ``Int64Index``, ``Float64Index`` and ``UInt64Index`` and those index types are therefore deprecated and will be removed in a future version of Pandas. - See :ref:`here ` for more. ``RangeIndex`` will not be removed as it represents an optimized version of an integer index. By default a :class:`Float64Index` will be automatically created when passing floating, or mixed-integer-floating values in index creation.