Skip to content

Commit 4710e31

Browse files
committed
add validator for timedelta
1 parent c650812 commit 4710e31

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import math
55
from numbers import Integral, Number
6+
from datetime import timedelta
67

78
import numpy as np
89
from numpy import ma
@@ -3289,8 +3290,8 @@ def has_negative_values(array):
32893290
return False
32903291
try:
32913292
return np.any(array < 0)
3292-
except TypeError:
3293-
pass # Don't fail on 'datetime.*' types
3293+
except TypeError: # if array contains 'datetime.timedelta' types
3294+
return np.any(array < timedelta(0))
32943295

32953296
if has_negative_values(xerr) or has_negative_values(yerr):
32963297
raise ValueError(

lib/matplotlib/tests/test_axes.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3524,6 +3524,12 @@ def test_xerr_yerr_positive():
35243524
ax.errorbar(x=[0], y=[0], xerr=[[-0.5], [1]])
35253525
with pytest.raises(ValueError, match=error_message):
35263526
ax.errorbar(x=[0], y=[0], yerr=[[-0.5], [1]])
3527+
with pytest.raises(ValueError, match=error_message):
3528+
x = np.arange(5)
3529+
y = [datetime.datetime(2021, 9, i * 2 + 1) for i in x]
3530+
ax.errorbar(x=x,
3531+
y=y,
3532+
yerr=datetime.timedelta(days=-10))
35273533

35283534

35293535
@check_figures_equal()

0 commit comments

Comments
 (0)