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

Skip to content
Merged
13 changes: 13 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7418,6 +7418,19 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
x = cbook._reshape_2D(x, 'x')
nx = len(x) # number of datasets

for arr in x:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, I'm not in favor of checking for all kinds of erronous inputs. This is not good, but it's possibly the least bad option (other than making timedelta work properlyI) because timedelta is an accepted input in other cases.

if (
hasattr(arr, "dtype") and np.issubdtype(arr.dtype, np.timedelta64)
or (
len(arr) > 0
and isinstance(arr[0], (datetime.timedelta, np.timedelta64))
)
):
Comment thread
scottshambaugh marked this conversation as resolved.
Outdated
raise TypeError(
"Axes.hist does not currently support timedelta inputs. "
"Convert to numeric values (e.g., .total_seconds()) first."
Comment thread
timhoffm marked this conversation as resolved.
Outdated
)

# Process unit information. _process_unit_info sets the unit and
# converts the first dataset; then we convert each following dataset
# one at a time.
Expand Down
15 changes: 15 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2461,6 +2461,21 @@ def test_hist_log_barstacked():
assert axs[0].get_ylim() == axs[1].get_ylim()


def test_hist_timedelta_raises():
import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

arr_np = np.array([1,2,5,7], dtype="timedelta64[D]")
Comment thread
scottshambaugh marked this conversation as resolved.
Outdated
with pytest.raises(TypeError, match="does not currently support timedelta inputs"):
ax.hist(arr_np)

arr_py = [datetime.timedelta(seconds=i) for i in range(5)]
with pytest.raises(TypeError, match="does not currently support timedelta inputs"):
ax.hist(arr_py)

Comment thread
timhoffm marked this conversation as resolved.

@image_comparison(['hist_bar_empty.png'], remove_text=True)
def test_hist_bar_empty():
# From #3886: creating hist from empty dataset raises ValueError
Expand Down
Loading