Skip to content

Commit 5c34c04

Browse files
Support positional and keyword arguments for Timestamp
1 parent 0d6884b commit 5c34c04

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

pandas/tseries/tests/test_tslib.py

-6
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,6 @@ def test_constructor_positional(self):
192192
Timestamp(2000, 1, 0)
193193
with tm.assertRaises(ValueError):
194194
Timestamp(2000, 1, 32)
195-
with tm.assertRaises(TypeError):
196-
Timestamp(2000, 1, 1, None)
197-
with tm.assertRaises(TypeError):
198-
Timestamp(2000, 1, 1, None, None)
199-
with tm.assertRaises(TypeError):
200-
Timestamp(2000, 1, 1, None, None, None)
201195

202196
ts = Timestamp(2000, 1, 2)
203197

pandas/tslib.pyx

+27-21
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ cdef inline bint _is_fixed_offset(object tz):
216216

217217

218218
_zero_time = datetime_time(0, 0)
219-
_no_unit = object()
220219

221220
# Python front end to C extension type _Timestamp
222221
# This serves as the box for datetime64
@@ -290,34 +289,41 @@ class Timestamp(_Timestamp):
290289
return cls(datetime.combine(date, time))
291290

292291
def __new__(cls,
293-
object ts_input, object offset=None, tz=None, unit=_no_unit,
294-
minute=0, second=0, microsecond=0, tzinfo=None):
292+
object ts_input=None, object offset=None, tz=None, unit=None,
293+
year=None, month=None, day=None,
294+
hour=None, minute=None, second=None, microsecond=None,
295+
tzinfo=None):
295296
# The parameter list folds together legacy parameter names (the first
296-
# four) and positional parameter names from pydatetime (starting with
297-
# `minute`).
297+
# four) and positional and keyword parameter names from pydatetime.
298298
#
299-
# When using the pydatetime form, the first three parameters are
300-
# required, but the fourth (called `unit` but standing in for `hour`)
301-
# is optional. However, when using the legacy form, only the first
302-
# parameter is required and the fourth parameter defaults to `None`.
303-
# We use a special non-`None` constant to distinguish when callers
304-
# pass `None` as the fourth pydatetime parameter.
299+
# There are three calling forms:
300+
# - In the legacy form, the first parameter, ts_input, is required
301+
# and may be datetime-like, str, int, or float. The second parameter,
302+
# offset, is optional and may be str or DateOffset.
303+
# - ints in the first, second, and third arguments indicate
304+
# pydatetime positional arguments. Only the first 8 arguments
305+
# (standing in for year, month, day, hour, minute, second,
306+
# microsecond, tzinfo) may be non-None. As a shortcut, we just check
307+
# that the second argument is an int.
308+
# - Nones for the first four (legacy) arguments indicate pydatetime
309+
# keyword arguments. year, month, and day are required. As a
310+
# shortcut, we just check that the first argument is None.
311+
#
312+
# Mixing pydatetime positional and keyword arguments is forbidden!
305313

306314
cdef _TSObject ts
307315
cdef _Timestamp ts_base
308316

309-
if offset is not None and is_integer_object(offset):
317+
if ts_input is None:
318+
# User passed keyword arguments.
319+
return Timestamp(datetime(year, month, day, hour or 0,
320+
minute or 0, second or 0, microsecond or 0, tzinfo),
321+
tz=tzinfo)
322+
if is_integer_object(offset):
310323
# User passed positional arguments:
311324
# Timestamp(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])
312-
# When using the positional form, the first three parameters are
313-
# required. Assign defaults to the rest.
314-
if unit is _no_unit:
315-
unit = 0
316-
# Forward positional arguments to datetime constructor.
317-
return Timestamp(datetime(ts_input, offset, tz, unit, minute, second, microsecond, tzinfo),
318-
tz=tzinfo)
319-
elif unit is _no_unit:
320-
unit = None
325+
return Timestamp(datetime(ts_input, offset, tz, unit or 0,
326+
year or 0, month or 0, day or 0, hour), tz=hour)
321327

322328
ts = convert_to_tsobject(ts_input, tz, unit, 0, 0)
323329

0 commit comments

Comments
 (0)