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

Skip to content
Merged
22 changes: 22 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7418,6 +7418,28 @@ 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.

arr = np.asarray(arr)
Comment thread
scottshambaugh marked this conversation as resolved.
Outdated

# catch numpy.timedelta64
if np.issubdtype(arr.dtype, np.timedelta64):
raise TypeError(
"Axes.hist does not currently support timedelta inputs. "
"Convert to numeric values (e.g., .total_seconds()) first."
)

# catch python datetime.timedelta
if (
arr.dtype == object
and arr.size > 0
and isinstance(arr[0], datetime.timedelta)
):
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
)
Comment thread
scottshambaugh 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
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2461,6 +2461,17 @@ 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

arr = np.array([1,2,5,7], dtype="timedelta64[D]")
fig, ax = plt.subplots()

with pytest.raises(TypeError, match="does not currently support timedelta"):
ax.hist(arr)

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