Skip to content

Commit 421cd26

Browse files
committed
ENH: Timestamp: Support addl datetime classmethods
* `utcfromtimestamp`,`timestamp`, and `combine` now supported * Closes pandas-dev#5351.
1 parent ac9b2ab commit 421cd26

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

doc/source/whatsnew/v0.15.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Enhancements
4343

4444
- Added ability to export Categorical data to Stata (:issue:`8633`).
4545
- 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 <io.hdf5-categorical>` for an example and caveats w.r.t. prior versions of pandas.
46+
- Added support for ``utcfromtimestamp()``, ``fromtimestamp()``, and ``combine()`` on `Timestamp` class (:issue:`5351`).
4647

4748
.. _whatsnew_0152.performance:
4849

pandas/tseries/tests/test_timeseries.py

+21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# pylint: disable-msg=E1101,W0612
2+
import calendar
23
from datetime import datetime, time, timedelta
34
import sys
45
import operator
@@ -3291,6 +3292,16 @@ def compare(x, y):
32913292
compare(Timestamp.now('UTC'), datetime.now(timezone('UTC')))
32923293
compare(Timestamp.utcnow(), datetime.utcnow())
32933294
compare(Timestamp.today(), datetime.today())
3295+
current_time = calendar.timegm(datetime.now().utctimetuple())
3296+
compare(Timestamp.utcfromtimestamp(current_time),
3297+
datetime.utcfromtimestamp(current_time))
3298+
compare(Timestamp.fromtimestamp(current_time),
3299+
datetime.fromtimestamp(current_time))
3300+
3301+
date_component = datetime.utcnow()
3302+
time_component = (date_component + timedelta(minutes=10)).time()
3303+
compare(Timestamp.combine(date_component, time_component),
3304+
datetime.combine(date_component, time_component))
32943305

32953306
def test_class_ops_dateutil(self):
32963307
tm._skip_if_no_dateutil()
@@ -3303,6 +3314,16 @@ def compare(x,y):
33033314
compare(Timestamp.now('UTC'), datetime.now(tzutc()))
33043315
compare(Timestamp.utcnow(),datetime.utcnow())
33053316
compare(Timestamp.today(),datetime.today())
3317+
current_time = calendar.timegm(datetime.now().utctimetuple())
3318+
compare(Timestamp.utcfromtimestamp(current_time),
3319+
datetime.utcfromtimestamp(current_time))
3320+
compare(Timestamp.fromtimestamp(current_time),
3321+
datetime.fromtimestamp(current_time))
3322+
3323+
date_component = datetime.utcnow()
3324+
time_component = (date_component + timedelta(minutes=10)).time()
3325+
compare(Timestamp.combine(date_component, time_component),
3326+
datetime.combine(date_component, time_component))
33063327

33073328
def test_basics_nanos(self):
33083329
val = np.int64(946684800000000000).view('M8[ns]')

pandas/tslib.pyx

+11
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,17 @@ class Timestamp(_Timestamp):
228228
def utcnow(cls):
229229
return cls.now('UTC')
230230

231+
@classmethod
232+
def utcfromtimestamp(cls, ts):
233+
return cls(datetime.utcfromtimestamp(ts))
234+
235+
@classmethod
236+
def fromtimestamp(cls, ts):
237+
return cls(datetime.fromtimestamp(ts))
238+
239+
@classmethod
240+
def combine(cls, date, time):
241+
return cls(datetime.combine(date, time))
231242

232243
def __new__(cls, object ts_input, object offset=None, tz=None, unit=None):
233244
cdef _TSObject ts

0 commit comments

Comments
 (0)