Skip to content

Commit 156e67e

Browse files
Fix docstring timestamps (Issue #59458) (#59688)
--------- Co-authored-by: Marco Gorelli <[email protected]>
1 parent e531732 commit 156e67e

File tree

3 files changed

+81
-37
lines changed

3 files changed

+81
-37
lines changed

Diff for: ci/code_checks.sh

-2
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,8 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
8181
-i "pandas.Timedelta.resolution PR02" \
8282
-i "pandas.Timestamp.max PR02" \
8383
-i "pandas.Timestamp.min PR02" \
84-
-i "pandas.Timestamp.nanosecond GL08" \
8584
-i "pandas.Timestamp.resolution PR02" \
8685
-i "pandas.Timestamp.tzinfo GL08" \
87-
-i "pandas.Timestamp.year GL08" \
8886
-i "pandas.api.types.is_re_compilable PR07,SA01" \
8987
-i "pandas.api.types.pandas_dtype PR07,RT03,SA01" \
9088
-i "pandas.arrays.ArrowExtensionArray PR07,SA01" \

Diff for: pandas/_libs/tslibs/timestamps.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ cdef _Timestamp create_timestamp_from_ts(int64_t value,
2121

2222
cdef class _Timestamp(ABCTimestamp):
2323
cdef readonly:
24-
int64_t _value, nanosecond, year
24+
int64_t _value, _nanosecond, _year
2525
NPY_DATETIMEUNIT _creso
2626

2727
cdef bint _get_start_end_field(self, str field, freq)

Diff for: pandas/_libs/tslibs/timestamps.pyx

+80-34
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ cdef _Timestamp create_timestamp_from_ts(
162162
dts.sec, dts.us, tz, fold=fold)
163163

164164
ts_base._value = value
165-
ts_base.year = dts.year
166-
ts_base.nanosecond = dts.ps // 1000
165+
ts_base._year = dts.year
166+
ts_base._nanosecond = dts.ps // 1000
167167
ts_base._creso = reso
168168

169169
return ts_base
@@ -356,9 +356,9 @@ cdef class _Timestamp(ABCTimestamp):
356356
# -----------------------------------------------------------------
357357

358358
def __hash__(_Timestamp self):
359-
if self.nanosecond:
359+
if self._nanosecond:
360360
return hash(self._value)
361-
if not (1 <= self.year <= 9999):
361+
if not (1 <= self._year <= 9999):
362362
# out of bounds for pydatetime
363363
return hash(self._value)
364364
if self.fold:
@@ -376,7 +376,7 @@ cdef class _Timestamp(ABCTimestamp):
376376
elif cnp.is_datetime64_object(other):
377377
ots = Timestamp(other)
378378
elif PyDateTime_Check(other):
379-
if self.nanosecond == 0:
379+
if self._nanosecond == 0:
380380
val = self.to_pydatetime()
381381
return PyObject_RichCompareBool(val, other, op)
382382

@@ -455,7 +455,7 @@ cdef class _Timestamp(ABCTimestamp):
455455
if not self._can_compare(other):
456456
return NotImplemented
457457

458-
if self.nanosecond == 0:
458+
if self._nanosecond == 0:
459459
return PyObject_RichCompareBool(dtval, other, op)
460460

461461
# otherwise we have dtval < self
@@ -464,9 +464,9 @@ cdef class _Timestamp(ABCTimestamp):
464464
if op == Py_EQ:
465465
return False
466466
if op == Py_LE or op == Py_LT:
467-
return self.year <= other.year
467+
return self._year <= other.year
468468
if op == Py_GE or op == Py_GT:
469-
return self.year >= other.year
469+
return self._year >= other.year
470470

471471
cdef bint _can_compare(self, datetime other):
472472
if self.tzinfo is not None:
@@ -607,7 +607,7 @@ cdef class _Timestamp(ABCTimestamp):
607607

608608
if own_tz is not None and not is_utc(own_tz):
609609
pydatetime_to_dtstruct(self, &dts)
610-
val = npy_datetimestruct_to_datetime(self._creso, &dts) + self.nanosecond
610+
val = npy_datetimestruct_to_datetime(self._creso, &dts) + self._nanosecond
611611
else:
612612
val = self._value
613613
return val
@@ -899,7 +899,7 @@ cdef class _Timestamp(ABCTimestamp):
899899
>>> ts.is_leap_year
900900
True
901901
"""
902-
return bool(ccalendar.is_leapyear(self.year))
902+
return bool(ccalendar.is_leapyear(self._year))
903903
904904
@property
905905
def day_of_week(self) -> int:
@@ -943,7 +943,7 @@ cdef class _Timestamp(ABCTimestamp):
943943
>>> ts.day_of_year
944944
74
945945
"""
946-
return ccalendar.get_day_of_year(self.year, self.month, self.day)
946+
return ccalendar.get_day_of_year(self._year, self.month, self.day)
947947
948948
@property
949949
def quarter(self) -> int:
@@ -1030,6 +1030,29 @@ cdef class _Timestamp(ABCTimestamp):
10301030
"""
10311031
return super().fold
10321032
1033+
@property
1034+
def year(self) -> int:
1035+
"""
1036+
Return the year of the Timestamp.
1037+
1038+
Returns
1039+
-------
1040+
int
1041+
The year of the Timestamp.
1042+
1043+
See Also
1044+
--------
1045+
Timestamp.month : Return the month of the Timestamp.
1046+
Timestamp.day : Return the day of the Timestamp.
1047+
1048+
Examples
1049+
--------
1050+
>>> ts = pd.Timestamp("2024-08-31 16:16:30")
1051+
>>> ts.year
1052+
2024
1053+
"""
1054+
return self._year
1055+
10331056
@property
10341057
def month(self) -> int:
10351058
"""
@@ -1145,6 +1168,29 @@ cdef class _Timestamp(ABCTimestamp):
11451168
"""
11461169
return super().microsecond
11471170
1171+
@property
1172+
def nanosecond(self) -> int:
1173+
"""
1174+
Return the nanosecond of the Timestamp.
1175+
1176+
Returns
1177+
-------
1178+
int
1179+
The nanosecond of the Timestamp.
1180+
1181+
See Also
1182+
--------
1183+
Timestamp.second : Return the second of the Timestamp.
1184+
Timestamp.microsecond : Return the microsecond of the Timestamp.
1185+
1186+
Examples
1187+
--------
1188+
>>> ts = pd.Timestamp("2024-08-31 16:16:30.230400015")
1189+
>>> ts.nanosecond
1190+
15
1191+
"""
1192+
return self._nanosecond
1193+
11481194
@property
11491195
def week(self) -> int:
11501196
"""
@@ -1165,7 +1211,7 @@ cdef class _Timestamp(ABCTimestamp):
11651211
>>> ts.week
11661212
11
11671213
"""
1168-
return ccalendar.get_week_of_year(self.year, self.month, self.day)
1214+
return ccalendar.get_week_of_year(self._year, self.month, self.day)
11691215
11701216
@property
11711217
def days_in_month(self) -> int:
@@ -1187,7 +1233,7 @@ cdef class _Timestamp(ABCTimestamp):
11871233
>>> ts.days_in_month
11881234
31
11891235
"""
1190-
return ccalendar.get_days_in_month(self.year, self.month)
1236+
return ccalendar.get_days_in_month(self._year, self.month)
11911237
11921238
# -----------------------------------------------------------------
11931239
# Transformation Methods
@@ -1261,7 +1307,7 @@ cdef class _Timestamp(ABCTimestamp):
12611307

12621308
The full format looks like 'YYYY-MM-DD HH:MM:SS.mmmmmmnnn'.
12631309
By default, the fractional part is omitted if self.microsecond == 0
1264-
and self.nanosecond == 0.
1310+
and self._nanosecond == 0.
12651311

12661312
If self.tzinfo is not None, the UTC offset is also attached, giving
12671313
giving a full format of 'YYYY-MM-DD HH:MM:SS.mmmmmmnnn+HH:MM'.
@@ -1297,21 +1343,21 @@ cdef class _Timestamp(ABCTimestamp):
12971343
base_ts = "microseconds" if timespec == "nanoseconds" else timespec
12981344
base = super(_Timestamp, self).isoformat(sep=sep, timespec=base_ts)
12991345
# We need to replace the fake year 1970 with our real year
1300-
base = f"{self.year:04d}-" + base.split("-", 1)[1]
1346+
base = f"{self._year:04d}-" + base.split("-", 1)[1]
13011347
1302-
if self.nanosecond == 0 and timespec != "nanoseconds":
1348+
if self._nanosecond == 0 and timespec != "nanoseconds":
13031349
return base
13041350
13051351
if self.tzinfo is not None:
13061352
base1, base2 = base[:-6], base[-6:]
13071353
else:
13081354
base1, base2 = base, ""
13091355
1310-
if timespec == "nanoseconds" or (timespec == "auto" and self.nanosecond):
1356+
if timespec == "nanoseconds" or (timespec == "auto" and self._nanosecond):
13111357
if self.microsecond or timespec == "nanoseconds":
1312-
base1 += f"{self.nanosecond:03d}"
1358+
base1 += f"{self._nanosecond:03d}"
13131359
else:
1314-
base1 += f".{self.nanosecond:09d}"
1360+
base1 += f".{self._nanosecond:09d}"
13151361
13161362
return base1 + base2
13171363
@@ -1345,14 +1391,14 @@ cdef class _Timestamp(ABCTimestamp):
13451391
def _date_repr(self) -> str:
13461392
# Ideal here would be self.strftime("%Y-%m-%d"), but
13471393
# the datetime strftime() methods require year >= 1900 and is slower
1348-
return f"{self.year}-{self.month:02d}-{self.day:02d}"
1394+
return f"{self._year}-{self.month:02d}-{self.day:02d}"
13491395
13501396
@property
13511397
def _time_repr(self) -> str:
13521398
result = f"{self.hour:02d}:{self.minute:02d}:{self.second:02d}"
13531399
1354-
if self.nanosecond != 0:
1355-
result += f".{self.nanosecond + 1000 * self.microsecond:09d}"
1400+
if self._nanosecond != 0:
1401+
result += f".{self._nanosecond + 1000 * self.microsecond:09d}"
13561402
elif self.microsecond != 0:
13571403
result += f".{self.microsecond:06d}"
13581404
@@ -1516,11 +1562,11 @@ cdef class _Timestamp(ABCTimestamp):
15161562
>>> pd.NaT.to_pydatetime()
15171563
NaT
15181564
"""
1519-
if self.nanosecond != 0 and warn:
1565+
if self._nanosecond != 0 and warn:
15201566
warnings.warn("Discarding nonzero nanoseconds in conversion.",
15211567
UserWarning, stacklevel=find_stack_level())
15221568
1523-
return datetime(self.year, self.month, self.day,
1569+
return datetime(self._year, self.month, self.day,
15241570
self.hour, self.minute, self.second,
15251571
self.microsecond, self.tzinfo, fold=self.fold)
15261572
@@ -1999,7 +2045,7 @@ class Timestamp(_Timestamp):
19992045
'2020-03-14 15:32:52'
20002046
"""
20012047
try:
2002-
_dt = datetime(self.year, self.month, self.day,
2048+
_dt = datetime(self._year, self.month, self.day,
20032049
self.hour, self.minute, self.second,
20042050
self.microsecond, self.tzinfo, fold=self.fold)
20052051
except ValueError as err:
@@ -2042,7 +2088,7 @@ class Timestamp(_Timestamp):
20422088
'Sun Jan 1 10:00:00 2023'
20432089
"""
20442090
try:
2045-
_dt = datetime(self.year, self.month, self.day,
2091+
_dt = datetime(self._year, self.month, self.day,
20462092
self.hour, self.minute, self.second,
20472093
self.microsecond, self.tzinfo, fold=self.fold)
20482094
except ValueError as err:
@@ -2082,7 +2128,7 @@ class Timestamp(_Timestamp):
20822128
datetime.date(2023, 1, 1)
20832129
"""
20842130
try:
2085-
_dt = dt.date(self.year, self.month, self.day)
2131+
_dt = dt.date(self._year, self.month, self.day)
20862132
except ValueError as err:
20872133
raise NotImplementedError(
20882134
"date not yet supported on Timestamps which "
@@ -2131,7 +2177,7 @@ class Timestamp(_Timestamp):
21312177
datetime.IsoCalendarDate(year=2022, week=52, weekday=7)
21322178
"""
21332179
try:
2134-
_dt = datetime(self.year, self.month, self.day,
2180+
_dt = datetime(self._year, self.month, self.day,
21352181
self.hour, self.minute, self.second,
21362182
self.microsecond, self.tzinfo, fold=self.fold)
21372183
except ValueError as err:
@@ -2273,7 +2319,7 @@ class Timestamp(_Timestamp):
22732319
tm_hour=10, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=1, tm_isdst=-1)
22742320
"""
22752321
try:
2276-
_dt = datetime(self.year, self.month, self.day,
2322+
_dt = datetime(self._year, self.month, self.day,
22772323
self.hour, self.minute, self.second,
22782324
self.microsecond, self.tzinfo, fold=self.fold)
22792325
except ValueError as err:
@@ -2334,7 +2380,7 @@ class Timestamp(_Timestamp):
23342380
738521
23352381
"""
23362382
try:
2337-
_dt = datetime(self.year, self.month, self.day,
2383+
_dt = datetime(self._year, self.month, self.day,
23382384
self.hour, self.minute, self.second,
23392385
self.microsecond, self.tzinfo, fold=self.fold)
23402386
except ValueError as err:
@@ -3223,7 +3269,7 @@ default 'raise'
32233269
32243270
# setup components
32253271
pandas_datetime_to_datetimestruct(value, self._creso, &dts)
3226-
dts.ps = self.nanosecond * 1000
3272+
dts.ps = self._nanosecond * 1000
32273273
32283274
# replace
32293275
def validate(k, v):
@@ -3313,7 +3359,7 @@ default 'raise'
33133359
>>> ts.to_julian_date()
33143360
2458923.147824074
33153361
"""
3316-
year = self.year
3362+
year = self._year
33173363
month = self.month
33183364
day = self.day
33193365
if month <= 2:
@@ -3330,7 +3376,7 @@ default 'raise'
33303376
self.minute / 60.0 +
33313377
self.second / 3600.0 +
33323378
self.microsecond / 3600.0 / 1e+6 +
3333-
self.nanosecond / 3600.0 / 1e+9
3379+
self._nanosecond / 3600.0 / 1e+9
33343380
) / 24.0)
33353381
33363382
def isoweekday(self):
@@ -3381,7 +3427,7 @@ default 'raise'
33813427
"""
33823428
# same as super().weekday(), but that breaks because of how
33833429
# we have overridden year, see note in create_timestamp_from_ts
3384-
return ccalendar.dayofweek(self.year, self.month, self.day)
3430+
return ccalendar.dayofweek(self._year, self.month, self.day)
33853431
33863432
33873433
# Aliases

0 commit comments

Comments
 (0)