Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1d0fce5

Browse files
committed
Make Timestamp('now') equivalent to Timestamp.now() and Timestamp('today') equivalent to Timestamp.today() and pass tz to today().
1 parent 4ab5409 commit 1d0fce5

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

doc/source/whatsnew/v0.15.2.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ API changes
6262

6363
- Allow equality comparisons of Series with a categorical dtype and object dtype; previously these would raise ``TypeError`` (:issue:`8938`)
6464

65+
- Timestamp('now') is now equivalent to Timestamp.now() in that it returns the local time rather than UTC. Also, Timestamp('today') is now
66+
equivalent to Timestamp.today() and both have tz as a possible argument. (:issue:`9000`)
67+
6568
.. _whatsnew_0152.enhancements:
6669

6770
Enhancements

pandas/tseries/tests/test_tslib.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,36 @@ def test_barely_oob_dts(self):
301301
def test_utc_z_designator(self):
302302
self.assertEqual(get_timezone(Timestamp('2014-11-02 01:00Z').tzinfo), 'UTC')
303303

304+
def test_now(self):
305+
# #9000
306+
ts_from_string = Timestamp('now')
307+
ts_from_method = Timestamp.now()
308+
ts_datetime = datetime.datetime.now()
309+
310+
ts_from_string_tz = Timestamp('now', tz='US/Eastern')
311+
ts_from_method_tz = Timestamp.now(tz='US/Eastern')
312+
313+
# Check that the delta between the times is less than 1s (arbitrarily small)
314+
delta = Timedelta(seconds=1)
315+
self.assertTrue((ts_from_method - ts_from_string) < delta)
316+
self.assertTrue((ts_from_method_tz - ts_from_string_tz) < delta)
317+
self.assertTrue((ts_from_string_tz.tz_localize(None) - ts_from_string) < delta)
318+
319+
def test_today(self):
320+
321+
ts_from_string = Timestamp('today')
322+
ts_from_method = Timestamp.today()
323+
ts_datetime = datetime.datetime.today()
324+
325+
ts_from_string_tz = Timestamp('today', tz='US/Eastern')
326+
ts_from_method_tz = Timestamp.today(tz='US/Eastern')
327+
328+
# Check that the delta between the times is less than 1s (arbitrarily small)
329+
delta = Timedelta(seconds=1)
330+
self.assertTrue((ts_from_method - ts_from_string) < delta)
331+
self.assertTrue((ts_datetime - ts_from_method) < delta)
332+
self.assertTrue((ts_datetime - ts_from_method) < delta)
333+
self.assertTrue((ts_from_string_tz.tz_localize(None) - ts_from_string) < delta)
304334

305335
class TestDatetimeParsingWrappers(tm.TestCase):
306336
def test_does_not_convert_mixed_integer(self):

pandas/tslib.pyx

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ def ints_to_pytimedelta(ndarray[int64_t] arr, box=False):
173173
result[i] = NaT
174174
else:
175175
if box:
176-
result[i] = Timedelta(value)
176+
result[i] = Timedelta(value)
177177
else:
178-
result[i] = timedelta(microseconds=int(value)/1000)
178+
result[i] = timedelta(microseconds=int(value)/1000)
179179

180180
return result
181181

@@ -216,15 +216,32 @@ class Timestamp(_Timestamp):
216216

217217
@classmethod
218218
def now(cls, tz=None):
219-
""" compat now with datetime """
219+
"""
220+
Return the current time in the local timezone. Equivalent
221+
to datetime.now([tz])
222+
223+
Parameters
224+
----------
225+
tz : string / timezone object, default None
226+
Timezone to localize to
227+
"""
220228
if isinstance(tz, basestring):
221229
tz = maybe_get_tz(tz)
222230
return cls(datetime.now(tz))
223231

224232
@classmethod
225-
def today(cls):
226-
""" compat today with datetime """
227-
return cls(datetime.today())
233+
def today(cls, tz=None):
234+
"""
235+
Return the current time in the local timezone. This differs
236+
from datetime.today() in that it can be localized to a
237+
passed timezone.
238+
239+
Parameters
240+
----------
241+
tz : string / timezone object, default None
242+
Timezone to localize to
243+
"""
244+
return cls.now(tz)
228245

229246
@classmethod
230247
def utcnow(cls):
@@ -1021,6 +1038,14 @@ cdef convert_to_tsobject(object ts, object tz, object unit):
10211038
if util.is_string_object(ts):
10221039
if ts in _nat_strings:
10231040
ts = NaT
1041+
elif ts == 'now':
1042+
# Issue 9000, we short-circuit rather than going
1043+
# into np_datetime_strings which returns utc
1044+
ts = Timestamp.now(tz)
1045+
elif ts == 'today':
1046+
# Issue 9000, we short-circuit rather than going
1047+
# into np_datetime_strings which returns a normalized datetime
1048+
ts = Timestamp.today(tz)
10241049
else:
10251050
try:
10261051
_string_to_dts(ts, &obj.dts, &out_local, &out_tzoffset)

0 commit comments

Comments
 (0)