Skip to content

Commit f9d2e50

Browse files
YinonHorevYinonLensorjorisvandenbossche
authored
ENH: set __module__ for pandas scalars (Timestamp/Timedelta/Period) (#57976)
Co-authored-by: Yinon Horev <[email protected]> Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent b5e62ef commit f9d2e50

File tree

5 files changed

+12
-5
lines changed

5 files changed

+12
-5
lines changed

pandas/_libs/tslibs/period.pyx

+2
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ from pandas._libs.tslibs.offsets import (
114114
INVALID_FREQ_ERR_MSG,
115115
BDay,
116116
)
117+
from pandas.util._decorators import set_module
117118

118119
cdef:
119120
enum:
@@ -2830,6 +2831,7 @@ cdef class _Period(PeriodMixin):
28302831
return period_format(self.ordinal, base, fmt)
28312832

28322833

2834+
@set_module("pandas")
28332835
class Period(_Period):
28342836
"""
28352837
Represents a period of time.

pandas/_libs/tslibs/timedeltas.pyx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import collections
22
import warnings
33

4+
from pandas.util._decorators import set_module
45
from pandas.util._exceptions import find_stack_level
56

67
cimport cython
@@ -1854,7 +1855,7 @@ cdef class _Timedelta(timedelta):
18541855

18551856
# Python front end to C extension type _Timedelta
18561857
# This serves as the box for timedelta64
1857-
1858+
@set_module("pandas")
18581859
class Timedelta(_Timedelta):
18591860
"""
18601861
Represents a duration, the difference between two dates or times.
@@ -1916,7 +1917,7 @@ class Timedelta(_Timedelta):
19161917
--------
19171918
Here we initialize Timedelta object with both value and unit
19181919
1919-
>>> td = pd.Timedelta(1, "d")
1920+
>>> td = pd.Timedelta(1, "D")
19201921
>>> td
19211922
Timedelta('1 days 00:00:00')
19221923

pandas/_libs/tslibs/timestamps.pyx

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import datetime as dt
5050
from pandas._libs.tslibs cimport ccalendar
5151
from pandas._libs.tslibs.base cimport ABCTimestamp
5252

53+
from pandas.util._decorators import set_module
5354
from pandas.util._exceptions import find_stack_level
5455

5556
from pandas._libs.tslibs.conversion cimport (
@@ -1648,7 +1649,7 @@ cdef class _Timestamp(ABCTimestamp):
16481649
# Python front end to C extension type _Timestamp
16491650
# This serves as the box for datetime64
16501651
1651-
1652+
@set_module("pandas")
16521653
class Timestamp(_Timestamp):
16531654
"""
16541655
Pandas replacement for python datetime.datetime object.
@@ -2926,7 +2927,7 @@ timedelta}, default 'raise'
29262927
--------
29272928
>>> ts = pd.Timestamp(1584226800, unit='s', tz='Europe/Stockholm')
29282929
>>> ts.tz
2929-
<DstTzInfo 'Europe/Stockholm' CET+1:00:00 STD>
2930+
zoneinfo.ZoneInfo(key='Europe/Stockholm')
29302931
"""
29312932
return self.tzinfo
29322933

pandas/tests/api/test_api.py

+3
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,6 @@ def test_util_in_top_level(self):
400400

401401
def test_set_module():
402402
assert pd.DataFrame.__module__ == "pandas"
403+
assert pd.Period.__module__ == "pandas"
404+
assert pd.Timestamp.__module__ == "pandas"
405+
assert pd.Timedelta.__module__ == "pandas"

pandas/tests/tslibs/test_timezones.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def test_maybe_get_tz_invalid_types():
144144
with pytest.raises(TypeError, match="<class 'module'>"):
145145
timezones.maybe_get_tz(pytest)
146146

147-
msg = "<class 'pandas._libs.tslibs.timestamps.Timestamp'>"
147+
msg = "<class 'pandas.Timestamp'>"
148148
with pytest.raises(TypeError, match=msg):
149149
timezones.maybe_get_tz(Timestamp("2021-01-01", tz="UTC"))
150150

0 commit comments

Comments
 (0)