Skip to content

DOC: Fixing EX01 - Added examples #53689

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 10 commits into from
Jun 20, 2023
4 changes: 0 additions & 4 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.errors.UnsupportedFunctionCall \
pandas.test \
pandas.NaT \
pandas.Timestamp.date \
pandas.Timestamp.dst \
pandas.Timestamp.isocalendar \
pandas.Timestamp.isoweekday \
pandas.Timestamp.strptime \
pandas.Timestamp.time \
pandas.Timestamp.timetuple \
Expand Down
53 changes: 49 additions & 4 deletions pandas/_libs/tslibs/nattype.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,14 @@ class NaTType(_NaT):
Return the day of the week represented by the date.

Monday == 1 ... Sunday == 7.

Examples
--------
>>> ts = pd.Timestamp('2023-01-01 10:00:00')
>>> ts
Timestamp('2023-01-01 10:00:00')
>>> ts.isoweekday()
7
""",
)
total_seconds = _make_nan_func(
Expand Down Expand Up @@ -506,13 +514,9 @@ class NaTType(_NaT):
""",
)
# _nat_methods
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)
Expand All @@ -524,6 +528,47 @@ class NaTType(_NaT):
# ----------------------------------------------------------------------
# The remaining methods have docstrings copy/pasted from the analogous
# Timestamp methods.
isocalendar = _make_error_func(
"isocalendar",
"""
Return a named tuple containing ISO year, week number, and weekday.

Examples
--------
>>> ts = pd.Timestamp('2023-01-01 10:00:00')
>>> ts
Timestamp('2023-01-01 10:00:00')
>>> ts.isocalendar()
datetime.IsoCalendarDate(year=2022, week=52, weekday=7)
"""
)
dst = _make_error_func(
"dst",
"""
Return self.tzinfo.dst(self).

Examples
--------
>>> ts = pd.Timestamp('2000-06-01 00:00:00',
... tz='Europe/Brussels').dst()
>>> ts
datetime.timedelta(seconds=3600)
"""
)
date = _make_nat_func(
"date",
"""
Return date object with same year, month and day.

Examples
--------
>>> ts = pd.Timestamp('2023-01-01 10:00:00.00')
>>> ts
Timestamp('2023-01-01 10:00:00')
>>> ts.date()
datetime.date(2023, 1, 1)
"""
)

ctime = _make_error_func(
"ctime",
Expand Down
83 changes: 82 additions & 1 deletion pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,70 @@ class Timestamp(_Timestamp):
) from err
return _dt.ctime()

def date(self):
"""
Return date object with same year, month and day.

Examples
--------
>>> ts = pd.Timestamp('2023-01-01 10:00:00.00')
>>> ts
Timestamp('2023-01-01 10:00:00')
>>> ts.date()
datetime.date(2023, 1, 1)
"""
try:
_dt = datetime(self.year, self.month, self.day,
self.hour, self.minute, self.second,
self.microsecond, self.tzinfo, fold=self.fold)
Copy link
Member

@MarcoGorelli MarcoGorelli Jun 19, 2023

Choose a reason for hiding this comment

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

can we construct a date object directly, and then return that? like

_dt = date(self.year, self.month, self.day)

, where date is imported from datetime?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the corrections. :)
I did all of them locally except this one. I can't find how to import date from datetime without breaking it.

Copy link
Member

Choose a reason for hiding this comment

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

might work to do import datetime as dt, then use dt.date

except ValueError as err:
raise NotImplementedError(
"date not yet supported on Timestamps which "
"are outside the range of Python's standard library. "
"For now, please call the components you need (such as `.year` "
"and `.month`) and construct your string from there."
Copy link
Member

Choose a reason for hiding this comment

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

we're not constructing a string, so we can remove this part of the message

) from err
return _dt.date()

def dst(self):
"""
Return self.tzinfo.dst(self).
Copy link
Member

Choose a reason for hiding this comment

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

How about

Return the daylight saving time (DST) adjustment.


Examples
--------
>>> ts = pd.Timestamp('2000-06-01 00:00:00',
... tz='Europe/Brussels').dst()
>>> ts
datetime.timedelta(seconds=3600)
Copy link
Member

Choose a reason for hiding this comment

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

ts usually means "timestamp", so maybe let's do

>>> ts = pd.Timestamp('2000-06-01 00:00:00', tz='Europe/Brussels')
>>> ts.dst()
datetime.timedelta(seconds=3600)

"""
_dt = datetime(self.year, self.month, self.day,
self.hour, self.minute, self.second,
self.microsecond, self.tzinfo, fold=self.fold)
return _dt.dst()
Copy link
Member

@MarcoGorelli MarcoGorelli Jun 19, 2023

Choose a reason for hiding this comment

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

looks like we're not catching anything here, so can we just return return super().dst()?

maybe we can do the same for the others where we're just overriding the docstrings, like
EDIT: please disregard the second half of this message, only the first sentence (above) makes sense


def isocalendar(self):
"""
Return a named tuple containing ISO year, week number, and weekday.

Examples
--------
>>> ts = pd.Timestamp('2023-01-01 10:00:00')
>>> ts
Timestamp('2023-01-01 10:00:00')
>>> ts.isocalendar()
datetime.IsoCalendarDate(year=2022, week=52, weekday=7)
"""
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(
"isocalendar not yet supported on Timestamps which "
"are outside the range of Python's standard library. "
) from err
return _dt.isocalendar()

# Issue 25016.
@classmethod
def strptime(cls, date_string, format):
Expand Down Expand Up @@ -2384,10 +2448,27 @@ default 'raise'
Return the day of the week represented by the date.

Monday == 1 ... Sunday == 7.

Examples
--------
>>> ts = pd.Timestamp('2023-01-01 10:00:00')
>>> ts
Timestamp('2023-01-01 10:00:00')
>>> ts.isoweekday()
7
"""
# same as super().isoweekday(), but that breaks because of how
# we have overridden year, see note in create_timestamp_from_ts
return self.weekday() + 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(
"isocalendar not yet supported on Timestamps which "
"are outside the range of Python's standard library. "
) from err
return _dt.weekday() + 1

def weekday(self):
"""
Expand Down
20 changes: 18 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,25 @@ 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")
func = "^date"
with pytest.raises(NotImplementedError, match=func + msg):
ts.date()

msg = (
"isocalendar not yet supported on Timestamps which "
"are outside the range of Python's standard library. "
)
with pytest.raises(NotImplementedError, match=msg):
ts.isocalendar()
with pytest.raises(NotImplementedError, match=msg):
ts.isoweekday()

with pytest.raises(pytz.NonExistentTimeError, match="-2000-01-01 00:00:00"):
Timestamp("-2000", tz="Europe/Brussels").dst()