@@ -222,6 +222,10 @@ def _to_ordinalf(dt):
222
222
dt = dt .astimezone (UTC )
223
223
tzi = UTC
224
224
225
+ if isinstance (dt , datetime .timedelta ):
226
+ base = dt / datetime .timedelta (days = 1 )
227
+ return base
228
+
225
229
base = float (dt .toordinal ())
226
230
227
231
# If it's sufficiently datetime-like, it will have a `date()` method
@@ -251,6 +255,11 @@ def _dt64_to_ordinalf(d):
251
255
because we do times compared to ``0001-01-01T00:00:00`` (plus one day).
252
256
"""
253
257
258
+ if (isinstance (d , np .timedelta64 ) or
259
+ (isinstance (d , np .ndarray ) and
260
+ np .issubdtype (d .dtype , np .timedelta64 ))):
261
+ return d / np .timedelta64 (1 , 'D' )
262
+
254
263
# the "extra" ensures that we at least allow the dynamic range out to
255
264
# seconds. That should get out to +/-2e11 years.
256
265
# NOTE: First cast truncates; second cast back is for NumPy 1.10.
@@ -271,6 +280,12 @@ def _dt64_to_ordinalf(d):
271
280
return dt
272
281
273
282
283
+ def _dt64_to_ordinalf_iterable (d ):
284
+ dnew = np .zeros (len (d ))
285
+ for nn , dd in enumerate (d ):
286
+ dnew [nn ] = _dt64_to_ordinalf (dd )
287
+ return dnew
288
+
274
289
def _from_ordinalf (x , tz = None ):
275
290
"""
276
291
Convert Gregorian float of the date, preserving hours, minutes,
@@ -405,22 +420,36 @@ def date2num(d):
405
420
Gregorian calendar is assumed; this is not universal practice.
406
421
For details see the module docstring.
407
422
"""
423
+
408
424
if hasattr (d , "values" ):
409
425
# this unpacks pandas series or dataframes...
410
426
d = d .values
411
- if not np .iterable (d ):
412
- if (isinstance (d , np .datetime64 ) or (isinstance (d , np .ndarray ) and
413
- np .issubdtype (d .dtype , np .datetime64 ))):
414
- return _dt64_to_ordinalf (d )
415
- return _to_ordinalf (d )
416
427
417
- else :
418
- d = np . asarray ( d )
419
- if np .issubdtype ( d . dtype , np .datetime64 ):
428
+ if not np . iterable ( d ) and not isinstance ( d , np . ndarray ) :
429
+ # single value logic...
430
+ if ( isinstance ( d , np .datetime64 ) or isinstance ( d , np .timedelta64 ) ):
420
431
return _dt64_to_ordinalf (d )
421
- if not d .size :
422
- return d
432
+ else :
433
+ return _to_ordinalf (d )
434
+
435
+ elif (isinstance (d , np .ndarray ) and
436
+ (np .issubdtype (d .dtype , np .datetime64 ) or
437
+ np .issubdtype (d .dtype , np .timedelta64 ))):
438
+ # array with all one type of datetime64 object.
439
+ return _dt64_to_ordinalf (d )
440
+
441
+ elif len (d ):
442
+ # this is a list or tuple...
443
+ if (isinstance (d [0 ], np .datetime64 ) or
444
+ isinstance (d [0 ], np .timedelta64 )):
445
+ return _dt64_to_ordinalf_iterable (d )
423
446
return _to_ordinalf_np_vectorized (d )
447
+ elif hasattr (d , 'size' ) and not d .size :
448
+ # this elif doesn't get tested, but leaving here in case anyone
449
+ # needs it.
450
+ return d
451
+ else :
452
+ return []
424
453
425
454
426
455
def julian2num (j ):
0 commit comments