🌐 AI搜索 & 代理 主页
Skip to content

Conversation

@hasanrashid
Copy link
Contributor

@hasanrashid hasanrashid commented Sep 11, 2025

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 PR 30508, 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
if any(isinstance(p, (datetime.datetime, datetime.date))
for p in positions):
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
Contributor Author

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

@timhoffm timhoffm Sep 21, 2025

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
Contributor Author

@hasanrashid hasanrashid Sep 28, 2025

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:

def test_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 supported
        ax.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

@timhoffm timhoffm 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:

def test_violinplot_bad_widths():

@hasanrashid hasanrashid closed this Oct 1, 2025
@hasanrashid hasanrashid deleted the enh-30417 branch October 1, 2025 22:52
@hasanrashid hasanrashid reopened this Oct 2, 2025
hasanrashid and others added 9 commits October 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 of
dvi internals.
Although event.step is only nonzero for scroll events, it seems
reasonable to always add it to str(MouseEvent).  After all, that str()
always contains e.g. dblclick, which doesn't make sense for
motion_notify_event either.  (IOW the alternative would be to more
carefully write different str()s for each kind of MouseEvents.)
Copy link
Member

@timhoffm timhoffm 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.

hasanrashid and others added 2 commits October 13, 2025 11:46
Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
@timhoffm
Copy link
Member

Still several unrelated commits. Do you need help with cleaning up the history?

@hasanrashid
Copy link
Contributor Author

Still several unrelated commits. Do you need help with cleaning up the history?
@timhoffm
Sorry, missed this message. Didn't realize this was still going on. Yes, I'd appreciate the help. I don't know where this is going wrong

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants