@@ -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,11 @@ def _dt64_to_ordinalf(d):
271
280
return dt
272
281
273
282
283
+ def _dt64_to_ordinalf_iterable (d ):
284
+ return np .fromiter ((_dt64_to_ordinalf (dd ) for dd in d ),
285
+ float , count = len (d ))
286
+
287
+
274
288
def _from_ordinalf (x , tz = None ):
275
289
"""
276
290
Convert Gregorian float of the date, preserving hours, minutes,
@@ -405,22 +419,36 @@ def date2num(d):
405
419
Gregorian calendar is assumed; this is not universal practice.
406
420
For details see the module docstring.
407
421
"""
422
+
408
423
if hasattr (d , "values" ):
409
424
# this unpacks pandas series or dataframes...
410
425
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
426
417
- else :
418
- d = np . asarray ( d )
419
- if np .issubdtype ( d . dtype , np .datetime64 ):
427
+ if not np . iterable ( d ) and not isinstance ( d , np . ndarray ) :
428
+ # single value logic...
429
+ if ( isinstance ( d , np .datetime64 ) or isinstance ( d , np .timedelta64 ) ):
420
430
return _dt64_to_ordinalf (d )
421
- if not d .size :
422
- return d
431
+ else :
432
+ return _to_ordinalf (d )
433
+
434
+ elif (isinstance (d , np .ndarray ) and
435
+ (np .issubdtype (d .dtype , np .datetime64 ) or
436
+ np .issubdtype (d .dtype , np .timedelta64 ))):
437
+ # array with all one type of datetime64 object.
438
+ return _dt64_to_ordinalf (d )
439
+
440
+ elif len (d ):
441
+ # this is a list or tuple...
442
+ if (isinstance (d [0 ], np .datetime64 ) or
443
+ isinstance (d [0 ], np .timedelta64 )):
444
+ return _dt64_to_ordinalf_iterable (d )
423
445
return _to_ordinalf_np_vectorized (d )
446
+ elif hasattr (d , 'size' ) and not d .size :
447
+ # this elif doesn't get tested, but leaving here in case anyone
448
+ # needs it.
449
+ return d
450
+ else :
451
+ return []
424
452
425
453
426
454
def julian2num (j ):
0 commit comments