Skip to content

Commit 82bfbe8

Browse files
committed
API: provide compat for Timestamp now/today/utcnow class methods (GH5339)
1 parent be7c4c0 commit 82bfbe8

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ API Changes
321321
(:issue:`4501`)
322322
- Support non-unique axes in a Panel via indexing operations (:issue:`4960`)
323323
- ``.truncate`` will raise a ``ValueError`` if invalid before and afters dates are given (:issue:`5242`)
324+
- ``Timestamp`` now supports ``now/today/utcnow`` class methods (:issue:`5339`)
324325

325326
Internal Refactoring
326327
~~~~~~~~~~~~~~~~~~~~

pandas/tseries/tests/test_timeseries.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -2516,6 +2516,18 @@ def test_string_index_series_name_converted(self):
25162516

25172517
class TestTimestamp(unittest.TestCase):
25182518

2519+
def test_class_ops(self):
2520+
_skip_if_no_pytz()
2521+
import pytz
2522+
2523+
def compare(x,y):
2524+
self.assert_(int(Timestamp(x).value/1e9) == int(Timestamp(y).value/1e9))
2525+
2526+
compare(Timestamp.now(),datetime.now())
2527+
compare(Timestamp.now('UTC'),datetime.now(pytz.timezone('UTC')))
2528+
compare(Timestamp.utcnow(),datetime.utcnow())
2529+
compare(Timestamp.today(),datetime.today())
2530+
25192531
def test_basics_nanos(self):
25202532
val = np.int64(946684800000000000).view('M8[ns]')
25212533
stamp = Timestamp(val.view('i8') + 500)
@@ -3031,10 +3043,10 @@ def test_frame_apply_dont_convert_datetime64(self):
30313043
df = df.applymap(lambda x: x + BDay())
30323044

30333045
self.assertTrue(df.x1.dtype == 'M8[ns]')
3034-
3046+
30353047
def test_date_range_fy5252(self):
3036-
dr = date_range(start="2013-01-01",
3037-
periods=2,
3048+
dr = date_range(start="2013-01-01",
3049+
periods=2,
30383050
freq=offsets.FY5253(startingMonth=1,
30393051
weekday=3,
30403052
variation="nearest"))

pandas/tslib.pyx

+16
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,22 @@ class Timestamp(_Timestamp):
140140
note: by definition there cannot be any tz info on the ordinal itself """
141141
return cls(datetime.fromordinal(ordinal),offset=offset,tz=tz)
142142

143+
@classmethod
144+
def now(cls, tz=None):
145+
""" compat now with datetime """
146+
if isinstance(tz, basestring):
147+
tz = pytz.timezone(tz)
148+
return cls(datetime.now(tz))
149+
150+
@classmethod
151+
def today(cls):
152+
""" compat today with datetime """
153+
return cls(datetime.today())
154+
155+
@classmethod
156+
def utcnow(cls):
157+
return cls.now('UTC')
158+
143159
def __new__(cls, object ts_input, object offset=None, tz=None, unit=None):
144160
cdef _TSObject ts
145161
cdef _Timestamp ts_base

0 commit comments

Comments
 (0)