Skip to content

Commit 5c23ef4

Browse files
jbrockmendelTomAugspurger
authored andcommitted
Add timestamp method+test; closes pandas-dev#17329 (pandas-dev#17906)
(cherry picked from commit 5dd2ea0)
1 parent ba36a14 commit 5c23ef4

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
@@ -961,8 +961,7 @@ class NaTType(_NaT):
961961
combine = _make_error_func('combine', None)
962962
utcnow = _make_error_func('utcnow', None)
963963

964-
if PY3:
965-
timestamp = _make_error_func('timestamp', datetime)
964+
timestamp = _make_error_func('timestamp', Timestamp)
966965

967966
# GH9513 NaT methods (except to_datetime64) to raise, return np.nan, or
968967
# return NaT create functions that raise, for binding to NaTType
@@ -1409,6 +1408,11 @@ cdef class _Timestamp(datetime):
14091408
def __get__(self):
14101409
return np.datetime64(self.value, 'ns')
14111410

1411+
def timestamp(self):
1412+
"""Return POSIX timestamp as float."""
1413+
# py27 compat, see GH#17329
1414+
return round(self.value / 1e9, 6)
1415+
14121416

14131417
cdef PyTypeObject* ts_type = <PyTypeObject*> Timestamp
14141418

@@ -3366,7 +3370,7 @@ cpdef int64_t tz_convert_single(int64_t val, object tz1, object tz2):
33663370
"""
33673371
Convert the val (in i8) from timezone1 to timezone2
33683372
3369-
This is a single timezone versoin of tz_convert
3373+
This is a single timezone version of tz_convert
33703374
33713375
Parameters
33723376
----------

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,
@@ -1079,6 +1079,23 @@ def test_is_leap_year(self):
10791079
dt = Timestamp('2100-01-01 00:00:00', tz=tz)
10801080
assert not dt.is_leap_year
10811081

1082+
def test_timestamp(self):
1083+
# GH#17329
1084+
# tz-naive --> treat it as if it were UTC for purposes of timestamp()
1085+
ts = Timestamp.now()
1086+
uts = ts.replace(tzinfo=utc)
1087+
assert ts.timestamp() == uts.timestamp()
1088+
1089+
tsc = Timestamp('2014-10-11 11:00:01.12345678', tz='US/Central')
1090+
utsc = tsc.tz_convert('UTC')
1091+
# utsc is a different representation of the same time
1092+
assert tsc.timestamp() == utsc.timestamp()
1093+
1094+
if PY3:
1095+
# should agree with datetime.timestamp method
1096+
dt = ts.to_pydatetime()
1097+
assert dt.timestamp() == ts.timestamp()
1098+
10821099

10831100
class TestTimestampNsOperations(object):
10841101

0 commit comments

Comments
 (0)