@@ -255,6 +255,9 @@ def _dt64_to_ordinalf(d):
255
255
because we do times compared to ``0001-01-01T00:00:00`` (plus one day).
256
256
"""
257
257
258
+ if isinstance (d , np .timedelta64 ):
259
+ return d / np .timedelta64 (1 , 'D' )
260
+
258
261
# the "extra" ensures that we at least allow the dynamic range out to
259
262
# seconds. That should get out to +/-2e11 years.
260
263
# NOTE: First cast truncates; second cast back is for NumPy 1.10.
@@ -275,6 +278,13 @@ def _dt64_to_ordinalf(d):
275
278
return dt
276
279
277
280
281
+ def _dt64_to_ordinalf_iterable (d ):
282
+ dnew = np .zeros (len (d ))
283
+ for nn , dd in enumerate (d ):
284
+ dnew [nn ] = _dt64_to_ordinalf (dd )
285
+ return (dnew )
286
+
287
+
278
288
def _from_ordinalf (x , tz = None ):
279
289
"""
280
290
Convert Gregorian float of the date, preserving hours, minutes,
@@ -413,19 +423,32 @@ def date2num(d):
413
423
if hasattr (d , "values" ):
414
424
# this unpacks pandas series or dataframes...
415
425
d = d .values
416
- if not np .iterable (d ):
417
- if (isinstance (d , np .datetime64 ) or (isinstance (d , np .ndarray ) and
418
- np .issubdtype (d .dtype , np .datetime64 ))):
419
- return _dt64_to_ordinalf (d )
420
- return _to_ordinalf (d )
421
426
422
- else :
423
- d = np .asarray (d )
424
- if np .issubdtype (d .dtype , np .datetime64 ):
427
+ if hasattr (d , 'size' ) and not d .size :
428
+ # this is an empty
429
+ return d
430
+
431
+ if not np .iterable (d ) and not isinstance (d , np .ndarray ):
432
+ # single value logic...
433
+ if (isinstance (d , np .datetime64 ) or isinstance (d , np .timedelta64 )):
425
434
return _dt64_to_ordinalf (d )
426
- if not d .size :
427
- return d
435
+ else :
436
+ return _to_ordinalf (d )
437
+
438
+ elif (isinstance (d , np .ndarray ) and
439
+ (np .issubdtype (d .dtype , np .datetime64 ) or
440
+ np .issubdtype (d .dtype , np .timedelta64 ))):
441
+ # array with all one type of datetime64 object.
442
+ return _dt64_to_ordinalf (d )
443
+
444
+ elif len (d ):
445
+ # this is a list or tuple...
446
+ if (isinstance (d [0 ], np .datetime64 ) or
447
+ isinstance (d [0 ], np .timedelta64 )):
448
+ return _dt64_to_ordinalf_iterable (d )
428
449
return _to_ordinalf_np_vectorized (d )
450
+ else :
451
+ return []
429
452
430
453
431
454
def julian2num (j ):
0 commit comments