'Updating xarray dimension values with numpy datetime array not working with netCDF file
An example ds:
x = xr.Dataset(
{
"temperature_c": (
("lat", "lon", "date"),
20 * np.random.rand(8).reshape(2, 2, 2),
),
},
coords={"lat": [10, 20], "lon": [150, 160], "date": [1, 2]},
)
>>> x
>>> <xarray.Dataset>
Dimensions: (lat: 2, lon: 2, date: 2)
Coordinates:
* lat (lat) int32 10 20
* lon (lon) int32 150 160
* date (date) int32 1 2
Data variables:
temperature_c (lat, lon) float64 10.98 14.3 12.06 10.9
And a numpy datetime array:
import numpy as np
times = np.array(['2007-07-13', '2006-01-13'], dtype='datetime64')
Using x.update({"date" : times})
works fine, and results in what I want.
>>> <xarray.Dataset>
Dimensions: (lat: 2, lon: 2, date: 2)
Coordinates:
* lat (lat) int32 10 20
* lon (lon) int32 150 160
* date (date) datetime64[ns] 2007-07-13, 2006-01-13
Data variables:
temperature_c (lat, lon) float64 10.98 14.3 12.06 10.9
In my case, I have read in a netcdf file as an xarray, with similar dimensions except bigger. My dataset looks like the following:
>>> ds
>>> <xarray.Dataset>
Dimensions: longitude: 720 latitude: 361 time: 504
Coordinates:
longitude (longitude) float32 -179.5 -179.0 ... 179.5 180.0
latitude (latitude) float32 -90.0 -89.5 -89.0 ... 89.5 90.0
time (time) float64 0 1 2 3 4 5 ... 499 500 501 502 503
And when I try to do the same thing, updating the time coordinates using the ds.update({"time": times})
it results in the following xarray.Dataset:
>>> ds
>>> <xarray.Dataset>
Dimensions: dim_0: 504 longitude: 720 latitude: 361 time: 504
Coordinates:
time (dim_0) datetime64[ns] 1979-01-15 ... 2020-12-15
longitude (longitude) float32 -179.5 -179.0 ... 179.5 180.0
latitude (latitude) float32 -90.0 -89.5 -89.0 ... 89.5 90.0
dim_0 (dim_0) int64 0 1 2 3 4 5 ... 499 500 501 502 503
What is the difference here? Why doesn't updating the dimension work when using a loaded in netCDF dataset? I feel like it has to do that there suddenly is a dim_0
dimension added, but this didn't happen before.
It's not usable like this, as the xarray.Dataset still uses the dim_0
as the dimension values. When I try to drop the dimension and assign the time as the new main one, this doesn't work and removes all the data variables. Swapping the dimension names doesn't work as well, as it doesn't recognize dim_0
as a valid dimension name.
The source dataset can be found here. It is 500mb.
Solution 1:[1]
It was very simple, there was no need for any methods in the xarray dataset as these all didn't work:
ds["time"] = ("time", times)
Did the trick
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | B.Quaink |