Skip to content

Commit b315e7d

Browse files
jbrockmendeljreback
authored andcommitted
modernize syntax in Timestamp (#18327)
1 parent 410ad37 commit b315e7d

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

pandas/_libs/tslib.pyx

+38-36
Original file line numberDiff line numberDiff line change
@@ -976,11 +976,10 @@ cdef class _Timestamp(datetime):
976976
pass
977977

978978
tz = ", tz='{0}'".format(zone) if zone is not None else ""
979-
freq = ", freq='{0}'".format(
980-
self.freq.freqstr) if self.freq is not None else ""
979+
freq = "" if self.freq is None else ", freq='{0}'".format(self.freqstr)
981980

982-
return "Timestamp('{stamp}'{tz}{freq})".format(
983-
stamp=stamp, tz=tz, freq=freq)
981+
return "Timestamp('{stamp}'{tz}{freq})".format(stamp=stamp,
982+
tz=tz, freq=freq)
984983

985984
cdef bint _compare_outside_nanorange(_Timestamp self, datetime other,
986985
int op) except -1:
@@ -1059,11 +1058,13 @@ cdef class _Timestamp(datetime):
10591058
return Timestamp((self.freq * other).apply(self), freq=self.freq)
10601059

10611060
elif PyDelta_Check(other) or hasattr(other, 'delta'):
1061+
# delta --> offsets.Tick
10621062
nanos = delta_to_nanoseconds(other)
10631063
result = Timestamp(self.value + nanos,
10641064
tz=self.tzinfo, freq=self.freq)
10651065
if getattr(other, 'normalize', False):
1066-
result = Timestamp(normalize_date(result))
1066+
# DateOffset
1067+
result = result.normalize()
10671068
return result
10681069

10691070
# index/series like
@@ -1153,42 +1154,43 @@ cdef class _Timestamp(datetime):
11531154
field, freqstr, month_kw)
11541155
return out[0]
11551156

1156-
property _repr_base:
1157-
def __get__(self):
1158-
return '%s %s' % (self._date_repr, self._time_repr)
1157+
@property
1158+
def _repr_base(self):
1159+
return '{date} {time}'.format(date=self._date_repr,
1160+
time=self._time_repr)
11591161

1160-
property _date_repr:
1161-
def __get__(self):
1162-
# Ideal here would be self.strftime("%Y-%m-%d"), but
1163-
# the datetime strftime() methods require year >= 1900
1164-
return '%d-%.2d-%.2d' % (self.year, self.month, self.day)
1162+
@property
1163+
def _date_repr(self):
1164+
# Ideal here would be self.strftime("%Y-%m-%d"), but
1165+
# the datetime strftime() methods require year >= 1900
1166+
return '%d-%.2d-%.2d' % (self.year, self.month, self.day)
11651167

1166-
property _time_repr:
1167-
def __get__(self):
1168-
result = '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second)
1168+
@property
1169+
def _time_repr(self):
1170+
result = '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second)
11691171

1170-
if self.nanosecond != 0:
1171-
result += '.%.9d' % (self.nanosecond + 1000 * self.microsecond)
1172-
elif self.microsecond != 0:
1173-
result += '.%.6d' % self.microsecond
1172+
if self.nanosecond != 0:
1173+
result += '.%.9d' % (self.nanosecond + 1000 * self.microsecond)
1174+
elif self.microsecond != 0:
1175+
result += '.%.6d' % self.microsecond
11741176

1175-
return result
1177+
return result
11761178

1177-
property _short_repr:
1178-
def __get__(self):
1179-
# format a Timestamp with only _date_repr if possible
1180-
# otherwise _repr_base
1181-
if (self.hour == 0 and
1182-
self.minute == 0 and
1183-
self.second == 0 and
1184-
self.microsecond == 0 and
1185-
self.nanosecond == 0):
1186-
return self._date_repr
1187-
return self._repr_base
1188-
1189-
property asm8:
1190-
def __get__(self):
1191-
return np.datetime64(self.value, 'ns')
1179+
@property
1180+
def _short_repr(self):
1181+
# format a Timestamp with only _date_repr if possible
1182+
# otherwise _repr_base
1183+
if (self.hour == 0 and
1184+
self.minute == 0 and
1185+
self.second == 0 and
1186+
self.microsecond == 0 and
1187+
self.nanosecond == 0):
1188+
return self._date_repr
1189+
return self._repr_base
1190+
1191+
@property
1192+
def asm8(self):
1193+
return np.datetime64(self.value, 'ns')
11921194

11931195
def timestamp(self):
11941196
"""Return POSIX timestamp as float."""

0 commit comments

Comments
 (0)