Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.1k
Improving error message for width and position type mismatch in violinplot#30545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Conversation
ifany(isinstance(p, (datetime.datetime,datetime.date)) | ||
forpinpositions): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Isn't it enough to check the first position? I think mixed positions are not allowed anyway. Also, do we need to handle np.datetime64 arrays?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@hasanrashid You've marked this as resolved. Please comment how my above questions are addressed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@timhoffm Yes, it is enough to check the first position. The position argument can't mix datetime with float. width can't mix timedelta and float either.
I was approaching this by converting entries into numeric equivalent until it was suggested that I provide clearer message instead.
Also, yes, datetime64 isn't needed either. I was thinking of all test cases, but I realize datetime64 isn't a supported type in violinplot.
Making the adjustments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Why is datetime64 not supported? Should we rather fix this? (Can be later, but already add the check here?)
hasanrashidSep 28, 2025 • edited by timhoffm
Loading Uh oh!
There was an error while loading.Please reload this page.
edited by timhoffm
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
If I try a testcase like this:
deftest_np_datetime64_positions_with_float_widths_behaves_like_numeric():"""Test that np.datetime64 positions with float widths do not raise TypeError (treated as numeric)."""fig,ax=plt.subplots()try:vpstats=violin_plot_stats()positions= [np.datetime64('2020-01-01'),np.datetime64('2021-01-01')]widths= [0.5,1.0]# Should not raise TypeError, but may raise downstream if not supportedax.violin(vpstats,positions=positions,widths=widths)finally:plt.close(fig)
This produces the following error:numpy._core._exceptions._UFuncBinaryResolutionError: ufunc 'add' cannot use operands with types dtype('float64') and dtype('<M8[D]')
I took this to mean that datetime64 was not an expected type of datetime for biolinplots- thus unsupported.
If we need to investigate adding datetime64 support in violineplot, should it be another issue? We are updating the error message enforcing timedelta with datetimes; datetime64 support- to me- seems like it should be investigated in a separate issue (which I am happy to open and work on)
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Please integrate the tests here:
matplotlib/lib/matplotlib/tests/test_axes.py
Line 4124 inee6c7f6
deftest_violinplot_bad_widths(): |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
The dviread module logs some information at the debug level (e.g., dvi"specials"). Allow printing them from the CLI, to ease verification ofdvi internals.
Although event.step is only nonzero for scroll events, it seemsreasonable to always add it to str(MouseEvent). After all, that str()always contains e.g. dblclick, which doesn't make sense formotion_notify_event either. (IOW the alternative would be to morecarefully write different str()s for each kind of MouseEvents.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Can you please clean up the commits? There are several unrelated commits included.
# Proactive validation: if positions are datetime-like | ||
# widths must be timedelta-like. | ||
ifany(isinstance(p, (datetime.datetime,datetime.date)) | ||
forpinpositions): | ||
_widths=widthsifnotnp.isscalar(widths)else [widths]*N | ||
ifany(notisinstance(w, (datetime.timedelta,np.timedelta64)) | ||
forwin_widths): | ||
raiseTypeError( | ||
"If positions are datetime/date values, pass widths as " | ||
"datetime.timedelta (e.g., datetime.timedelta(days=10))" | ||
"or numpy.timedelta64." | ||
) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Let's do it like this:
# Proactive validation: if positions are datetime-like | |
# widths must be timedelta-like. | |
ifany(isinstance(p, (datetime.datetime,datetime.date)) | |
forpinpositions): | |
_widths=widthsifnotnp.isscalar(widths)else [widths]*N | |
ifany(notisinstance(w, (datetime.timedelta,np.timedelta64)) | |
forwin_widths): | |
raiseTypeError( | |
"If positions are datetime/date values, pass widths as " | |
"datetime.timedelta (e.g., datetime.timedelta(days=10))" | |
"or numpy.timedelta64." | |
) | |
# For usability / better error message: | |
# Validate that datetime-like positions have timedelta-like widths. | |
# Checking only the first element is good enough for standard misuse cases | |
pos0=positions[0] | |
width0=widthsifnp.isscalar(widths)elsewidths[0] | |
if (isinstance(pos0, (datetime.datetime,datetime.date)) | |
andnotisinstance(width0,datetime.timedelta)): | |
raiseTypeError( | |
"datetime/date 'position' values, require timedelta 'widths'") | |
elif (isinstance(pos0,np.datetime64) | |
andnotisinstance(width0,np.timedelta64): | |
raiseTypeError( | |
"np.datetime64 'position' values, require np.timdelta64 'widths'") |
Uh oh!
There was an error while loading.Please reload this page.
This PR intends to close [ENH]: Support using datetimes as positions argument to violin(...)#30417
It should be possible to set the position of a violin plot to be a datetime. Currently, an attempt to do this results in this error message: TypeError: unsupported operand type(s) for +: 'float' and 'datetime.datetime'
The error stems from trying to perform operations between float and datetime if datetime was provided as position arguments.
The proposed solution improves the error message to be:
"If positions are datetime/date values, pass widths as datetime.timedelta (e.g., datetime.timedelta(days=10)) or numpy.timedelta64.
unit tests are in tests\test_violinplot_datetime.py
I had opened another PR30508, but messed up the commits while making changes. I am making this one after reading the suggestion here:#30508 (comment) by@rcomer . This change updates the error message instead of converting the position and width