From e8e7bbd5699807bac35cedee6e6e99b73d2c8642 Mon Sep 17 00:00:00 2001 From: Yinon Horev Date: Sat, 23 Mar 2024 17:14:07 +0100 Subject: [PATCH 1/4] ENH: set __module__ for objects in pandas Scalars API --- pandas/_libs/tslibs/period.pyx | 3 +++ pandas/_libs/tslibs/timedeltas.pyx | 3 ++- pandas/_libs/tslibs/timestamps.pyx | 3 ++- pandas/tests/api/test_api.py | 3 +++ 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 838b5b9f4595f..e5320900444e4 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 @@ -2662,6 +2664,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 18ead7d5381df..38261c5d2a3b3 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1,6 +1,7 @@ import collections import warnings +from pandas.util._decorators import set_module from pandas.util._exceptions import find_stack_level cimport cython @@ -1771,7 +1772,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 d4cd90613ca5b..c36b60ff4fe87 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -50,6 +50,7 @@ import datetime as dt from pandas._libs.tslibs cimport ccalendar from pandas._libs.tslibs.base cimport ABCTimestamp +from pandas.util._decorators import set_module from pandas.util._exceptions import find_stack_level from pandas._libs.tslibs.conversion cimport ( @@ -1263,7 +1264,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 82c5c305b574c..8c734eec407c6 100644 --- a/pandas/tests/api/test_api.py +++ b/pandas/tests/api/test_api.py @@ -405,3 +405,6 @@ def test_pandas_array_alias(): def test_set_module(): assert pd.DataFrame.__module__ == "pandas" + assert pd.Period.__module__ == "pandas" + assert pd.Timestamp.__module__ == "pandas" + assert pd.Timedelta.__module__ == "pandas" From 4b0a3f5b93f0a7daf7539a1babae2953b417ab7f Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 10 Jun 2024 20:44:13 +0200 Subject: [PATCH 2/4] update test --- pandas/tests/tslibs/test_timezones.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/tslibs/test_timezones.py b/pandas/tests/tslibs/test_timezones.py index 28e4889983fb9..332e53f393b63 100644 --- a/pandas/tests/tslibs/test_timezones.py +++ b/pandas/tests/tslibs/test_timezones.py @@ -141,7 +141,7 @@ def test_maybe_get_tz_invalid_types(): with pytest.raises(TypeError, match=""): timezones.maybe_get_tz(pytz) - msg = "" + msg = "" with pytest.raises(TypeError, match=msg): timezones.maybe_get_tz(Timestamp("2021-01-01", tz="UTC")) From 7f00938a7eacb40bceeb21bd1fda0e8559e02445 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 10 Jun 2024 21:02:45 +0200 Subject: [PATCH 3/4] lint --- pandas/_libs/tslibs/period.pyx | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 34d979d6211ab..774e33bbe952d 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -114,7 +114,6 @@ from pandas._libs.tslibs.offsets import ( INVALID_FREQ_ERR_MSG, BDay, ) - from pandas.util._decorators import set_module cdef: From 4ae9e4790deef93d9cc57399cd4911925016ec97 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Thu, 7 Nov 2024 21:48:16 +0100 Subject: [PATCH 4/4] fix doctests --- pandas/_libs/tslibs/timedeltas.pyx | 2 +- pandas/_libs/tslibs/timestamps.pyx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 707808aaa1840..e320aca04683c 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1917,7 +1917,7 @@ class Timedelta(_Timedelta): -------- Here we initialize Timedelta object with both value and unit - >>> td = pd.Timedelta(1, "d") + >>> td = pd.Timedelta(1, "D") >>> td Timedelta('1 days 00:00:00') diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index f53fbdfcf0391..1ab34da7ab53f 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -2927,7 +2927,7 @@ timedelta}, default 'raise' -------- >>> ts = pd.Timestamp(1584226800, unit='s', tz='Europe/Stockholm') >>> ts.tz - + zoneinfo.ZoneInfo(key='Europe/Stockholm') """ return self.tzinfo