mwaskom / seaborn

Statistical data visualization in Python
https://seaborn.pydata.org
BSD 3-Clause "New" or "Revised" License
12.63k stars 1.93k forks source link

jointplot with kind="hex" fails with datetime64[ns] #3664

Open fynnkroeger opened 8 months ago

fynnkroeger commented 8 months ago

Minimal example:

import seaborn as sns
import numpy as np
dates = np.array(["2023-01-01", "2023-01-02", "2023-01-03"], dtype="datetime64[ns]")
sns.jointplot(x=dates, y=[1, 2, 3], kind="hex")

Error:

Traceback (most recent call last):
  File "/.../seaborn_bug.py", line 21, in <module>
    sns.jointplot(x=dates, y=[1, 2, 3], kind="hex")
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/seaborn/axisgrid.py", line 2307, in jointplot
    x_bins = min(_freedman_diaconis_bins(grid.x), 50)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/seaborn/distributions.py", line 2381, in _freedman_diaconis_bins
    iqr = np.subtract.reduce(np.nanpercentile(a, [75, 25]))
TypeError: the resolved dtypes are not compatible with subtract.reduce. Resolved (dtype('<M8[ns]'), dtype('<M8[ns]'), dtype('<m8[ns]'))

I think this should work, as datetime64[ns] is the default type for datetimes in pandas. It works when I omit kind="hex" or use kind="kde". The error was in version 0.13.2. In 0.11.2 I got a error in the same cases but it was a integer overflow with numpy during conversions.

athompson1991 commented 4 months ago

Hi, I'm not especially experienced in open source development, I think would count as my first contribution, but I just opened a pull request on this. The initial bug was easy enough to sort out, but there were some downstream complications I describe in the pull request. Let me know if this is an appropriate fix, glad to help as best I can.