Skip to content

DOC: Fixing EX01 - Added examples #53725

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

Merged
merged 8 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 0 additions & 5 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.Timestamp.isocalendar \
pandas.Timestamp.isoweekday \
pandas.Timestamp.strptime \
pandas.Timestamp.time \
pandas.Timestamp.timetuple \
pandas.Timestamp.timetz \
pandas.Timestamp.to_datetime64 \
pandas.Timestamp.toordinal \
pandas.Timestamp.tzname \
pandas.Timestamp.utcoffset \
pandas.Timestamp.utctimetuple \
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,10 @@ Datetimelike
- :meth:`DatetimeIndex.map` with ``na_action="ignore"`` now works as expected. (:issue:`51644`)
- Bug in :func:`date_range` when ``freq`` was a :class:`DateOffset` with ``nanoseconds`` (:issue:`46877`)
- Bug in :meth:`Timestamp.round` with values close to the implementation bounds returning incorrect results instead of raising ``OutOfBoundsDatetime`` (:issue:`51494`)
- Bug in :meth:`Timestamp.timetuple` and :meth:`Timestamp.toordinal` were returning incorrect results for inputs outside those supported by the Python standard library's datetime module (:issue:`53668`)
- Bug in :meth:`arrays.DatetimeArray.map` and :meth:`DatetimeIndex.map`, where the supplied callable operated array-wise instead of element-wise (:issue:`51977`)
- Bug in constructing a :class:`Series` or :class:`DataFrame` from a datetime or timedelta scalar always inferring nanosecond resolution instead of inferring from the input (:issue:`52212`)
- Bug in parsing datetime strings with weekday but no day e.g. "2023 Sept Thu" incorrectly raising ``AttributeError`` instead of ``ValueError`` (:issue:`52659`)
-

Timedelta
^^^^^^^^^
Expand Down
72 changes: 67 additions & 5 deletions pandas/_libs/tslibs/nattype.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,16 @@ cdef class _NaT(datetime):

def to_datetime64(self) -> np.datetime64:
"""
Return a numpy.datetime64 object with 'ns' precision.
Return a numpy.datetime64 object with same precision.

Examples
--------
>>> ts = pd.Timestamp(year=2023, month=1, day=1,
... hour=10, second=15)
>>> ts
Timestamp('2023-01-01 10:00:15')
>>> ts.to_datetime64()
numpy.datetime64('2023-01-01T10:00:15.000000')
"""
return np.datetime64("NaT", "ns")

Expand Down Expand Up @@ -509,12 +518,8 @@ class NaTType(_NaT):
date = _make_nat_func("date", datetime.date.__doc__)

utctimetuple = _make_error_func("utctimetuple", datetime)
timetz = _make_error_func("timetz", datetime)
timetuple = _make_error_func("timetuple", datetime)
isocalendar = _make_error_func("isocalendar", datetime)
dst = _make_error_func("dst", datetime)
time = _make_error_func("time", datetime)
toordinal = _make_error_func("toordinal", datetime)
tzname = _make_error_func("tzname", datetime)
utcoffset = _make_error_func("utcoffset", datetime)

Expand All @@ -525,6 +530,63 @@ class NaTType(_NaT):
# The remaining methods have docstrings copy/pasted from the analogous
# Timestamp methods.

time = _make_error_func(
"time",
"""
Return time object with same time but with tzinfo=None.

Examples
--------
>>> ts = pd.Timestamp('2023-01-01 10:00:00')
>>> ts
Timestamp('2023-01-01 10:00:00')
>>> ts.time()
datetime.time(10, 0)
""",
)
timetuple = _make_error_func(
"timetuple",
"""
Return time tuple, compatible with time.localtime().

Examples
--------
>>> ts = pd.Timestamp('2023-01-01 10:00:00')
>>> ts
Timestamp('2023-01-01 10:00:00')
>>> ts.timetuple()
time.struct_time(tm_year=2023, tm_mon=1, tm_mday=1,
tm_hour=10, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=1, tm_isdst=-1)
"""
)
timetz = _make_error_func(
"timetz",
"""
Return time object with same time and tzinfo.

Examples
--------
>>> ts = pd.Timestamp('2023-01-01 10:00:00', tz='Europe/Brussels')
>>> ts
Timestamp('2023-01-01 10:00:00+0100', tz='Europe/Brussels')
>>> ts.timetz()
datetime.time(10, 0, tzinfo=<DstTzInfo 'Europe/Brussels' CET+1:00:00 STD>)
"""
)
toordinal = _make_error_func(
"toordinal",
"""
Return proleptic Gregorian ordinal. January 1 of year 1 is day 1.

Examples
--------
>>> ts = pd.Timestamp('2023-01-01 10:00:50')
>>> ts
Timestamp('2023-01-01 10:00:50')
>>> ts.toordinal()
738521
"""
)
ctime = _make_error_func(
"ctime",
"""
Expand Down
92 changes: 91 additions & 1 deletion pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ from cpython.object cimport (

import_datetime()

import datetime as dt

from pandas._libs.tslibs cimport ccalendar
from pandas._libs.tslibs.base cimport ABCTimestamp

Expand Down Expand Up @@ -1202,7 +1204,16 @@ cdef class _Timestamp(ABCTimestamp):

cpdef to_datetime64(self):
"""
Return a numpy.datetime64 object with 'ns' precision.
Return a numpy.datetime64 object with same precision.

Examples
--------
>>> ts = pd.Timestamp(year=2023, month=1, day=1,
... hour=10, second=15)
>>> ts
Timestamp('2023-01-01 10:00:15')
>>> ts.to_datetime64()
numpy.datetime64('2023-01-01T10:00:15.000000')
"""
# TODO: find a way to construct dt64 directly from _reso
abbrev = npy_unit_to_abbrev(self._creso)
Expand Down Expand Up @@ -1531,6 +1542,85 @@ class Timestamp(_Timestamp):
) from err
return _dt.ctime()

def time(self):
"""
Return time object with same time but with tzinfo=None.

Examples
--------
>>> ts = pd.Timestamp('2023-01-01 10:00:00')
>>> ts
Timestamp('2023-01-01 10:00:00')
>>> ts.time()
datetime.time(10, 0)
"""
_dt = dt.time(self.hour, self.minute, self.second,
self.microsecond, self.tzinfo, fold=self.fold)
return _dt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super().time() probably works here too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


def timetuple(self):
"""
Return time tuple, compatible with time.localtime().

Examples
--------
>>> ts = pd.Timestamp('2023-01-01 10:00:00')
>>> ts
Timestamp('2023-01-01 10:00:00')
>>> ts.timetuple()
time.struct_time(tm_year=2023, tm_mon=1, tm_mday=1,
tm_hour=10, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=1, tm_isdst=-1)
"""
try:
_dt = datetime(self.year, self.month, self.day,
self.hour, self.minute, self.second,
self.microsecond, self.tzinfo, fold=self.fold)
except ValueError as err:
raise NotImplementedError(
"timetuple not yet supported on Timestamps which "
"are outside the range of Python's standard library. "
) from err
return _dt.timetuple()

def timetz(self):
"""
Return time object with same time and tzinfo.

Examples
--------
>>> ts = pd.Timestamp('2023-01-01 10:00:00', tz='Europe/Brussels')
>>> ts
Timestamp('2023-01-01 10:00:00+0100', tz='Europe/Brussels')
>>> ts.timetz()
datetime.time(10, 0, tzinfo=<DstTzInfo 'Europe/Brussels' CET+1:00:00 STD>)
"""
_dt = dt.time(self.hour, self.minute, self.second,
self.microsecond, self.tzinfo)
return _dt

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarcoGorelli - sorry I just saw that here I'm returning dt.time, not timetz.
But should I return super().timetz instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is a super().timetz method

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wait, sorry, there is, that's why there was no definition previously

in which case, yeah, if it doesn't give incorrect results for negative dates or anything, then that looks fine

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to work fine

In [41]: pd.Timestamp('-2000-01-01 01:00:00+01:00').timetz()
Out[41]: datetime.time(1, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600)))

ok to just use this then, thanks for noticing!

def toordinal(self):
"""
Return proleptic Gregorian ordinal. January 1 of year 1 is day 1.

Examples
--------
>>> ts = pd.Timestamp('2023-01-01 10:00:50')
>>> ts
Timestamp('2023-01-01 10:00:50')
>>> ts.toordinal()
738521
"""
try:
_dt = datetime(self.year, self.month, self.day,
self.hour, self.minute, self.second,
self.microsecond, self.tzinfo, fold=self.fold)
except ValueError as err:
raise NotImplementedError(
"toordinal not yet supported on Timestamps which "
"are outside the range of Python's standard library. "
) from err
return _dt.toordinal()

# Issue 25016.
@classmethod
def strptime(cls, date_string, format):
Expand Down
18 changes: 16 additions & 2 deletions pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,9 +1124,23 @@ def test_negative_dates():
# https://github.com/pandas-dev/pandas/issues/50787
ts = Timestamp("-2000-01-01")
msg = (
"^strftime not yet supported on Timestamps which are outside the range of "
" not yet supported on Timestamps which are outside the range of "
"Python's standard library. For now, please call the components you need "
r"\(such as `.year` and `.month`\) and construct your string from there.$"
)
with pytest.raises(NotImplementedError, match=msg):
func = "^strftime"
with pytest.raises(NotImplementedError, match=func + msg):
ts.strftime("%Y")

msg = (
" not yet supported on Timestamps which "
"are outside the range of Python's standard library. "
)

func = "^timetuple"
with pytest.raises(NotImplementedError, match=func + msg):
ts.timetuple()

func = "^toordinal"
with pytest.raises(NotImplementedError, match=func + msg):
ts.toordinal()