Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
hasanrashid wants to merge9 commits intomatplotlib:main
base:main
Choose a base branch
Loading
fromhasanrashid:enh-30417

Conversation

hasanrashid
Copy link
Contributor

@hasanrashidhasanrashid commentedSep 11, 2025
edited
Loading

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

Comment on lines +9053 to +9056
ifany(isinstance(p, (datetime.datetime,datetime.date))
forpinpositions):
Copy link
Member

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?

Copy link
Member

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.

Copy link
ContributorAuthor

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

Copy link
Member

@timhoffmtimhoffmSep 21, 2025
edited
Loading

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?)

Copy link
ContributorAuthor

@hasanrashidhasanrashidSep 28, 2025
edited by timhoffm
Loading

Choose a reason for hiding this comment

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

@timhoffm

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)

Copy link
Member

@timhoffmtimhoffm left a 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:

deftest_violinplot_bad_widths():

@hasanrashidhasanrashid deleted the enh-30417 branchOctober 1, 2025 22:52
hasanrashidand others added9 commitsOctober 1, 2025 20:46
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.)
Copy link
Member

@timhoffmtimhoffm left a 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.

Comment on lines +9053 to +9065
# 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."
)

Copy link
Member

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:

Suggested change
# 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'")

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@timhoffmtimhoffmtimhoffm left review comments

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@hasanrashid@timhoffm@dstansby@anntzer

[8]ページ先頭

©2009-2025 Movatter.jp