@@ -162,8 +162,8 @@ cdef _Timestamp create_timestamp_from_ts(
162
162
dts.sec, dts.us, tz, fold = fold)
163
163
164
164
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
167
167
ts_base._creso = reso
168
168
169
169
return ts_base
@@ -356,9 +356,9 @@ cdef class _Timestamp(ABCTimestamp):
356
356
# -----------------------------------------------------------------
357
357
358
358
def __hash__ (_Timestamp self ):
359
- if self .nanosecond :
359
+ if self ._nanosecond :
360
360
return hash (self ._value)
361
- if not (1 <= self .year <= 9999 ):
361
+ if not (1 <= self ._year <= 9999 ):
362
362
# out of bounds for pydatetime
363
363
return hash (self ._value)
364
364
if self .fold:
@@ -376,7 +376,7 @@ cdef class _Timestamp(ABCTimestamp):
376
376
elif cnp.is_datetime64_object(other):
377
377
ots = Timestamp(other)
378
378
elif PyDateTime_Check(other):
379
- if self .nanosecond == 0 :
379
+ if self ._nanosecond == 0 :
380
380
val = self .to_pydatetime()
381
381
return PyObject_RichCompareBool(val, other, op)
382
382
@@ -455,7 +455,7 @@ cdef class _Timestamp(ABCTimestamp):
455
455
if not self ._can_compare(other):
456
456
return NotImplemented
457
457
458
- if self .nanosecond == 0 :
458
+ if self ._nanosecond == 0 :
459
459
return PyObject_RichCompareBool(dtval, other, op)
460
460
461
461
# otherwise we have dtval < self
@@ -464,9 +464,9 @@ cdef class _Timestamp(ABCTimestamp):
464
464
if op == Py_EQ:
465
465
return False
466
466
if op == Py_LE or op == Py_LT:
467
- return self .year <= other.year
467
+ return self ._year <= other.year
468
468
if op == Py_GE or op == Py_GT:
469
- return self .year >= other.year
469
+ return self ._year >= other.year
470
470
471
471
cdef bint _can_compare(self , datetime other):
472
472
if self .tzinfo is not None :
@@ -607,7 +607,7 @@ cdef class _Timestamp(ABCTimestamp):
607
607
608
608
if own_tz is not None and not is_utc(own_tz):
609
609
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
611
611
else :
612
612
val = self ._value
613
613
return val
@@ -899,7 +899,7 @@ cdef class _Timestamp(ABCTimestamp):
899
899
>>> ts.is_leap_year
900
900
True
901
901
"""
902
- return bool(ccalendar.is_leapyear(self.year ))
902
+ return bool(ccalendar.is_leapyear(self._year ))
903
903
904
904
@property
905
905
def day_of_week(self) -> int:
@@ -943,7 +943,7 @@ cdef class _Timestamp(ABCTimestamp):
943
943
>>> ts.day_of_year
944
944
74
945
945
"""
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)
947
947
948
948
@property
949
949
def quarter(self) -> int:
@@ -1030,6 +1030,29 @@ cdef class _Timestamp(ABCTimestamp):
1030
1030
"""
1031
1031
return super().fold
1032
1032
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
+
1033
1056
@property
1034
1057
def month(self) -> int:
1035
1058
"""
@@ -1145,6 +1168,29 @@ cdef class _Timestamp(ABCTimestamp):
1145
1168
"""
1146
1169
return super().microsecond
1147
1170
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
+
1148
1194
@property
1149
1195
def week(self) -> int:
1150
1196
"""
@@ -1165,7 +1211,7 @@ cdef class _Timestamp(ABCTimestamp):
1165
1211
>>> ts.week
1166
1212
11
1167
1213
"""
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)
1169
1215
1170
1216
@property
1171
1217
def days_in_month(self) -> int:
@@ -1187,7 +1233,7 @@ cdef class _Timestamp(ABCTimestamp):
1187
1233
>>> ts.days_in_month
1188
1234
31
1189
1235
"""
1190
- return ccalendar.get_days_in_month(self.year , self.month)
1236
+ return ccalendar.get_days_in_month(self._year , self.month)
1191
1237
1192
1238
# -----------------------------------------------------------------
1193
1239
# Transformation Methods
@@ -1261,7 +1307,7 @@ cdef class _Timestamp(ABCTimestamp):
1261
1307
1262
1308
The full format looks like ' YYYY-MM-DD HH:MM:SS.mmmmmmnnn' .
1263
1309
By default, the fractional part is omitted if self .microsecond == 0
1264
- and self .nanosecond == 0.
1310
+ and self ._nanosecond == 0.
1265
1311
1266
1312
If self .tzinfo is not None , the UTC offset is also attached, giving
1267
1313
giving a full format of ' YYYY-MM-DD HH:MM:SS.mmmmmmnnn+HH:MM' .
@@ -1297,21 +1343,21 @@ cdef class _Timestamp(ABCTimestamp):
1297
1343
base_ts = "microseconds" if timespec == "nanoseconds" else timespec
1298
1344
base = super(_Timestamp, self).isoformat(sep=sep, timespec=base_ts)
1299
1345
# 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]
1301
1347
1302
- if self.nanosecond == 0 and timespec != "nanoseconds":
1348
+ if self._nanosecond == 0 and timespec != "nanoseconds":
1303
1349
return base
1304
1350
1305
1351
if self.tzinfo is not None:
1306
1352
base1, base2 = base[:-6], base[-6:]
1307
1353
else:
1308
1354
base1, base2 = base, ""
1309
1355
1310
- if timespec == "nanoseconds" or (timespec == "auto" and self.nanosecond ):
1356
+ if timespec == "nanoseconds" or (timespec == "auto" and self._nanosecond ):
1311
1357
if self.microsecond or timespec == "nanoseconds":
1312
- base1 += f"{self.nanosecond :03d}"
1358
+ base1 += f"{self._nanosecond :03d}"
1313
1359
else:
1314
- base1 += f".{self.nanosecond :09d}"
1360
+ base1 += f".{self._nanosecond :09d}"
1315
1361
1316
1362
return base1 + base2
1317
1363
@@ -1345,14 +1391,14 @@ cdef class _Timestamp(ABCTimestamp):
1345
1391
def _date_repr(self) -> str:
1346
1392
# Ideal here would be self.strftime("%Y -%m -%d "), but
1347
1393
# 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}"
1349
1395
1350
1396
@property
1351
1397
def _time_repr(self) -> str:
1352
1398
result = f"{self.hour:02d}:{self.minute:02d}:{self.second:02d}"
1353
1399
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}"
1356
1402
elif self.microsecond != 0:
1357
1403
result += f".{self.microsecond:06d}"
1358
1404
@@ -1516,11 +1562,11 @@ cdef class _Timestamp(ABCTimestamp):
1516
1562
>>> pd.NaT.to_pydatetime()
1517
1563
NaT
1518
1564
"""
1519
- if self.nanosecond != 0 and warn:
1565
+ if self._nanosecond != 0 and warn:
1520
1566
warnings.warn("Discarding nonzero nanoseconds in conversion.",
1521
1567
UserWarning, stacklevel=find_stack_level())
1522
1568
1523
- return datetime(self.year , self.month, self.day,
1569
+ return datetime(self._year , self.month, self.day,
1524
1570
self.hour, self.minute, self.second,
1525
1571
self.microsecond, self.tzinfo, fold=self.fold)
1526
1572
@@ -1999,7 +2045,7 @@ class Timestamp(_Timestamp):
1999
2045
' 2020-03-14 15:32:52'
2000
2046
"""
2001
2047
try:
2002
- _dt = datetime(self.year , self.month, self.day,
2048
+ _dt = datetime(self._year , self.month, self.day,
2003
2049
self.hour, self.minute, self.second,
2004
2050
self.microsecond, self.tzinfo, fold=self.fold)
2005
2051
except ValueError as err:
@@ -2042,7 +2088,7 @@ class Timestamp(_Timestamp):
2042
2088
' Sun Jan 1 10:00:00 2023'
2043
2089
"""
2044
2090
try:
2045
- _dt = datetime(self.year , self.month, self.day,
2091
+ _dt = datetime(self._year , self.month, self.day,
2046
2092
self.hour, self.minute, self.second,
2047
2093
self.microsecond, self.tzinfo, fold=self.fold)
2048
2094
except ValueError as err:
@@ -2082,7 +2128,7 @@ class Timestamp(_Timestamp):
2082
2128
datetime.date(2023 , 1 , 1 )
2083
2129
"""
2084
2130
try:
2085
- _dt = dt.date(self.year , self.month, self.day)
2131
+ _dt = dt.date(self._year , self.month, self.day)
2086
2132
except ValueError as err:
2087
2133
raise NotImplementedError(
2088
2134
"date not yet supported on Timestamps which "
@@ -2131,7 +2177,7 @@ class Timestamp(_Timestamp):
2131
2177
datetime.IsoCalendarDate(year = 2022 , week = 52 , weekday = 7 )
2132
2178
"""
2133
2179
try:
2134
- _dt = datetime(self.year , self.month, self.day,
2180
+ _dt = datetime(self._year , self.month, self.day,
2135
2181
self.hour, self.minute, self.second,
2136
2182
self.microsecond, self.tzinfo, fold=self.fold)
2137
2183
except ValueError as err:
@@ -2273,7 +2319,7 @@ class Timestamp(_Timestamp):
2273
2319
tm_hour = 10 , tm_min = 0 , tm_sec = 0 , tm_wday = 6 , tm_yday = 1 , tm_isdst = - 1 )
2274
2320
"""
2275
2321
try:
2276
- _dt = datetime(self.year , self.month, self.day,
2322
+ _dt = datetime(self._year , self.month, self.day,
2277
2323
self.hour, self.minute, self.second,
2278
2324
self.microsecond, self.tzinfo, fold=self.fold)
2279
2325
except ValueError as err:
@@ -2334,7 +2380,7 @@ class Timestamp(_Timestamp):
2334
2380
738521
2335
2381
"""
2336
2382
try:
2337
- _dt = datetime(self.year , self.month, self.day,
2383
+ _dt = datetime(self._year , self.month, self.day,
2338
2384
self.hour, self.minute, self.second,
2339
2385
self.microsecond, self.tzinfo, fold=self.fold)
2340
2386
except ValueError as err:
@@ -3223,7 +3269,7 @@ default 'raise'
3223
3269
3224
3270
# setup components
3225
3271
pandas_datetime_to_datetimestruct(value, self._creso, &dts)
3226
- dts.ps = self.nanosecond * 1000
3272
+ dts.ps = self._nanosecond * 1000
3227
3273
3228
3274
# replace
3229
3275
def validate(k, v):
@@ -3313,7 +3359,7 @@ default 'raise'
3313
3359
>>> ts.to_julian_date()
3314
3360
2458923.147824074
3315
3361
"""
3316
- year = self.year
3362
+ year = self._year
3317
3363
month = self.month
3318
3364
day = self.day
3319
3365
if month <= 2:
@@ -3330,7 +3376,7 @@ default 'raise'
3330
3376
self.minute / 60.0 +
3331
3377
self.second / 3600.0 +
3332
3378
self.microsecond / 3600.0 / 1e+6 +
3333
- self.nanosecond / 3600.0 / 1e+9
3379
+ self._nanosecond / 3600.0 / 1e+9
3334
3380
) / 24.0)
3335
3381
3336
3382
def isoweekday(self):
@@ -3381,7 +3427,7 @@ default 'raise'
3381
3427
"""
3382
3428
# same as super().weekday(), but that breaks because of how
3383
3429
# 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)
3385
3431
3386
3432
3387
3433
# Aliases
0 commit comments