Merging two tensors based on time series

Hi everyone,

I have two tensors:

A -> (128,19,3,99,99) #(batch, date, data, data, data)
B -> (128,9,223)  #(batch, date, data)

After encoding I will have something like that:

A2 -> (128,19,10) # (batch, date, encoded data with CNN_1)
B2 -> (128,9,10) # (batch, date, encoded data with CNN_2)
C -> (128,28,10) # merge of A2 and B2

I have the records of time-series of each tensor (A,B),
I want to be able to merge them on date axis to a single tensor.

Here is an example of the time series of tensor A:
['2021-12-13', '2021-12-28', '2022-01-02', '2022-01-10', '2022-01-20', '2022-01-26', '2022-02-06', '2022-02-14', '2022-02-22', '2022-03-02', '2022-03-17', '2022-03-21', '2022-03-27', '2022-03-30', ...]
Tensor B has different dates that do not necessarily fall on the exact dates of tensor A.

So at the end, the axis of date in tensor C will be something like:
A2,A2,B2,A2,B2,A2,B2,B2 depends on the original order of dates.

Any help will be great!

You could concatenate both tensors with their dates to a single (unsorted) one, create the sorted indices via e.g.:

date = ['2021-12-13', '2022-01-02', '2022-02-14', '2021-12-28', '2022-02-06', '2022-01-10', '2022-01-20', '2022-01-26', '2022-03-17', '2022-02-22', '2022-03-02', '2022-03-21', '2022-03-27', '2022-03-30']
indices = sorted(range(len(date)), key=lambda index: datetime.strptime(date[index], "%Y-%m-%d"))
print(indices)
# [0, 3, 1, 5, 6, 7, 4, 2, 9, 10, 8, 11, 12, 13]

and sort the concatenated tensors again.
As you might imagine, this approach would create some overhead in memory, so depending on your use case you might prefer a (slower) loop approach to avoid creating copies of the data.

Thank you!
I keep the order of data so I can just make the sort on a single vector and then use it after the manipulations on datasets to restore desired order…