Skip to content

Commit 6fad30b

Browse files
Fix docstring; add tests for kwargs
1 parent 5c34c04 commit 6fad30b

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

pandas/tseries/tests/test_tslib.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,39 @@ def test_constructor_positional(self):
204204
self.assertEqual(Timestamp(*pos_args).to_pydatetime(),
205205
datetime.datetime(*pos_args))
206206

207+
def test_constructor_keyword(self):
208+
# GH 10758
209+
with tm.assertRaises(TypeError):
210+
Timestamp(year=2000, month=1)
211+
with tm.assertRaises(ValueError):
212+
Timestamp(year=2000, month=0, day=1)
213+
with tm.assertRaises(ValueError):
214+
Timestamp(year=2000, month=13, day=1)
215+
with tm.assertRaises(ValueError):
216+
Timestamp(year=2000, month=1, day=0)
217+
with tm.assertRaises(ValueError):
218+
Timestamp(year=2000, month=1, day=32)
219+
220+
ts = Timestamp(year=2000, month=1, day=2)
221+
222+
actual = ts.to_pydatetime()
223+
expected = datetime.datetime(year=2000, month=1, day=2)
224+
self.assertEqual(actual, expected)
225+
self.assertEqual(type(actual), type(expected))
226+
227+
pos_args = [2000, 1, 2, 3, 4, 5, 999999]
228+
kw_args = {
229+
'year': 2000,
230+
'month': 1,
231+
'day': 2,
232+
'hour': 3,
233+
'minute': 4,
234+
'second': 5,
235+
'microsecond': 999999,
236+
}
237+
self.assertEqual(Timestamp(**kw_args).to_pydatetime(),
238+
datetime.datetime(**kw_args))
239+
207240
def test_conversion(self):
208241
# GH 9255
209242
ts = Timestamp('2000-01-01')

pandas/tslib.pyx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,12 @@ class Timestamp(_Timestamp):
225225
for the entries that make up a DatetimeIndex, and other timeseries
226226
oriented data structures in pandas.
227227
228-
Parameters
229-
----------
228+
There are essentially three calling conventions for the constructor. The
229+
first, legacy form accepts four parameters. They can be passed by
230+
position or keyword.
231+
232+
Legacy Parameters
233+
-----------------
230234
ts_input : datetime-like, str, int, float
231235
Value to be converted to Timestamp
232236
offset : str, DateOffset
@@ -235,6 +239,20 @@ class Timestamp(_Timestamp):
235239
Time zone for time which Timestamp will have.
236240
unit : string
237241
numpy unit used for conversion, if ts_input is int or float
242+
243+
The other two forms copy the parameters from datetime.datetime. They can
244+
be passed by either position or keyword, but not both mixed together.
245+
246+
datetime.datetime Parameters
247+
------------------------------
248+
year : int
249+
month : int
250+
day : int
251+
hour : int [optional]
252+
minute : int [optional]
253+
second : int [optional]
254+
microsecond : int [optional]
255+
tzinfo : datetime.tzinfo [optional]
238256
"""
239257

240258
@classmethod
@@ -319,7 +337,7 @@ class Timestamp(_Timestamp):
319337
return Timestamp(datetime(year, month, day, hour or 0,
320338
minute or 0, second or 0, microsecond or 0, tzinfo),
321339
tz=tzinfo)
322-
if is_integer_object(offset):
340+
elif is_integer_object(offset):
323341
# User passed positional arguments:
324342
# Timestamp(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])
325343
return Timestamp(datetime(ts_input, offset, tz, unit or 0,

0 commit comments

Comments
 (0)