@@ -244,53 +244,10 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
244
244
245
245
Examples
246
246
--------
247
- Take separate series and convert to datetime
248
-
249
- >>> import pandas as pd
250
- >>> i = pd.date_range('20000101',periods=100)
251
- >>> df = pd.DataFrame(dict(year = i.year, month = i.month, day = i.day))
252
- >>> pd.to_datetime(df.year*10000 + df.month*100 + df.day, format='%Y%m%d')
253
- 0 2000-01-01
254
- 1 2000-01-02
255
- ...
256
- 98 2000-04-08
257
- 99 2000-04-09
258
- Length: 100, dtype: datetime64[ns]
259
-
260
- Or from strings
261
-
262
- >>> dfs = df.astype(str)
263
- >>> pd.to_datetime(dfs.day + dfs.month + dfs.year, format="%d%m%Y")
264
- 0 2000-01-01
265
- 1 2000-01-02
266
- ...
267
- 98 2000-04-08
268
- 99 2000-04-09
269
- Length: 100, dtype: datetime64[ns]
270
-
271
- Infer the format from the first entry
272
-
273
- >>> pd.to_datetime(dfs.month + '/' + dfs.day + '/' + dfs.year,
274
- infer_datetime_format=True)
275
- 0 2000-01-01
276
- 1 2000-01-02
277
- ...
278
- 98 2000-04-08
279
- 99 2000-04-09
280
-
281
- This gives the same results as omitting the `infer_datetime_format=True`,
282
- but is much faster.
283
-
284
- Date that does not meet timestamp limitations:
285
-
286
- >>> pd.to_datetime('13000101', format='%Y%m%d')
287
- datetime.datetime(1300, 1, 1, 0, 0)
288
- >>> pd.to_datetime('13000101', format='%Y%m%d', errors='coerce')
289
- NaT
290
-
291
247
292
248
Assembling a datetime from multiple columns of a DataFrame. The keys can be
293
- strptime-like (%Y, %m) or common abbreviations like ('year', 'month')
249
+ common abbreviations like ['year', 'month', 'day', 'minute', 'second',
250
+ 'ms', 'us', 'ns']) or plurals of the same
294
251
295
252
>>> df = pd.DataFrame({'year': [2015, 2016],
296
253
'month': [2, 3],
@@ -300,6 +257,12 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
300
257
1 2016-03-05
301
258
dtype: datetime64[ns]
302
259
260
+ Date that does not meet timestamp limitations:
261
+
262
+ >>> pd.to_datetime('13000101', format='%Y%m%d')
263
+ datetime.datetime(1300, 1, 1, 0, 0)
264
+ >>> pd.to_datetime('13000101', format='%Y%m%d', errors='coerce')
265
+ NaT
303
266
"""
304
267
return _to_datetime (arg , errors = errors , dayfirst = dayfirst ,
305
268
yearfirst = yearfirst ,
@@ -439,31 +402,21 @@ def _convert_listlike(arg, box, format, name=None):
439
402
440
403
# mappings for assembling units
441
404
_unit_map = {'year' : 'year' ,
442
- 'y' : 'year' ,
443
- '%Y' : 'year' ,
405
+ 'years' : 'year' ,
444
406
'month' : 'month' ,
445
- 'M' : 'month' ,
446
- '%m' : 'month' ,
407
+ 'months' : 'month' ,
447
408
'day' : 'day' ,
448
409
'days' : 'day' ,
449
- 'd' : 'day' ,
450
- '%d' : 'day' ,
451
- 'h' : 'h' ,
452
410
'hour' : 'h' ,
453
- 'hh' : 'h' ,
454
- '%H' : 'h' ,
411
+ 'hours' : 'h' ,
455
412
'minute' : 'm' ,
456
- 't ' : 'm' ,
413
+ 'minutes ' : 'm' ,
457
414
'min' : 'm' ,
458
- '%M' : 'm' ,
459
- 'mm' : 'm' ,
460
- 'MM' : 'm' ,
461
- '%M' : 'm' ,
462
- 's' : 's' ,
463
- 'seconds' : 's' ,
415
+ 'mins' : 'm' ,
464
416
'second' : 's' ,
465
- '%S' : 's' ,
466
- 'ss' : 's' ,
417
+ 'seconds' : 's' ,
418
+ 'sec' : 's' ,
419
+ 'secs' : 's' ,
467
420
'ms' : 'ms' ,
468
421
'millisecond' : 'ms' ,
469
422
'milliseconds' : 'ms' ,
@@ -505,7 +458,7 @@ def f(value):
505
458
return _unit_map [value ]
506
459
507
460
# m is case significant
508
- if value .lower () in _unit_map and not value . startswith ( 'm' ) :
461
+ if value .lower () in _unit_map :
509
462
return _unit_map [value .lower ()]
510
463
511
464
return value
0 commit comments