-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
Add timestamp method+test; closes #17329 #17906
Changes from 7 commits
f9e2ad5
aa7b06d
4155b03
c73c2fe
66d4a2b
54ebd13
a88419d
49accab
698e688
5eed82b
715b740
b7747ec
237be98
a775671
8f6d8d5
580a1c6
7992462
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -956,8 +956,7 @@ class NaTType(_NaT): | |
combine = _make_error_func('combine', None) | ||
utcnow = _make_error_func('utcnow', None) | ||
|
||
if PY3: | ||
timestamp = _make_error_func('timestamp', datetime) | ||
timestamp = _make_error_func('timestamp', Timestamp) | ||
|
||
# GH9513 NaT methods (except to_datetime64) to raise, return np.nan, or | ||
# return NaT create functions that raise, for binding to NaTType | ||
|
@@ -1405,6 +1404,11 @@ cdef class _Timestamp(datetime): | |
def __get__(self): | ||
return np.datetime64(self.value, 'ns') | ||
|
||
def timestamp(self): | ||
"""Return POSIX timestamp as float.""" | ||
# py27 compat, see GH#17329 | ||
return self.value / 1e9 | ||
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. can you add a doc-string 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. I think this is wrong if we have a nanosecond component. should issue a warning if we have one (I think its a print statement, but that's consistent with what we have now). add a test for this. 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. Why is it wrong with a nanosecond component? Is it a precision issue? 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. yes its not compatible with datetime.timestamp() (since you cannot construct one in datetime, we should not allow a time value which is invalid) 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. I'll make this change, will add follow-up to the tslibs todo list, since it isn't obvious to me that this is the correct decision. |
||
|
||
|
||
cdef PyTypeObject* ts_type = <PyTypeObject*> Timestamp | ||
|
||
|
@@ -3635,7 +3639,7 @@ cpdef int64_t tz_convert_single(int64_t val, object tz1, object tz2): | |
""" | ||
Convert the val (in i8) from timezone1 to timezone2 | ||
|
||
This is a single timezone versoin of tz_convert | ||
This is a single timezone version of tz_convert | ||
|
||
Parameters | ||
---------- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1079,6 +1079,22 @@ def test_is_leap_year(self): | |
dt = Timestamp('2100-01-01 00:00:00', tz=tz) | ||
assert not dt.is_leap_year | ||
|
||
def test_timestamp(self): | ||
# GH#17329 | ||
# tz-naive --> treat it as if it were UTC for purposes of timestamp() | ||
ts = Timestamp.now() | ||
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. test versus a datetime.timestamp() value here (as well) 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. e.g. |
||
uts = ts.replace(tzinfo=utc) | ||
assert ts.timestamp() == uts.timestamp() | ||
|
||
tsc = Timestamp('2014-10-11 11:00:01.12345678', tz='US/Central') | ||
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. can u maker sure test for mat is ok? 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. I don't understand the question. 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. for NaT |
||
utsc = tsc.tz_convert('UTC') | ||
# utsc is a different representation of the same time | ||
assert tsc.timestamp() == utsc.timestamp() | ||
|
||
if PY3: | ||
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. is this not available in py2? on datetime? 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. Correct. The method was added to the stdlib datetime.datetime in py3.3 |
||
dt = ts.to_pydatetime() | ||
assert dt.timestamp() == ts.timestamp() - ts.nanoseconds | ||
|
||
|
||
class TestTimestampNsOperations(object): | ||
|
||
|
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.
is this in api.rst?
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.
It is now.