Oct-17-2023, 02:07 PM
(This post was last modified: Oct-17-2023, 02:07 PM by deanhystad.)
I don't think concatenate will work. An odd combination of matching and replacing makes me think that there is not a vectorized solution. In pandas you could make a date intex and merge, but that adds a column instead of replacing values in an existing column. The code below does he matching and replacement in a single pass. It does require that the arrays are sorted by date.
import numpy as np # make two Nx3 arrays to work with. toc_n = np.array([[a, b, 0] for a in range(1492, 2023) for b in range(1, 366)]) toc = np.array([[a, b, 1] for a in range(1492, 2023) for b in range(1, 366, 3)]) # Combine year and day into a date value. If not sorted, sort arrays by # this value. Sorting is an expensive operation, but the numpy sort is # efficient compared to looping in python. toc_n = np.hstack((toc_n, (toc_n[:, 0]*365+toc_n[:, 1]).reshape(-1, 1))) toc = np.hstack((toc, (toc[:, 0]*365+toc[:, 1]).reshape(-1, 1))) # Loop through toc_n and toc, copying toc[2] to toc_n[2] when # the dates match. Since both arrays are sorted this can be # done in a single pass. try: toc_iter = iter(toc) t = next(toc_iter) for t_n in toc_n: while t[-1] < t_n[-1]: t = next(toc_iter) if t[-1] == t_n[-1]: t_n[2] = t[2] t = next(toc_iter) except StopIteration: pass