Skip to content

modernize syntax in Timestamp #18327

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 2 commits into from
Nov 19, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 38 additions & 36 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -981,11 +981,10 @@ cdef class _Timestamp(datetime):
pass

tz = ", tz='{0}'".format(zone) if zone is not None else ""
freq = ", freq='{0}'".format(
self.freq.freqstr) if self.freq is not None else ""
freq = "" if self.freq is None else ", freq='{0}'".format(self.freqstr)

return "Timestamp('{stamp}'{tz}{freq})".format(
stamp=stamp, tz=tz, freq=freq)
return "Timestamp('{stamp}'{tz}{freq})".format(stamp=stamp,
tz=tz, freq=freq)

cdef bint _compare_outside_nanorange(_Timestamp self, datetime other,
int op) except -1:
Expand Down Expand Up @@ -1064,11 +1063,13 @@ cdef class _Timestamp(datetime):
return Timestamp((self.freq * other).apply(self), freq=self.freq)

elif PyDelta_Check(other) or hasattr(other, 'delta'):
# delta --> offsets.Tick
nanos = delta_to_nanoseconds(other)
result = Timestamp(self.value + nanos,
tz=self.tzinfo, freq=self.freq)
if getattr(other, 'normalize', False):
result = Timestamp(normalize_date(result))
# DateOffset
result = result.normalize()
return result

# index/series like
Expand Down Expand Up @@ -1158,42 +1159,43 @@ cdef class _Timestamp(datetime):
field, freqstr, month_kw)
return out[0]

property _repr_base:
def __get__(self):
return '%s %s' % (self._date_repr, self._time_repr)
@property
def _repr_base(self):
return '{date} {time}'.format(date=self._date_repr,
time=self._time_repr)

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

property _time_repr:
def __get__(self):
result = '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second)
@property
def _time_repr(self):
result = '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second)

if self.nanosecond != 0:
result += '.%.9d' % (self.nanosecond + 1000 * self.microsecond)
elif self.microsecond != 0:
result += '.%.6d' % self.microsecond
if self.nanosecond != 0:
result += '.%.9d' % (self.nanosecond + 1000 * self.microsecond)
elif self.microsecond != 0:
result += '.%.6d' % self.microsecond

return result
return result

property _short_repr:
def __get__(self):
# format a Timestamp with only _date_repr if possible
# otherwise _repr_base
if (self.hour == 0 and
self.minute == 0 and
self.second == 0 and
self.microsecond == 0 and
self.nanosecond == 0):
return self._date_repr
return self._repr_base

property asm8:
def __get__(self):
return np.datetime64(self.value, 'ns')
@property
def _short_repr(self):
# format a Timestamp with only _date_repr if possible
# otherwise _repr_base
if (self.hour == 0 and
self.minute == 0 and
self.second == 0 and
self.microsecond == 0 and
self.nanosecond == 0):
return self._date_repr
return self._repr_base

@property
def asm8(self):
return np.datetime64(self.value, 'ns')

def timestamp(self):
"""Return POSIX timestamp as float."""
Expand Down