-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Cannot plot datetime.date dimension #8866
Comments
Why does the x-axis go from a dtype import xarray as xr
import numpy as np
import datetime
start = datetime.datetime(2024, 1,1)
time = [start + datetime.timedelta(hours=x) for x in range(720)]
data = xr.DataArray(np.random.randn(len(time)), coords=dict(time=('time', time)))
print(data.time.dtype) # datetime64[ns]
r = data.resample({"time":"1D"}).mean()
print(r.time.dtype) # datetime64[ns]
g = data.groupby('time.date').mean()
print(g.date.dtype) # object |
I am not sure how xarray handles lists of For date's that's not happening e.g. start = datetime.date(2024, 1,1)
time = [start + datetime.timedelta(days=x) for x in range(30)]
xr.DataArray(np.random.randn(len(time)), coords=dict(time=('time', time))).time.dtype also returns It would be nice if it was transformed to a |
FYI: #7493 |
Alright based on that issue, I gather that making it work with through As mentioned above, I believe just adding It calls |
From what I can tell, This happens at xarray/xarray/core/variable.py Lines 292 to 293 in 6af547c
Here it checks for xarray/xarray/core/variable.py Lines 197 to 219 in 6af547c
Q1: Should xarray convert Those questions are independent of each other. For Q2 I do not see something blocking this. |
In #8873 I made the suggested change and checked that it works as expected. I wonder whether its even necessary to have |
This is the intended return type of If you want a datetime64 array |
@dcherian this assumes that the original dtype is In any case, I think the proposed solution to enable plotting As I mentioned, earlier in case of groupby there is also the option to use |
If this is the intended dtype for this groupby operation then I think we move forward with your suggested solution. |
What happened?
I noticed that xarray doesnt support plotting when the x-axis is a
datetime.date
. In my case, I would like to plot hourly data aggregated by date. I know that in this particular case, I could just use.resample('1D')
to achieve the same result and be able to plot it but I am wondering whether xarray shouldn't just also support plotting dates.I am pretty sure that matplotlib supports date on the x-axis so maybe adding it to an acceptable type in plot/utils.py L675 in
_ensure_plottable
would already do the trick?I am happy to look into this if this is a wanted feature.
What did you expect to happen?
No response
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
No response
Environment
xarray: 2023.12.0
pandas: 2.1.4
numpy: 1.26.3
scipy: 1.12.0
netCDF4: 1.6.5
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: 2.16.1
cftime: 1.6.3
nc_time_axis: 1.4.1
iris: None
bottleneck: 1.3.7
dask: 2024.1.1
distributed: None
matplotlib: 3.8.2
cartopy: None
seaborn: None
numbagg: None
fsspec: 2023.12.2
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 69.1.0
pip: 24.0
conda: None
pytest: None
mypy: None
IPython: 8.21.0
sphinx: None
The text was updated successfully, but these errors were encountered: