Skip to content

Commit 5cbdf48

Browse files
committed
Simplify implementation of vectorized date operations.
1) We don't need to handle separately the scalar case -- tolist() returns a scalar when called on a 0d-array, as returned by np.vectorize() when called on a scalar. 2) We don't need to handle separately the empty case, as long as the output type ("O") is passed to np.vectorize.
1 parent 45b9201 commit 5cbdf48

File tree

1 file changed

+5
-21
lines changed

1 file changed

+5
-21
lines changed

lib/matplotlib/dates.py

+5-21
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def _from_ordinalf(x, tz=None):
305305

306306

307307
# a version of _from_ordinalf that can operate on numpy arrays
308-
_from_ordinalf_np_vectorized = np.vectorize(_from_ordinalf)
308+
_from_ordinalf_np_vectorized = np.vectorize(_from_ordinalf, otypes="O")
309309

310310

311311
@cbook.deprecated(
@@ -498,20 +498,11 @@ def num2date(x, tz=None):
498498
"""
499499
if tz is None:
500500
tz = _get_rc_timezone()
501-
if not np.iterable(x):
502-
return _from_ordinalf(x, tz)
503-
else:
504-
x = np.asarray(x)
505-
if not x.size:
506-
return x
507-
return _from_ordinalf_np_vectorized(x, tz).tolist()
508-
509-
510-
def _ordinalf_to_timedelta(x):
511-
return datetime.timedelta(days=x)
501+
return _from_ordinalf_np_vectorized(x, tz).tolist()
512502

513503

514-
_ordinalf_to_timedelta_np_vectorized = np.vectorize(_ordinalf_to_timedelta)
504+
_ordinalf_to_timedelta_np_vectorized = np.vectorize(
505+
lambda x: datetime.timedelta(days=x), otypes="O")
515506

516507

517508
def num2timedelta(x):
@@ -529,15 +520,8 @@ def num2timedelta(x):
529520
Returns
530521
-------
531522
`datetime.timedelta` or list[`datetime.timedelta`]
532-
533523
"""
534-
if not np.iterable(x):
535-
return _ordinalf_to_timedelta(x)
536-
else:
537-
x = np.asarray(x)
538-
if not x.size:
539-
return x
540-
return _ordinalf_to_timedelta_np_vectorized(x).tolist()
524+
return _ordinalf_to_timedelta_np_vectorized(x).tolist()
541525

542526

543527
def drange(dstart, dend, delta):

0 commit comments

Comments
 (0)