Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: set __module__ for pandas scalars (Timestamp/Timedelta/Period) #57976

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ from pandas._libs.tslibs.offsets import (
INVALID_FREQ_ERR_MSG,
BDay,
)
from pandas.util._decorators import set_module

cdef:
enum:
Expand Down Expand Up @@ -2830,6 +2831,7 @@ cdef class _Period(PeriodMixin):
return period_format(self.ordinal, base, fmt)


@set_module("pandas")
class Period(_Period):
"""
Represents a period of time.
Expand Down
5 changes: 3 additions & 2 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -1854,7 +1855,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.
Expand Down Expand Up @@ -1916,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')
Expand Down
5 changes: 3 additions & 2 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -2926,7 +2927,7 @@ timedelta}, default 'raise'
--------
>>> ts = pd.Timestamp(1584226800, unit='s', tz='Europe/Stockholm')
>>> ts.tz
<DstTzInfo 'Europe/Stockholm' CET+1:00:00 STD>
zoneinfo.ZoneInfo(key='Europe/Stockholm')
"""
return self.tzinfo
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,6 @@ def test_util_in_top_level(self):

def test_set_module():
assert pd.DataFrame.__module__ == "pandas"
assert pd.Period.__module__ == "pandas"
assert pd.Timestamp.__module__ == "pandas"
assert pd.Timedelta.__module__ == "pandas"
2 changes: 1 addition & 1 deletion pandas/tests/tslibs/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def test_maybe_get_tz_invalid_types():
with pytest.raises(TypeError, match="<class 'module'>"):
timezones.maybe_get_tz(pytest)

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

Expand Down