@@ -176,8 +176,8 @@ def _guess_datetime_format_for_array(arr, **kwargs):
176
176
177
177
178
178
def to_datetime (arg , errors = 'raise' , dayfirst = False , yearfirst = False ,
179
- utc = None , box = True , format = None , exact = True ,
180
- unit = None , infer_datetime_format = False ):
179
+ utc = None , box = True , format = None , exact = True , coerce = None ,
180
+ unit = None , infer_datetime_format = False , origin = 'epoch' ):
181
181
"""
182
182
Convert argument to datetime.
183
183
@@ -236,6 +236,19 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
236
236
datetime strings, and if it can be inferred, switch to a faster
237
237
method of parsing them. In some cases this can increase the parsing
238
238
speed by ~5-10x.
239
+ origin : scalar convertible to Timestamp / string ('julian', 'epoch'),
240
+ default 'epoch'.
241
+ Define reference date. The numeric values would be parsed as number
242
+ of units (defined by `unit`) since this reference date.
243
+
244
+ - If 'epoch', origin is set to 1970-01-01.
245
+ - If 'julian', unit must be 'D', and origin is set to beginning of
246
+ Julian Calendar. Julian day number 0 is assigned to the day starting
247
+ at noon on January 1, 4713 BC.
248
+ - If Timestamp convertible, origin is set to Timestamp identified by
249
+ origin.
250
+
251
+ .. versionadded: 0.19.0
239
252
240
253
Returns
241
254
-------
@@ -297,8 +310,14 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
297
310
>>> %timeit pd.to_datetime(s,infer_datetime_format=False)
298
311
1 loop, best of 3: 471 ms per loop
299
312
300
- """
313
+ Using non-epoch origins to parse date
314
+
315
+ >>> pd.to_datetime([1,2,3], unit='D', origin=pd.Timestamp('1960-01-01'))
316
+ 0 1960-01-02
317
+ 1 1960-01-03
318
+ 2 1960-01-04
301
319
320
+ """
302
321
from pandas .tseries .index import DatetimeIndex
303
322
304
323
tz = 'utc' if utc else None
@@ -409,22 +428,43 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
409
428
except (ValueError , TypeError ):
410
429
raise e
411
430
412
- if arg is None :
413
- return arg
414
- elif isinstance (arg , tslib .Timestamp ):
415
- return arg
416
- elif isinstance (arg , ABCSeries ):
417
- from pandas import Series
418
- values = _convert_listlike (arg ._values , False , format )
419
- return Series (values , index = arg .index , name = arg .name )
420
- elif isinstance (arg , (ABCDataFrame , MutableMapping )):
421
- return _assemble_from_unit_mappings (arg , errors = errors )
422
- elif isinstance (arg , ABCIndexClass ):
423
- return _convert_listlike (arg , box , format , name = arg .name )
424
- elif is_list_like (arg ):
425
- return _convert_listlike (arg , box , format )
431
+ def intermediate_result (arg ):
432
+ if origin == 'julian' :
433
+ if unit != 'D' :
434
+ raise ValueError ("unit must be 'D' for origin='julian'" )
435
+ try :
436
+ arg = arg - tslib .Timestamp (0 ).to_julian_date ()
437
+ except :
438
+ raise ValueError ("incompatible 'arg' type for given "
439
+ "'origin'='julian'" )
440
+ if arg is None :
441
+ return arg
442
+ elif isinstance (arg , tslib .Timestamp ):
443
+ return arg
444
+ elif isinstance (arg , ABCSeries ):
445
+ from pandas import Series
446
+ values = _convert_listlike (arg ._values , False , format )
447
+ return Series (values , index = arg .index , name = arg .name )
448
+ elif isinstance (arg , (ABCDataFrame , MutableMapping )):
449
+ return _assemble_from_unit_mappings (arg , errors = errors )
450
+ elif isinstance (arg , ABCIndexClass ):
451
+ return _convert_listlike (arg , box , format , name = arg .name )
452
+ elif is_list_like (arg ):
453
+ return _convert_listlike (arg , box , format )
454
+ return _convert_listlike (np .array ([arg ]), box , format )[0 ]
455
+
456
+ result = intermediate_result (arg )
457
+
458
+ offset = None
459
+ if origin not in ['epoch' , 'julian' ]:
460
+ try :
461
+ offset = tslib .Timestamp (origin ) - tslib .Timestamp (0 )
462
+ except ValueError :
463
+ raise ValueError ("Invalid 'origin' or 'origin' Out of Bound" )
426
464
427
- return _convert_listlike (np .array ([arg ]), box , format )[0 ]
465
+ if offset is not None :
466
+ result = result + offset
467
+ return result
428
468
429
469
430
470
# mappings for assembling units
0 commit comments