Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 526ad8a

Browse files
committed
Run c722367: Fix Axes.hist crash for numpy timedelta64 inputs (matplotlib#31203)
1 parent 2e67022 commit 526ad8a

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

lib/matplotlib/axes/_axes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7421,6 +7421,15 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
74217421
x = cbook._reshape_2D(x, 'x')
74227422
nx = len(x) # number of datasets
74237423

7424+
for arr in x:
7425+
if len(arr) > 0 and isinstance(
7426+
arr[0], (datetime.timedelta, np.timedelta64)
7427+
):
7428+
raise TypeError(
7429+
"Axes.hist does not currently support timedelta inputs. "
7430+
"Convert to numeric values (e.g., .total_seconds()) first."
7431+
)
7432+
74247433
# Process unit information. _process_unit_info sets the unit and
74257434
# converts the first dataset; then we convert each following dataset
74267435
# one at a time.

lib/matplotlib/tests/test_axes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,6 +2461,21 @@ def test_hist_log_barstacked():
24612461
assert axs[0].get_ylim() == axs[1].get_ylim()
24622462

24632463

2464+
def test_hist_timedelta_raises():
2465+
import numpy as np
2466+
import matplotlib.pyplot as plt
2467+
2468+
fig, ax = plt.subplots()
2469+
2470+
arr_np = np.array([1, 2, 5, 7], dtype="timedelta64[D]")
2471+
with pytest.raises(TypeError, match="does not currently support timedelta inputs"):
2472+
ax.hist(arr_np)
2473+
2474+
arr_py = [datetime.timedelta(seconds=i) for i in range(5)]
2475+
with pytest.raises(TypeError, match="does not currently support timedelta inputs"):
2476+
ax.hist(arr_py)
2477+
2478+
24642479
@image_comparison(['hist_bar_empty.png'], remove_text=True)
24652480
def test_hist_bar_empty():
24662481
# From #3886: creating hist from empty dataset raises ValueError

0 commit comments

Comments
 (0)