-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,7 @@ from tslibs.nattype import NaT, nat_strings, iNaT | |
from tslibs.nattype cimport _checknull_with_nat, NPY_NAT | ||
|
||
|
||
# TODO: Can this become a classmethod? | ||
cdef inline object create_timestamp_from_ts( | ||
int64_t value, pandas_datetimestruct dts, | ||
object tz, object freq): | ||
|
@@ -981,11 +982,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: | ||
|
@@ -1064,11 +1064,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 | ||
|
@@ -1158,42 +1160,42 @@ 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 '%s %s' % (self._date_repr, self._time_repr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change what you can to use format syntax |
||
|
||
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.""" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jreback @jorisvandenbossche
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, see how its used, it is for compat on creating datetime, date, Timestamp