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

Improve the way NetCDFTimeConverter.convert handles scalars #45

Closed
spencerkclark opened this issue Jul 26, 2020 · 2 comments · Fixed by #46
Closed

Improve the way NetCDFTimeConverter.convert handles scalars #45

spencerkclark opened this issue Jul 26, 2020 · 2 comments · Fixed by #46
Assignees
Milestone

Comments

@spencerkclark
Copy link
Member

Currently NetCDFTimeConverter.convert promotes scalar CalendarDateTime or cftime.datetime objects to arrays of floats:

>>> import cftime
>>> from nc_time_axis import NetCDFTimeConverter
>>> NetCDFTimeConverter.convert(cftime.DatetimeNoLeap(2010, 1, 1), None, None)
array([3650.])

Within matplotlib, this can lead to unusual assignment behavior, which will eventually be deprecated in NumPy (numpy/numpy#16943):

$ python -Wd
>>> import cftime
>>> import matplotlib.pyplot as plt
>>> import nc_time_axis
>>> times = [cftime.DatetimeNoLeap(2000, 1, 1), cftime.DatetimeNoLeap(2000, 1, 2), cftime.DatetimeNoLeap(2000, 1, 3)]
>>> data = range(3)
>>> plt.plot(times, data)
.../matplotlib/transforms.py:943: DeprecationWarning: setting an array element with a sequence. This was supported in some cases where the elements are arrays with a single element. For example `np.array([1, np.array([2])], dtype=int)`. In the future this will raise the same ValueError as `np.array([1, [2]], dtype=int)`.
  self._points[:, 0] = interval
.../nc_time_axis/__init__.py:266: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. Use `object` by itself, which is identical in behavior, to silence this warning. If you specifically wanted the numpy scalar type, use `np.object_` here.
  if value.dtype != np.object:
[<matplotlib.lines.Line2D object at 0x7fc550278c40>]

In the above example, self._points is np.array([[0., 0.], [1., 1.]]) and interval is (np.array([0.]), np.array([3650.])). If the converter handled scalars without promoting them to arrays, interval would be (0., 3650.), and the assignment would proceed without warning.

xref: pydata/xarray#4265

@bjlittle
Copy link
Member

bjlittle commented Jun 7, 2021

@spencerkclark Is this still a live issue that we want to pull into v1.3.0 ?

@spencerkclark
Copy link
Member Author

Yes, I think this would be good to fix in v1.3.0; my PR in #46 should address it.

@bjlittle bjlittle added this to the v1.3.0 milestone Jun 8, 2021
@rcomer rcomer linked a pull request Jun 8, 2021 that will close this issue
@bjlittle bjlittle self-assigned this Jun 10, 2021
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 a pull request may close this issue.

2 participants