python_user111
python_user111

Reputation: 37

Combine two time series dataarray

I am working on 2 dataarrays with time, latitude and longitude dimensions.

Data1 looks like:

print (data1)
 <xarray.DataArray (lon: 20, lat: 40, time: 2880)>
  array([[[6.02970212, 4.49268718, 2.47512044, ..., 7.09662201,
              0.34438006, 0.664115  ]]])
 Coordinates:
     * lon      (lon) int32 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
     * lat      (lat) int32 0 1 2 3 4 5 6 7 8 9 ... 30 31 32 33 34 35 36 37 38 39
     * time     (time) datetime64[ns] 2017-06-01 ... 2017-07-30T23:30:00

Data2 looks like:

print (data2)
<xarray.DataArray (lon: 20, lat: 40, time: 2880)>
array([[[1.60607837, 3.07589422, 6.26158588, ..., 6.95746878,
     0.51368952, 1.45280591]]])
 Coordinates:
     * lon      (lon) int32 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
     * lat      (lat) int32 0 1 2 3 4 5 6 7 8 9 ... 30 31 32 33 34 35 36 37 38 39
     * time     (time) datetime64[ns] 2017-08-01 ... 2017-09-29T23:30:00

The "lon" and "lat" dimensions are similar in both data arrays. Which is not the case for the "time" dimension. I want to create a new data array that combine both data1 and data2. So the new data array (data3) will look like:

print(data3)
<xarray.DataArray (lon: 20, lat: 40, time: 5808)>
array([[[4.82000138, 8.13537618, 2.39793625, ..., 2.03778308,
     4.13311001, 5.57075556]]])
 Coordinates:
    * lon      (lon) int32 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
    * lat      (lat) int32 0 1 2 3 4 5 6 7 8 9 ... 30 31 32 33 34 35 36 37 38 39
    * time     (time) datetime64[ns] 2017-06-01 ... 2017-09-29T23:30:00

Any idea to do so?

This is a code to regenerate data1 and data2:

from datetime import timedelta

import xarray as xr
import numpy as np

precipitation = 10 * np.random.rand(20, 40, 2880)
lon = range(20)
lat = range(40)
time1 = np.arange('2017-06-01', '2017-07-31', 
              timedelta(minutes=30),dtype='datetime64[ns]')
time2 = np.arange('2017-08-01', '2017-09-30', 
              timedelta(minutes=30),dtype='datetime64[ns]')
data1 =xr.DataArray(
data=precipitation,
dims=["lon","lat","time"],
coords=[lon,lat,time1]          
        )
print (data1)

data2 =xr.DataArray(
data=precipitation,
dims=["lon","lat","time"],
coords=[lon,lat,time2]          
        )
print (data2)

Upvotes: 0

Views: 1244

Answers (2)

Vincent B&#233;net
Vincent B&#233;net

Reputation: 1314

Here the snippet you need:

time3 = np.concatenate((time1, time2), dtype='datetime64[ns]')
data3 = xr.DataArray(
    data=10 * np.random.rand(20, 40, len(time3)),
    dims=["lon", "lat", "time"],
    coords=[lon, lat, time3]          
)
print (data3)

This is my output:

<xarray.DataArray (lon: 20, lat: 40, time: 5760)>
Coordinates:
  * lon      (lon) int32 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
  * lat      (lat) int32 0 1 2 3 4 5 6 7 8 9 ... 30 31 32 33 34 35 36 37 38 39
  * time     (time) datetime64[ns] 2017-06-01 ... 2017-09-29T23:30:00

Upvotes: 1

Val
Val

Reputation: 7023

If you want to stack your data arrays along the time dimension, you can simply do

data3 = xr.concat([data1, data2], dim="time")

Upvotes: 4

Related Questions