Skip to content

API: provide compat for Timestamp now/today/utcnow class methods (GH5339) #5342

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
Oct 27, 2013
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/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~
Expand Down
18 changes: 15 additions & 3 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"))
Expand Down
16 changes: 16 additions & 0 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat, didn't know you could use decorators in Cython at all...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep u can use them to tell cython not to check array bounds too, for example.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, right the 'fake' cython decorators.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they are real. like u said, cython implements decorators. i think cython is a superset of python so all of python is implemented, with the exception of assignment in the body of a class.

def now(cls, tz=None):
""" compat now with datetime """
if isinstance(tz, basestring):
tz = pytz.timezone(tz)
return cls(datetime.now(tz))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpcloud so is this the right option here? Or should it just be "cls('now')"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's fine the way it is and that would depend on dateutil parsing i think. i'd prefer to just depend on stdlib here ... but not super strong about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is what datetime does

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep ... looks good to me


@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
Expand Down