Skip to content

Commit 5dd2ea0

Browse files
jbrockmendeljreback
authored andcommitted
Add timestamp method+test; closes #17329 (#17906)
1 parent dff5109 commit 5dd2ea0

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

doc/source/api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,7 @@ Methods
17941794
Timestamp.strftime
17951795
Timestamp.strptime
17961796
Timestamp.time
1797+
Timestamp.timestamp
17971798
Timestamp.timetuple
17981799
Timestamp.timetz
17991800
Timestamp.to_datetime64

doc/source/whatsnew/v0.21.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ New features
2121
Other Enhancements
2222
^^^^^^^^^^^^^^^^^^
2323

24-
-
24+
- :meth:`Timestamp.timestamp` is now available in Python 2.7. (:issue:`17329`)
2525
-
2626
-
2727

pandas/_libs/tslib.pyx

+7-3
Original file line numberDiff line numberDiff line change
@@ -977,8 +977,7 @@ class NaTType(_NaT):
977977
combine = _make_error_func('combine', None)
978978
utcnow = _make_error_func('utcnow', None)
979979

980-
if PY3:
981-
timestamp = _make_error_func('timestamp', datetime)
980+
timestamp = _make_error_func('timestamp', Timestamp)
982981

983982
# GH9513 NaT methods (except to_datetime64) to raise, return np.nan, or
984983
# return NaT create functions that raise, for binding to NaTType
@@ -1425,6 +1424,11 @@ cdef class _Timestamp(datetime):
14251424
def __get__(self):
14261425
return np.datetime64(self.value, 'ns')
14271426

1427+
def timestamp(self):
1428+
"""Return POSIX timestamp as float."""
1429+
# py27 compat, see GH#17329
1430+
return round(self.value / 1e9, 6)
1431+
14281432

14291433
cdef PyTypeObject* ts_type = <PyTypeObject*> Timestamp
14301434

@@ -3382,7 +3386,7 @@ cpdef int64_t tz_convert_single(int64_t val, object tz1, object tz2):
33823386
"""
33833387
Convert the val (in i8) from timezone1 to timezone2
33843388
3385-
This is a single timezone versoin of tz_convert
3389+
This is a single timezone version of tz_convert
33863390
33873391
Parameters
33883392
----------

pandas/tests/scalar/test_nat.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,13 @@ def test_round_nat(klass):
125125

126126
def test_NaT_methods():
127127
# GH 9513
128+
# GH 17329 for `timestamp`
128129
raise_methods = ['astimezone', 'combine', 'ctime', 'dst',
129130
'fromordinal', 'fromtimestamp', 'isocalendar',
130131
'strftime', 'strptime', 'time', 'timestamp',
131132
'timetuple', 'timetz', 'toordinal', 'tzname',
132133
'utcfromtimestamp', 'utcnow', 'utcoffset',
133-
'utctimetuple']
134+
'utctimetuple', 'timestamp']
134135
nat_methods = ['date', 'now', 'replace', 'to_datetime', 'today',
135136
'tz_convert', 'tz_localize']
136137
nan_methods = ['weekday', 'isoweekday']

pandas/tests/scalar/test_timestamp.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from pandas._libs import tslib, period
2020
from pandas._libs.tslibs.timezones import get_timezone
2121

22-
from pandas.compat import lrange, long
22+
from pandas.compat import lrange, long, PY3
2323
from pandas.util.testing import assert_series_equal
2424
from pandas.compat.numpy import np_datetime64_compat
2525
from pandas import (Timestamp, date_range, Period, Timedelta, compat,
@@ -1097,6 +1097,23 @@ def test_is_leap_year(self):
10971097
dt = Timestamp('2100-01-01 00:00:00', tz=tz)
10981098
assert not dt.is_leap_year
10991099

1100+
def test_timestamp(self):
1101+
# GH#17329
1102+
# tz-naive --> treat it as if it were UTC for purposes of timestamp()
1103+
ts = Timestamp.now()
1104+
uts = ts.replace(tzinfo=utc)
1105+
assert ts.timestamp() == uts.timestamp()
1106+
1107+
tsc = Timestamp('2014-10-11 11:00:01.12345678', tz='US/Central')
1108+
utsc = tsc.tz_convert('UTC')
1109+
# utsc is a different representation of the same time
1110+
assert tsc.timestamp() == utsc.timestamp()
1111+
1112+
if PY3:
1113+
# should agree with datetime.timestamp method
1114+
dt = ts.to_pydatetime()
1115+
assert dt.timestamp() == ts.timestamp()
1116+
11001117

11011118
class TestTimestampNsOperations(object):
11021119

0 commit comments

Comments
 (0)