@@ -255,6 +255,11 @@ 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 ) or
259
+ (isinstance (d , np .ndarray ) and
260
+ np .issubdtype (d .dtype , np .timedelta64 ))):
261
+ return d / np .timedelta64 (1 , 'D' )
262
+
258
263
# the "extra" ensures that we at least allow the dynamic range out to
259
264
# seconds. That should get out to +/-2e11 years.
260
265
# NOTE: First cast truncates; second cast back is for NumPy 1.10.
@@ -275,6 +280,13 @@ def _dt64_to_ordinalf(d):
275
280
return dt
276
281
277
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
+
289
+
278
290
def _from_ordinalf (x , tz = None ):
279
291
"""
280
292
Convert Gregorian float of the date, preserving hours, minutes,
@@ -413,19 +425,31 @@ def date2num(d):
413
425
if hasattr (d , "values" ):
414
426
# this unpacks pandas series or dataframes...
415
427
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
428
422
- else :
423
- d = np . asarray ( d )
424
- if np .issubdtype ( d . dtype , np .datetime64 ):
429
+ if not np . iterable ( d ) and not isinstance ( d , np . ndarray ) :
430
+ # single value logic...
431
+ if ( isinstance ( d , np .datetime64 ) or isinstance ( d , np .timedelta64 ) ):
425
432
return _dt64_to_ordinalf (d )
426
- if not d .size :
427
- return d
433
+ else :
434
+ return _to_ordinalf (d )
435
+
436
+ elif (isinstance (d , np .ndarray ) and
437
+ (np .issubdtype (d .dtype , np .datetime64 ) or
438
+ np .issubdtype (d .dtype , np .timedelta64 ))):
439
+ # array with all one type of datetime64 object.
440
+ return _dt64_to_ordinalf (d )
441
+
442
+ elif len (d ):
443
+ # this is a list or tuple...
444
+ if (isinstance (d [0 ], np .datetime64 ) or
445
+ isinstance (d [0 ], np .timedelta64 )):
446
+ return _dt64_to_ordinalf_iterable (d )
428
447
return _to_ordinalf_np_vectorized (d )
448
+ elif hasattr (d , 'size' ) and not d .size :
449
+ # this is an empty
450
+ return d
451
+ else :
452
+ return []
429
453
430
454
431
455
def julian2num (j ):
0 commit comments