Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

da.plot.pcolormesh fails when there is a datetime coordinate #1661

Closed
gerritholl opened this issue Oct 25, 2017 · 9 comments · Fixed by #1669
Closed

da.plot.pcolormesh fails when there is a datetime coordinate #1661

gerritholl opened this issue Oct 25, 2017 · 9 comments · Fixed by #1669
Labels

Comments

@gerritholl
Copy link
Contributor

da.plot.pcolormesh, where da is a DataArray, fails with TypeError: invalid type promotion when one of the coordinates is a datetime array:

$ cat mwe.py
#!/usr/bin/env python3.6

import xarray
import numpy

da = xarray.DataArray(
    numpy.arange(3*4).reshape(3,4),
    dims=("x", "y"),
    coords={"x": [1,2,3],
            "y": [numpy.datetime64(f"2000-01-{x:02d}")
                  for x in range(1, 5)]})

da.plot.pcolormesh()
$ ./mwe.py
Traceback (most recent call last):
  File "./mwe.py", line 13, in <module>
    da.plot.pcolormesh()
  File "/dev/shm/gerrit/venv/stable-3.6/lib/python3.6/site-packages/xarray/plot/plot.py", line 547, in plotmethod
    return newplotfunc(**allargs)
  File "/dev/shm/gerrit/venv/stable-3.6/lib/python3.6/site-packages/xarray/plot/plot.py", line 500, in newplotfunc
    **kwargs)
  File "/dev/shm/gerrit/venv/stable-3.6/lib/python3.6/site-packages/xarray/plot/plot.py", line 667, in pcolormesh
    primitive = ax.pcolormesh(x, y, z, **kwargs)
  File "/dev/shm/gerrit/venv/stable-3.6/lib/python3.6/site-packages/matplotlib/__init__.py", line 1710, in inner
    return func(ax, *args, **kwargs)
  File "/dev/shm/gerrit/venv/stable-3.6/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 5636, in pcolormesh
    coords = np.column_stack((X, Y)).astype(float, copy=False)
  File "/dev/shm/gerrit/venv/stable-3.6/lib/python3.6/site-packages/numpy/lib/shape_base.py", line 353, in column_stack
    return _nx.concatenate(arrays, 1)
TypeError: invalid type promotion
@gerritholl
Copy link
Contributor Author

I'm quite sure it worked in the past, but trying old versions of xarray yields the same error, so either my memory is wrong, or this started failing due to changes in dependencies.

@gerritholl
Copy link
Contributor Author

Never mind, this is in matplotlib. See matplotlib/matplotlib#9577.

@gerritholl
Copy link
Contributor Author

Actually, it isn't in matplotlib really. It's xarrays responsibility after all. To plot with pcolormesh, one needs to convert the date axis using datenum, see https://stackoverflow.com/a/27918586/974555 . When plotting with xarray, that is out of control of the user, so there must be some step within xarray to prepare this. What I still don't know is why my code (not this MWE, but my actual code) worked several months ago but not now.

@gerritholl gerritholl reopened this Oct 25, 2017
@gerritholl
Copy link
Contributor Author

The problem is triggered by a recent change in pandas. I'm currently bisecting pandas to see where it is but it's a little slow due to the compilation at every step.

@gerritholl
Copy link
Contributor Author

This happens after: pandas-dev/pandas@2310faa

@TomAugspurger
Copy link
Contributor

Pandas used to register a matplotlib converter for datetimes on import. I’ll take a closer look in a bit.

@gerritholl
Copy link
Contributor Author

@TomAugspurger Is it this one?

@TomAugspurger
Copy link
Contributor

Yep, that was the change.

The fix is to explicitly register the converters before plotting:

from pandas.tseries import converter
converter.register()

@shoyer
Copy link
Member

shoyer commented Oct 28, 2017

Thanks @gerritholl and @TomAugspurger for your help figuring this out.

See #1669 for a fix.

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

Successfully merging a pull request may close this issue.

3 participants