I would like to merge two data frames based upon both a time period, and lat lon coordinates.
I originally performed an outer product to construct distances between the two data-frames using a window function. However this created an enormous data explosion, and shut down my cluster whenever I tried to run it (I can include this code if requested). In response, I decided to perform a sequence of inner joins in order to avoid this outer product. Put simply, I joined on the absolute value of the difference being equal to some specific value until the remaining un-matched coordinates could be merged using the naive window approach. I have looked around but not found any Pyspark code in the stack that has explicitly dealt with this problem so any help is appreciated.
# starting with exact
conditions = [df1.lon1 == df2.lon2,
df1.lat1 == df2.lat2,
df2.start <= df1.time,
df2.end >= df1.time]
current_df_hit = df1.join(df2, conditions, 'inner')
....
# then 1 off
conditions = [F.abs(df1.lon1 - df2.lon2) == 1,
df1.lat1 == df2.lat2,
df2.start <= df1.time,
df2.end >= df1.time]
current_df_hit = df1.join(df2, conditions, 'inner')
...
conditions = [df1.lon1 == df2.lon2,
F.abs(df1.lat1 - df2.lat2==1,
df2.start <= df1.time,
df2.end >= df1.time]
current_df_hit = df1.join(df2, conditions, 'inner')
...
# then both ect.
conditions = [F.abs(df1.lon1 - df2.lon2) == 1,
F.abs(df1.lat1 - df2.lat2==1,
df2.start <= df1.time,
df2.end >= df1.time]
current_df_hit = df1.join(df2, conditions, 'inner')
this does not produce the expected results. For example, run the following:
df0 = spark.createDataFrame([
('id1', 10.1, 12.1),
('id5', 13.1, 13.1)], ["id0", "lat0", "lon0"])
df1 = spark.createDataFrame([
('id1', 10.1, 12.3),
('id5', 13.1, 13.2)], ["id1", "lat1", "lon1"])
# this produces nothing
df0.join(df1, F.abs(df1.lon1 - df0.lon0) == 0.1, 'inner').show()
# and so does this
df0.join(df1, F.round(df1.lon1 - df0.lon0, 1) == 0.1).show()```
which produces nothing. Please advise, and thanks in advance!