Skip to content

Add timestamp method+test; closes #17329 #17906

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 17 commits into from
Oct 27, 2017
Merged
Show file tree
Hide file tree
Changes from 13 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
1 change: 1 addition & 0 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,7 @@ Methods
Timestamp.strftime
Timestamp.strptime
Timestamp.time
Timestamp.timestamp
Timestamp.timetuple
Timestamp.timetz
Timestamp.to_datetime64
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ Other Enhancements
- Improved the import time of pandas by about 2.25x. (:issue:`16764`)
- :func:`read_json` and :func:`to_json` now accept a ``compression`` argument which allows them to transparently handle compressed files. (:issue:`17798`)
- :func:`Series.reindex`, :func:`DataFrame.reindex`, :func:`Index.get_indexer` now support list-like argument for ``tolerance``. (:issue:`17367`)
- :meth:`Timestamp.timestamp` is now available in Python 2.7. (:issue:`17329`)
Copy link
Contributor

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is now.


.. _whatsnew_0210.api_breaking:

Expand Down
10 changes: 7 additions & 3 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a doc-string

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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)

Copy link
Member Author

Choose a reason for hiding this comment

The 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

Expand Down Expand Up @@ -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
----------
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/scalar/test_nat.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ def test_round_nat(klass):
assert round_method(freq) is ts


def test_timestamp():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls just add this where the others are (IOW in the list), don't special case this.

# GH#17329
pytest.raises(ValueError, NaT.timestamp)


def test_NaT_methods():
# GH 9513
raise_methods = ['astimezone', 'combine', 'ctime', 'dst',
Expand Down
19 changes: 18 additions & 1 deletion pandas/tests/scalar/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pandas._libs import tslib, period
from pandas._libs.tslibs.timezones import get_timezone

from pandas.compat import lrange, long
from pandas.compat import lrange, long, PY3
from pandas.util.testing import assert_series_equal
from pandas.compat.numpy import np_datetime64_compat
from pandas import (Timestamp, date_range, Period, Timedelta, compat,
Expand Down Expand Up @@ -1079,6 +1079,23 @@ 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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test versus a datetime.timestamp() value here (as well)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. ts.to_pydatetime().timestamp()

uts = ts.replace(tzinfo=utc)
assert ts.timestamp() == uts.timestamp()

tsc = Timestamp('2014-10-11 11:00:01.12345678', tz='US/Central')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can u maker sure test for mat is ok?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the question.

Copy link
Contributor

Choose a reason for hiding this comment

The 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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this not available in py2? on datetime?

Copy link
Member Author

Choose a reason for hiding this comment

The 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

# should agree with datetime.timestamp method
dt = ts.to_pydatetime()
assert dt.timestamp() == round(ts.timestamp(), 6)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need to round?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because dt.timestamp() is the datetime.timestamp method which only has microsecond-precision.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly, you should not need to do this



class TestTimestampNsOperations(object):

Expand Down