Skip to content

ENH: Timestamp: Support addl datetime classmethods #8837

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 1 commit into from
Nov 17, 2014
Merged
Show file tree
Hide file tree
Changes from all 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/whatsnew/v0.15.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <io.hdf5-categorical>` 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:

Expand Down
21 changes: 21 additions & 0 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# pylint: disable-msg=E1101,W0612
import calendar
from datetime import datetime, time, timedelta
import sys
import operator
Expand Down Expand Up @@ -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()
Expand All @@ -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]')
Expand Down
11 changes: 11 additions & 0 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down