I have a set of 1000 (2D) pd.Dataframe
(lets say, index:time, columns: run_id) and each one of them has 3 properties (let's say temperature, pressure, location). Ideally I would like to have everything in a xr.DataArray
with 5 dimensions (or a xr.Dataset
with 4 dimensions and having the last dimension as unique data variables).
I created one DataArray with two dims and 2+3 coords but then the xr.concat
doesnt seem to work for multiple dimensions. (I followed the approach mentioned here Add 'constant' dimension to xarray Dataset. )
Example: I build DataArrays from individual dataframes and list of properties.
# Mock data:
data = {}
for i in np.arange(500):
data[i] = pd.DataFrame(np.random.randn(1000, 8), index=pd.DatetimeIndex(start='01.01.2013',periods=1000,freq='h'),
columns=list('ABCDEFGH'))
df_catalogue = pd.DataFrame(np.random.choice(10,(500, 3)), columns=['temp','pre','zon'])
#Build DataArrays adding scalar coords
res_da = []
for i,v in df_catalogue.iterrows():
i_df = data[i] # data is a dictionary of properly indexed dataframes
da = xr.DataArray(i_df.values,
coords={'time':i_df.index.values,'runs':i_df.columns.values,
'temp':v['temp'], 'pre':v['pre'],'zon':v['zon']},
dims=['time','runs'])
res_da.append(da)
But when I try all_da = xr.concat(res_da, dim=['temp','pre','zon'])
I get strange results. What is the best way to achieve something like this:
<xarray.DataArray (time: 8000, runs: 50, temp:8, pre:10, zon: 5)>
array([[[ 4545.453613, 4545.453613, ..., 4545.453613, 4545.453613],
[ 4545.453613, 4545.453613, ..., 4545.453613, 4545.453613],
...,
[ 4177.425781, 4177.425781, ..., 4177.425781, 4177.425781]]], dtype=float32)
Coordinates:
* runs (runs) object 'A' 'B' ...
* time (time) datetime64[ns] 2013-12-31T23:00:00 2014-01-01 ...
* zon (zon) 'zon1', 'zon2', 'zon3', ......
* temp (temp) 'XX' 'YY', 'ZZ' .....
* pre (pre) 'AAA', 'BBB', 'CCC' ....