Multiplying data arrays of same dimensions fails #7915
-
What is your issue?Hi all, I am trying to multiply two data arrays of same dimensions: print(data1)
<xarray.DataArray 'var' (lat: 2160, lon: 4320)>
[9331200 values with dtype=int8]
Coordinates:
* lon (lon) float64 -180.0 -179.9 -179.8 -179.7 ... 179.8 179.9 180.0
* lat (lat) float64 89.96 89.88 89.79 89.71 ... -89.79 -89.88 -89.96
print(data2)
<xarray.DataArray 'var' (lat: 2160, lon: 4320)>
[9331200 values with dtype=float32]
Coordinates:
* lon (lon) float64 -180.0 -179.9 -179.8 -179.7 ... 179.8 179.9 180.0
* lat (lat) float64 89.96 89.88 89.79 89.71 ... -89.79 -89.87 -89.96
Note that following this thread, I made sure to have consistent dimensions and re-indexed both data arrays. Since both arrays have different On the other hand, this returned an empty array: data3 = data1.astype(np.float64) * data2.astype(np.float64)
print(data3)
<xarray.DataArray 'var' (lat: 0, lon: 0)>
array([], shape=(0, 0), dtype=float64)
Coordinates:
* lon (lon) float64
* lat (lat) float64 The only way I found to achieve this multiplication was to get the underlying np data: data3 = data1.data * data2.data Although this works for my need, I am still curious to understand why the pure xarray method fails. Can anyone inform me or point me towards a part of the documentation I might have missed? Thanks loads |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
Thanks for opening your first issue here at xarray! Be sure to follow the issue template! |
Beta Was this translation helpful? Give feedback.
-
@e5k, a minor discrepancy in the coordinate values might exist, even though the dimensions and the coordinate labels are the same for both import xarray as xr
xr.testing.assert_equal(data1.lon, data2.lon)
xr.testing.assert_equal(data1.lat, data2.lat) or xr.testing.assert_allclose(data1.lon, data2.lon)
xr.testing.assert_allclose(data1.lat, data2.lat)
this worked because numpy is less strict about matching coordinates compared to xarray. it simply multiplies the arrays based on their shape without checking if the coordinates match (or performing any coordinate alignment) |
Beta Was this translation helpful? Give feedback.
-
This one is a mystery to me. Can you create a minimal example? |
Beta Was this translation helpful? Give feedback.
-
@andersy005 - you were right! Sorry for not checking that, thanks for your help. @dcherian - in case you are still interested: data1 data2 Thanks both! |
Beta Was this translation helpful? Give feedback.
-
It is a very cryptic error, would been nice if it could've been made clearer. |
Beta Was this translation helpful? Give feedback.
@e5k,
a minor discrepancy in the coordinate values might exist, even though the dimensions and the coordinate labels are the same for both
DataArray
objects. this might happen due to a slight difference in the floating point precision for the latitude or longitude values indata1
anddata2
. you might want to inspect the coordinate values of both arrays and see if there is any such discrepancy.or