From 0a3577ed0ec83f6281dd250a252ea829cad91321 Mon Sep 17 00:00:00 2001 From: Amir Date: Fri, 27 Sep 2024 13:49:38 +0200 Subject: [PATCH 1/6] Modified top-level name object on CategoricalDtype class by importing the set module decorator and decorating the class. Tested the modification by adding an assert statement to the already existing test_set_module test function --- pandas/core/dtypes/dtypes.py | 2 ++ pandas/tests/api/test_api.py | 1 + 2 files changed, 3 insertions(+) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index bb6610c514375..7806abf10c4f8 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -48,6 +48,7 @@ from pandas._libs.tslibs.offsets import BDay from pandas.compat import pa_version_under10p1 from pandas.errors import PerformanceWarning +from pandas.util._decorators import set_module from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.base import ( @@ -155,6 +156,7 @@ class CategoricalDtypeType(type): @register_extension_dtype +@set_module("pandas") class CategoricalDtype(PandasExtensionDtype, ExtensionDtype): """ Type for categorical data with the categories and orderedness. diff --git a/pandas/tests/api/test_api.py b/pandas/tests/api/test_api.py index b23876d9280f7..962e9ff42b806 100644 --- a/pandas/tests/api/test_api.py +++ b/pandas/tests/api/test_api.py @@ -400,3 +400,4 @@ def test_util_in_top_level(self): def test_set_module(): assert pd.DataFrame.__module__ == "pandas" + assert pd.CategoricalDtype.__module__ == "pandas" From d028f957a26611991c2d07c370d8bb41d29e775f Mon Sep 17 00:00:00 2001 From: Amir Date: Fri, 27 Sep 2024 14:10:30 +0200 Subject: [PATCH 2/6] Modified top-level name object on PeriodDtype IntervalDtype SparseDtype ArrowDtype clasess by decorating them with the set module decorator. Tested the modification by adding an assert statement to the already existing test_set_module test function --- pandas/core/dtypes/dtypes.py | 5 +++++ pandas/tests/api/test_api.py | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 7806abf10c4f8..004a1aab5436e 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -708,6 +708,7 @@ def index_class(self) -> type_t[CategoricalIndex]: @register_extension_dtype +@set_module("pandas") class DatetimeTZDtype(PandasExtensionDtype): """ An ExtensionDtype for timezone-aware datetime data. @@ -976,6 +977,7 @@ def index_class(self) -> type_t[DatetimeIndex]: @register_extension_dtype +@set_module("pandas") class PeriodDtype(PeriodDtypeBase, PandasExtensionDtype): """ An ExtensionDtype for Period data. @@ -1217,6 +1219,7 @@ def index_class(self) -> type_t[PeriodIndex]: @register_extension_dtype +@set_module("pandas") class IntervalDtype(PandasExtensionDtype): """ An ExtensionDtype for Interval data. @@ -1693,6 +1696,7 @@ def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None: @register_extension_dtype +@set_module("pandas") class SparseDtype(ExtensionDtype): """ Dtype for data stored in :class:`SparseArray`. @@ -2132,6 +2136,7 @@ def _get_common_dtype(self, dtypes: list[DtypeObj]) -> DtypeObj | None: @register_extension_dtype +@set_module("pandas") class ArrowDtype(StorageExtensionDtype): """ An ExtensionDtype for PyArrow data types. diff --git a/pandas/tests/api/test_api.py b/pandas/tests/api/test_api.py index 962e9ff42b806..bdee26dcb2127 100644 --- a/pandas/tests/api/test_api.py +++ b/pandas/tests/api/test_api.py @@ -401,3 +401,7 @@ def test_util_in_top_level(self): def test_set_module(): assert pd.DataFrame.__module__ == "pandas" assert pd.CategoricalDtype.__module__ == "pandas" + assert pd.PeriodDtype.__module__ == "pandas" + assert pd.IntervalDtype.__module__ == "pandas" + assert pd.SparseDtype.__module__ == "pandas" + assert pd.ArrowDtype.__module__ == "pandas" From 2da62af1c97e717dc7ca1e274f9f287e0ecdda07 Mon Sep 17 00:00:00 2001 From: Amir Date: Fri, 27 Sep 2024 15:27:51 +0200 Subject: [PATCH 3/6] Modified top-level name object for all Indexes classes --- pandas/core/indexes/base.py | 3 ++- pandas/core/indexes/datetimes.py | 2 ++ pandas/core/indexes/extension.py | 4 +++- pandas/core/indexes/interval.py | 2 ++ pandas/core/indexes/multi.py | 2 ++ pandas/core/indexes/period.py | 2 ++ pandas/core/indexes/range.py | 2 ++ pandas/core/indexes/timedeltas.py | 2 ++ pandas/tests/api/test_api.py | 8 ++++++++ 9 files changed, 25 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 852049804a4f5..b3155817247e0 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -71,6 +71,7 @@ Appender, cache_readonly, doc, + set_module, ) from pandas.util._exceptions import ( find_stack_level, @@ -314,7 +315,7 @@ def _new_Index(cls, d): d["dtype"] = d["data"].dtype return cls.__new__(cls, **d) - +@set_module("pandas") class Index(IndexOpsMixin, PandasObject): """ Immutable sequence used for indexing and alignment. diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 536f22d38468d..b3d9c3bc78a66 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -26,6 +26,7 @@ from pandas.util._decorators import ( cache_readonly, doc, + set_module, ) from pandas.core.dtypes.common import is_scalar @@ -126,6 +127,7 @@ def _new_DatetimeIndex(cls, d): + DatetimeArray._bool_ops, DatetimeArray, ) +@set_module("pandas") class DatetimeIndex(DatetimeTimedeltaMixin): """ Immutable ndarray-like of datetime64 data. diff --git a/pandas/core/indexes/extension.py b/pandas/core/indexes/extension.py index 2eeacfb769be4..102f48827d4fc 100644 --- a/pandas/core/indexes/extension.py +++ b/pandas/core/indexes/extension.py @@ -10,7 +10,9 @@ TypeVar, ) -from pandas.util._decorators import cache_readonly +from pandas.util._decorators import ( + cache_readonly, +) from pandas.core.dtypes.generic import ABCDataFrame diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 8feac890883eb..7af26b9827b83 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -32,6 +32,7 @@ from pandas.util._decorators import ( Appender, cache_readonly, + set_module, ) from pandas.util._exceptions import rewrite_exception @@ -202,6 +203,7 @@ def _new_IntervalIndex(cls, d): IntervalArray, ) @inherit_names(["is_non_overlapping_monotonic", "closed"], IntervalArray, cache=True) +@set_module("pandas") class IntervalIndex(ExtensionIndex): _typ = "intervalindex" diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 9eccb7645fbe7..c555d262e7ece 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -53,6 +53,7 @@ Appender, cache_readonly, doc, + set_module, ) from pandas.util._exceptions import find_stack_level @@ -194,6 +195,7 @@ def new_meth(self_or_cls, *args, **kwargs): return cast(F, new_meth) +@set_module("pandas") class MultiIndex(Index): """ A multi-level, or hierarchical, index object for pandas objects. diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index b5f05ef0ab78f..64444ccf5dc82 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -20,6 +20,7 @@ from pandas.util._decorators import ( cache_readonly, doc, + set_module, ) from pandas.core.dtypes.common import is_integer @@ -81,6 +82,7 @@ def _new_PeriodIndex(cls, **d): wrap=True, ) @inherit_names(["is_leap_year"], PeriodArray) +@set_module("pandas") class PeriodIndex(DatetimeIndexOpsMixin): """ Immutable ndarray holding ordinal values indicating regular periods in time. diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index dc96d1c11db74..7eeaab3b0443f 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -27,6 +27,7 @@ from pandas.util._decorators import ( cache_readonly, doc, + set_module, ) from pandas.core.dtypes.base import ExtensionDtype @@ -74,6 +75,7 @@ def min_fitting_element(start: int, step: int, lower_limit: int) -> int: return start + abs(step) * no_steps +@set_module("pandas") class RangeIndex(Index): """ Immutable Index implementing a monotonic integer range. diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index 29039ffd0217e..6bbe86816d81f 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -13,6 +13,7 @@ Timedelta, to_offset, ) +from pandas.util._decorators import set_module from pandas.core.dtypes.common import ( is_scalar, @@ -50,6 +51,7 @@ ], TimedeltaArray, ) +@set_module("pandas") class TimedeltaIndex(DatetimeTimedeltaMixin): """ Immutable Index of timedelta64 data. diff --git a/pandas/tests/api/test_api.py b/pandas/tests/api/test_api.py index bdee26dcb2127..fadbe7645744e 100644 --- a/pandas/tests/api/test_api.py +++ b/pandas/tests/api/test_api.py @@ -405,3 +405,11 @@ def test_set_module(): assert pd.IntervalDtype.__module__ == "pandas" assert pd.SparseDtype.__module__ == "pandas" assert pd.ArrowDtype.__module__ == "pandas" + assert pd.Index.__module__ == "pandas" + assert pd.CategoricalIndex.__module__ == "pandas" + assert pd.DatetimeIndex.__module__ == "pandas" + assert pd.IntervalIndex.__module__ == "pandas" + assert pd.MultiIndex.__module__ == "pandas" + assert pd.PeriodIndex.__module__ == "pandas" + assert pd.RangeIndex.__module__ == "pandas" + assert pd.TimedeltaIndex.__module__ == "pandas" From aa71f329607f6c5f134559abd08b334047edd3f0 Mon Sep 17 00:00:00 2001 From: Amir Date: Fri, 27 Sep 2024 16:13:24 +0200 Subject: [PATCH 4/6] Modified top-level name object for all cython Type classes --- pandas/_libs/interval.pyx | 2 ++ pandas/_libs/tslibs/period.pyx | 3 +++ pandas/_libs/tslibs/timedeltas.pyx | 2 ++ pandas/_libs/tslibs/timestamps.pyx | 3 ++- pandas/tests/api/test_api.py | 4 ++++ 5 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/interval.pyx b/pandas/_libs/interval.pyx index 564019d7c0d8c..294cd6542221a 100644 --- a/pandas/_libs/interval.pyx +++ b/pandas/_libs/interval.pyx @@ -41,6 +41,8 @@ from pandas._libs.tslibs.util cimport ( is_integer_object, ) +from pandas.util._decorators import set_module + VALID_CLOSED = frozenset(["left", "right", "both", "neither"]) diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index c563ab91c4142..40760061ec60b 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -115,6 +115,8 @@ from pandas._libs.tslibs.offsets import ( BDay, ) +from pandas.util._decorators import set_module + cdef: enum: INT32_MIN = -2_147_483_648LL @@ -2830,6 +2832,7 @@ cdef class _Period(PeriodMixin): return period_format(self.ordinal, base, fmt) +@set_module("pandas") class Period(_Period): """ Represents a period of time. diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 84ca48c96459f..1ec86e424c70e 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -2,6 +2,7 @@ import collections import warnings from pandas.util._exceptions import find_stack_level +from pandas.util._decorators import set_module cimport cython from cpython.object cimport ( @@ -1852,6 +1853,7 @@ cdef class _Timedelta(timedelta): # Python front end to C extension type _Timedelta # This serves as the box for timedelta64 +@set_module("pandas") class Timedelta(_Timedelta): """ Represents a duration, the difference between two dates or times. diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 34c84d396ad64..e320b80b7f8dd 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -51,6 +51,7 @@ from pandas._libs.tslibs cimport ccalendar from pandas._libs.tslibs.base cimport ABCTimestamp from pandas.util._exceptions import find_stack_level +from pandas.util._decorators import set_module from pandas._libs.tslibs.conversion cimport ( _TSObject, @@ -1648,7 +1649,7 @@ cdef class _Timestamp(ABCTimestamp): # Python front end to C extension type _Timestamp # This serves as the box for datetime64 - +@set_module("pandas") class Timestamp(_Timestamp): """ Pandas replacement for python datetime.datetime object. diff --git a/pandas/tests/api/test_api.py b/pandas/tests/api/test_api.py index fadbe7645744e..171181bb04801 100644 --- a/pandas/tests/api/test_api.py +++ b/pandas/tests/api/test_api.py @@ -413,3 +413,7 @@ def test_set_module(): assert pd.PeriodIndex.__module__ == "pandas" assert pd.RangeIndex.__module__ == "pandas" assert pd.TimedeltaIndex.__module__ == "pandas" + assert pd.Timestamp.__module__ == "pandas" + assert pd.Timedelta.__module__ == "pandas" + assert pd.Period.__module__ == "pandas" + From 824552f74702f8f49395b9ac47b8249d54ce64f6 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Thu, 7 Nov 2024 22:55:44 +0100 Subject: [PATCH 5/6] fixup categoricalindex --- pandas/core/indexes/category.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 312219eb7b91a..d20a84449fb85 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -13,6 +13,7 @@ from pandas.util._decorators import ( cache_readonly, doc, + set_module, ) from pandas.core.dtypes.common import is_scalar @@ -76,6 +77,7 @@ Categorical, wrap=True, ) +@set_module("pandas") class CategoricalIndex(NDArrayBackedExtensionIndex): """ Index based on an underlying :class:`Categorical`. From d748d21f0fc69d27e85a396f05dd16bcc48e249c Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Thu, 7 Nov 2024 23:04:59 +0100 Subject: [PATCH 6/6] linting --- pandas/_libs/interval.pyx | 2 -- pandas/core/indexes/base.py | 1 + pandas/core/indexes/extension.py | 4 +--- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/pandas/_libs/interval.pyx b/pandas/_libs/interval.pyx index 294cd6542221a..564019d7c0d8c 100644 --- a/pandas/_libs/interval.pyx +++ b/pandas/_libs/interval.pyx @@ -41,8 +41,6 @@ from pandas._libs.tslibs.util cimport ( is_integer_object, ) -from pandas.util._decorators import set_module - VALID_CLOSED = frozenset(["left", "right", "both", "neither"]) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 850ef3470354e..4a90b164c89cc 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -315,6 +315,7 @@ def _new_Index(cls, d): d["dtype"] = d["data"].dtype return cls.__new__(cls, **d) + @set_module("pandas") class Index(IndexOpsMixin, PandasObject): """ diff --git a/pandas/core/indexes/extension.py b/pandas/core/indexes/extension.py index 102f48827d4fc..2eeacfb769be4 100644 --- a/pandas/core/indexes/extension.py +++ b/pandas/core/indexes/extension.py @@ -10,9 +10,7 @@ TypeVar, ) -from pandas.util._decorators import ( - cache_readonly, -) +from pandas.util._decorators import cache_readonly from pandas.core.dtypes.generic import ABCDataFrame