@@ -170,7 +170,7 @@ def _guess_datetime_format_for_array(arr, **kwargs):
170
170
mapping = {True : 'coerce' , False : 'raise' })
171
171
def to_datetime (arg , errors = 'raise' , dayfirst = False , yearfirst = False ,
172
172
utc = None , box = True , format = None , exact = True , coerce = None ,
173
- unit = None , infer_datetime_format = False ):
173
+ unit = None , infer_datetime_format = False , origin = 'epoch' ):
174
174
"""
175
175
Convert argument to datetime.
176
176
@@ -228,6 +228,17 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
228
228
datetime strings, and if it can be inferred, switch to a faster
229
229
method of parsing them. In some cases this can increase the parsing
230
230
speed by ~5-10x.
231
+ origin : scalar convertible to Timestamp / string ('julian', 'epoch'),
232
+ default 'epoch'.
233
+ Define relative offset for the returned dates.
234
+
235
+ - If 'epoch', offset is set to 1-1-1970.
236
+ - If 'julian', unit must be 'D', and offset is set to beginning of
237
+ Julian Calendar.
238
+ - If Timestamp convertible, offset is set to Timestamp identified by
239
+ origin.
240
+
241
+ .. versionadded: 0.18.1
231
242
232
243
Returns
233
244
-------
@@ -260,6 +271,14 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
260
271
If a date that does not meet timestamp limitations, passing errors='coerce'
261
272
will force to NaT. Furthermore this will force non-dates to NaT as well.
262
273
274
+ >>> pd.to_datetime(range(100), unit='D', origin=Timestamp('1960-01-01'))
275
+ 0 1960-01-01
276
+ 1 1960-01-02
277
+ ...
278
+ 98 1960-04-08
279
+ 99 1960-04-09
280
+
281
+
263
282
>>> pd.to_datetime('13000101', format='%Y%m%d')
264
283
datetime.datetime(1300, 1, 1, 0, 0)
265
284
>>> pd.to_datetime('13000101', format='%Y%m%d', errors='coerce')
@@ -285,10 +304,25 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
285
304
1 loop, best of 3: 471 ms per loop
286
305
287
306
"""
288
- return _to_datetime (arg , errors = errors , dayfirst = dayfirst ,
289
- yearfirst = yearfirst ,
290
- utc = utc , box = box , format = format , exact = exact ,
291
- unit = unit , infer_datetime_format = infer_datetime_format )
307
+ # variable to set offset as per origin parameter
308
+ offset = None
309
+
310
+ if origin == 'julian' :
311
+ if unit != 'D' :
312
+ raise ValueError ("unit must be 'D' for origin='julian'" )
313
+ arg = arg - tslib .Timestamp (0 ).to_julian_date ()
314
+ elif origin != 'epoch' :
315
+ offset = tslib .Timestamp (origin ) - tslib .Timestamp (0 )
316
+
317
+ result = _to_datetime (arg , errors = errors , dayfirst = dayfirst ,
318
+ yearfirst = yearfirst , utc = utc , box = box , format = format ,
319
+ exact = exact , unit = unit ,
320
+ infer_datetime_format = infer_datetime_format )
321
+
322
+ if offset is not None :
323
+ result = result + offset
324
+
325
+ return result
292
326
293
327
294
328
def _to_datetime (arg , errors = 'raise' , dayfirst = False , yearfirst = False ,
0 commit comments