Skip to content

Commit f424ff5

Browse files
DeaMariaLeonim-vinicius
authored and
im-vinicius
committed
DOC: Fixing EX01 - Added examples (pandas-dev#53689)
* Added Timestamp examples * Added test and corrected msg * Correcting error on isoweekday * Added requested corrections * added import datetime * Removed isoweekday raise error, corrected time and timetz
1 parent 429053e commit f424ff5

File tree

5 files changed

+134
-10
lines changed

5 files changed

+134
-10
lines changed

ci/code_checks.sh

-4
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
105105
pandas.errors.UnsupportedFunctionCall \
106106
pandas.test \
107107
pandas.NaT \
108-
pandas.Timestamp.date \
109-
pandas.Timestamp.dst \
110-
pandas.Timestamp.isocalendar \
111-
pandas.Timestamp.isoweekday \
112108
pandas.Timestamp.strptime \
113109
pandas.Timestamp.time \
114110
pandas.Timestamp.timetuple \

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ Datetimelike
355355
^^^^^^^^^^^^
356356
- :meth:`DatetimeIndex.map` with ``na_action="ignore"`` now works as expected. (:issue:`51644`)
357357
- Bug in :func:`date_range` when ``freq`` was a :class:`DateOffset` with ``nanoseconds`` (:issue:`46877`)
358+
- Bug in :meth:`Timestamp.date`, :meth:`Timestamp.isocalendar` were returning incorrect results for inputs outside those supported by the Python standard library's datetime module (:issue:`53668`)
358359
- Bug in :meth:`Timestamp.round` with values close to the implementation bounds returning incorrect results instead of raising ``OutOfBoundsDatetime`` (:issue:`51494`)
359360
- Bug in :meth:`arrays.DatetimeArray.map` and :meth:`DatetimeIndex.map`, where the supplied callable operated array-wise instead of element-wise (:issue:`51977`)
360361
- 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`)

pandas/_libs/tslibs/nattype.pyx

+50-4
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,14 @@ class NaTType(_NaT):
437437
Return the day of the week represented by the date.
438438
439439
Monday == 1 ... Sunday == 7.
440+
441+
Examples
442+
--------
443+
>>> ts = pd.Timestamp('2023-01-01 10:00:00')
444+
>>> ts
445+
Timestamp('2023-01-01 10:00:00')
446+
>>> ts.isoweekday()
447+
7
440448
""",
441449
)
442450
total_seconds = _make_nan_func(
@@ -506,13 +514,9 @@ class NaTType(_NaT):
506514
""",
507515
)
508516
# _nat_methods
509-
date = _make_nat_func("date", datetime.date.__doc__)
510-
511517
utctimetuple = _make_error_func("utctimetuple", datetime)
512518
timetz = _make_error_func("timetz", datetime)
513519
timetuple = _make_error_func("timetuple", datetime)
514-
isocalendar = _make_error_func("isocalendar", datetime)
515-
dst = _make_error_func("dst", datetime)
516520
time = _make_error_func("time", datetime)
517521
toordinal = _make_error_func("toordinal", datetime)
518522
tzname = _make_error_func("tzname", datetime)
@@ -524,6 +528,48 @@ class NaTType(_NaT):
524528
# ----------------------------------------------------------------------
525529
# The remaining methods have docstrings copy/pasted from the analogous
526530
# Timestamp methods.
531+
isocalendar = _make_error_func(
532+
"isocalendar",
533+
"""
534+
Return a named tuple containing ISO year, week number, and weekday.
535+
536+
Examples
537+
--------
538+
>>> ts = pd.Timestamp('2023-01-01 10:00:00')
539+
>>> ts
540+
Timestamp('2023-01-01 10:00:00')
541+
>>> ts.isocalendar()
542+
datetime.IsoCalendarDate(year=2022, week=52, weekday=7)
543+
"""
544+
)
545+
dst = _make_error_func(
546+
"dst",
547+
"""
548+
Return the daylight saving time (DST) adjustment.
549+
550+
Examples
551+
--------
552+
>>> ts = pd.Timestamp('2000-06-01 00:00:00', tz='Europe/Brussels')
553+
>>> ts
554+
Timestamp('2000-06-01 00:00:00+0200', tz='Europe/Brussels')
555+
>>> ts.dst()
556+
datetime.timedelta(seconds=3600)
557+
"""
558+
)
559+
date = _make_nat_func(
560+
"date",
561+
"""
562+
Return date object with same year, month and day.
563+
564+
Examples
565+
--------
566+
>>> ts = pd.Timestamp('2023-01-01 10:00:00.00')
567+
>>> ts
568+
Timestamp('2023-01-01 10:00:00')
569+
>>> ts.date()
570+
datetime.date(2023, 1, 1)
571+
"""
572+
)
527573

528574
ctime = _make_error_func(
529575
"ctime",

pandas/_libs/tslibs/timestamps.pyx

+69
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ from cpython.object cimport (
4545

4646
import_datetime()
4747

48+
import datetime as dt
49+
4850
from pandas._libs.tslibs cimport ccalendar
4951
from pandas._libs.tslibs.base cimport ABCTimestamp
5052

@@ -1531,6 +1533,64 @@ class Timestamp(_Timestamp):
15311533
) from err
15321534
return _dt.ctime()
15331535

1536+
def date(self):
1537+
"""
1538+
Return date object with same year, month and day.
1539+
1540+
Examples
1541+
--------
1542+
>>> ts = pd.Timestamp('2023-01-01 10:00:00.00')
1543+
>>> ts
1544+
Timestamp('2023-01-01 10:00:00')
1545+
>>> ts.date()
1546+
datetime.date(2023, 1, 1)
1547+
"""
1548+
try:
1549+
_dt = dt.date(self.year, self.month, self.day)
1550+
except ValueError as err:
1551+
raise NotImplementedError(
1552+
"date not yet supported on Timestamps which "
1553+
"are outside the range of Python's standard library. "
1554+
) from err
1555+
return _dt
1556+
1557+
def dst(self):
1558+
"""
1559+
Return the daylight saving time (DST) adjustment.
1560+
1561+
Examples
1562+
--------
1563+
>>> ts = pd.Timestamp('2000-06-01 00:00:00', tz='Europe/Brussels')
1564+
>>> ts
1565+
Timestamp('2000-06-01 00:00:00+0200', tz='Europe/Brussels')
1566+
>>> ts.dst()
1567+
datetime.timedelta(seconds=3600)
1568+
"""
1569+
return super().dst()
1570+
1571+
def isocalendar(self):
1572+
"""
1573+
Return a named tuple containing ISO year, week number, and weekday.
1574+
1575+
Examples
1576+
--------
1577+
>>> ts = pd.Timestamp('2023-01-01 10:00:00')
1578+
>>> ts
1579+
Timestamp('2023-01-01 10:00:00')
1580+
>>> ts.isocalendar()
1581+
datetime.IsoCalendarDate(year=2022, week=52, weekday=7)
1582+
"""
1583+
try:
1584+
_dt = datetime(self.year, self.month, self.day,
1585+
self.hour, self.minute, self.second,
1586+
self.microsecond, self.tzinfo, fold=self.fold)
1587+
except ValueError as err:
1588+
raise NotImplementedError(
1589+
"isocalendar not yet supported on Timestamps which "
1590+
"are outside the range of Python's standard library. "
1591+
) from err
1592+
return _dt.isocalendar()
1593+
15341594
# Issue 25016.
15351595
@classmethod
15361596
def strptime(cls, date_string, format):
@@ -2384,9 +2444,18 @@ default 'raise'
23842444
Return the day of the week represented by the date.
23852445
23862446
Monday == 1 ... Sunday == 7.
2447+
2448+
Examples
2449+
--------
2450+
>>> ts = pd.Timestamp('2023-01-01 10:00:00')
2451+
>>> ts
2452+
Timestamp('2023-01-01 10:00:00')
2453+
>>> ts.isoweekday()
2454+
7
23872455
"""
23882456
# same as super().isoweekday(), but that breaks because of how
23892457
# we have overridden year, see note in create_timestamp_from_ts
2458+
23902459
return self.weekday() + 1
23912460

23922461
def weekday(self):

pandas/tests/scalar/timestamp/test_timestamp.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -1124,9 +1124,21 @@ def test_negative_dates():
11241124
# https://github.com/pandas-dev/pandas/issues/50787
11251125
ts = Timestamp("-2000-01-01")
11261126
msg = (
1127-
"^strftime not yet supported on Timestamps which are outside the range of "
1127+
" not yet supported on Timestamps which are outside the range of "
11281128
"Python's standard library. For now, please call the components you need "
11291129
r"\(such as `.year` and `.month`\) and construct your string from there.$"
11301130
)
1131-
with pytest.raises(NotImplementedError, match=msg):
1131+
func = "^strftime"
1132+
with pytest.raises(NotImplementedError, match=func + msg):
11321133
ts.strftime("%Y")
1134+
1135+
msg = (
1136+
" not yet supported on Timestamps which "
1137+
"are outside the range of Python's standard library. "
1138+
)
1139+
func = "^date"
1140+
with pytest.raises(NotImplementedError, match=func + msg):
1141+
ts.date()
1142+
func = "^isocalendar"
1143+
with pytest.raises(NotImplementedError, match=func + msg):
1144+
ts.isocalendar()

0 commit comments

Comments
 (0)