From 82bfbe89387470e96f238cde232966a22fd0a63b Mon Sep 17 00:00:00 2001 From: jreback Date: Sat, 26 Oct 2013 15:36:48 -0400 Subject: [PATCH] API: provide compat for Timestamp now/today/utcnow class methods (GH5339) --- doc/source/release.rst | 1 + pandas/tseries/tests/test_timeseries.py | 18 +++++++++++++++--- pandas/tslib.pyx | 16 ++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index b74b23029a2ac..6c8ae847254f2 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -321,6 +321,7 @@ API Changes (:issue:`4501`) - Support non-unique axes in a Panel via indexing operations (:issue:`4960`) - ``.truncate`` will raise a ``ValueError`` if invalid before and afters dates are given (:issue:`5242`) + - ``Timestamp`` now supports ``now/today/utcnow`` class methods (:issue:`5339`) Internal Refactoring ~~~~~~~~~~~~~~~~~~~~ diff --git a/pandas/tseries/tests/test_timeseries.py b/pandas/tseries/tests/test_timeseries.py index dee0587aaaa02..b8144c2b5eab9 100644 --- a/pandas/tseries/tests/test_timeseries.py +++ b/pandas/tseries/tests/test_timeseries.py @@ -2516,6 +2516,18 @@ def test_string_index_series_name_converted(self): class TestTimestamp(unittest.TestCase): + def test_class_ops(self): + _skip_if_no_pytz() + import pytz + + def compare(x,y): + self.assert_(int(Timestamp(x).value/1e9) == int(Timestamp(y).value/1e9)) + + compare(Timestamp.now(),datetime.now()) + compare(Timestamp.now('UTC'),datetime.now(pytz.timezone('UTC'))) + compare(Timestamp.utcnow(),datetime.utcnow()) + compare(Timestamp.today(),datetime.today()) + def test_basics_nanos(self): val = np.int64(946684800000000000).view('M8[ns]') stamp = Timestamp(val.view('i8') + 500) @@ -3031,10 +3043,10 @@ def test_frame_apply_dont_convert_datetime64(self): df = df.applymap(lambda x: x + BDay()) self.assertTrue(df.x1.dtype == 'M8[ns]') - + def test_date_range_fy5252(self): - dr = date_range(start="2013-01-01", - periods=2, + dr = date_range(start="2013-01-01", + periods=2, freq=offsets.FY5253(startingMonth=1, weekday=3, variation="nearest")) diff --git a/pandas/tslib.pyx b/pandas/tslib.pyx index c487202c4c0f9..11b86db8b8d92 100644 --- a/pandas/tslib.pyx +++ b/pandas/tslib.pyx @@ -140,6 +140,22 @@ class Timestamp(_Timestamp): note: by definition there cannot be any tz info on the ordinal itself """ return cls(datetime.fromordinal(ordinal),offset=offset,tz=tz) + @classmethod + def now(cls, tz=None): + """ compat now with datetime """ + if isinstance(tz, basestring): + tz = pytz.timezone(tz) + return cls(datetime.now(tz)) + + @classmethod + def today(cls): + """ compat today with datetime """ + return cls(datetime.today()) + + @classmethod + def utcnow(cls): + return cls.now('UTC') + def __new__(cls, object ts_input, object offset=None, tz=None, unit=None): cdef _TSObject ts cdef _Timestamp ts_base