diff --git a/doc/source/whatsnew/v0.15.2.txt b/doc/source/whatsnew/v0.15.2.txt index a4d7c3969f056..4274eb5c9ec58 100644 --- a/doc/source/whatsnew/v0.15.2.txt +++ b/doc/source/whatsnew/v0.15.2.txt @@ -43,6 +43,7 @@ Enhancements - Added ability to export Categorical data to Stata (:issue:`8633`). - Added ability to export Categorical data to to/from HDF5 (:issue:`7621`). Queries work the same as if it was an object array. However, the ``category`` dtyped data is stored in a more efficient manner. See :ref:`here ` for an example and caveats w.r.t. prior versions of pandas. +- Added support for ``utcfromtimestamp()``, ``fromtimestamp()``, and ``combine()`` on `Timestamp` class (:issue:`5351`). .. _whatsnew_0152.performance: diff --git a/pandas/tseries/tests/test_timeseries.py b/pandas/tseries/tests/test_timeseries.py index bf1f0d31e8d3e..e6b4bf23e806f 100644 --- a/pandas/tseries/tests/test_timeseries.py +++ b/pandas/tseries/tests/test_timeseries.py @@ -1,4 +1,5 @@ # pylint: disable-msg=E1101,W0612 +import calendar from datetime import datetime, time, timedelta import sys import operator @@ -3291,6 +3292,16 @@ def compare(x, y): compare(Timestamp.now('UTC'), datetime.now(timezone('UTC'))) compare(Timestamp.utcnow(), datetime.utcnow()) compare(Timestamp.today(), datetime.today()) + current_time = calendar.timegm(datetime.now().utctimetuple()) + compare(Timestamp.utcfromtimestamp(current_time), + datetime.utcfromtimestamp(current_time)) + compare(Timestamp.fromtimestamp(current_time), + datetime.fromtimestamp(current_time)) + + date_component = datetime.utcnow() + time_component = (date_component + timedelta(minutes=10)).time() + compare(Timestamp.combine(date_component, time_component), + datetime.combine(date_component, time_component)) def test_class_ops_dateutil(self): tm._skip_if_no_dateutil() @@ -3303,6 +3314,16 @@ def compare(x,y): compare(Timestamp.now('UTC'), datetime.now(tzutc())) compare(Timestamp.utcnow(),datetime.utcnow()) compare(Timestamp.today(),datetime.today()) + current_time = calendar.timegm(datetime.now().utctimetuple()) + compare(Timestamp.utcfromtimestamp(current_time), + datetime.utcfromtimestamp(current_time)) + compare(Timestamp.fromtimestamp(current_time), + datetime.fromtimestamp(current_time)) + + date_component = datetime.utcnow() + time_component = (date_component + timedelta(minutes=10)).time() + compare(Timestamp.combine(date_component, time_component), + datetime.combine(date_component, time_component)) def test_basics_nanos(self): val = np.int64(946684800000000000).view('M8[ns]') diff --git a/pandas/tslib.pyx b/pandas/tslib.pyx index 3834e4e6b24d9..aed6dea264be6 100644 --- a/pandas/tslib.pyx +++ b/pandas/tslib.pyx @@ -228,6 +228,17 @@ class Timestamp(_Timestamp): def utcnow(cls): return cls.now('UTC') + @classmethod + def utcfromtimestamp(cls, ts): + return cls(datetime.utcfromtimestamp(ts)) + + @classmethod + def fromtimestamp(cls, ts): + return cls(datetime.fromtimestamp(ts)) + + @classmethod + def combine(cls, date, time): + return cls(datetime.combine(date, time)) def __new__(cls, object ts_input, object offset=None, tz=None, unit=None): cdef _TSObject ts