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

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 +9054
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?

# widths must be timedelta-like.
if any(isinstance(p, (datetime.datetime, datetime.date))
for p in positions):
_widths = widths if not np.isscalar(widths) else [widths] * N
Copy link
Member

Choose a reason for hiding this comment

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

This is only used to do the instance check. We don't need to do this N times on the the same instance.

Suggested change
_widths = widths if not np.isscalar(widths) else [widths] * N
_widths = widths if not np.isscalar(widths) else [widths]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants