I'm fairly new to Spark and I have a DF that stacks 8000 time series on top of each other. These time series make more sense as individual columns so I decided to filter them one by one and join them on the timestamp in a new master DF.
So far so good, however as soon as I try that I get a OOM error.
# My stacked DF and the name of the columns
stocks_df.cache()
tickers = ["TS1", "TS2", "TS3", ...]
def filter_for_symbol(ticker):
security = stocks_df.filter(stocks_df["Symbol"] == ticker).drop("Symbol")
# Avoids conflict in column names...
# for f in features:
# security = security.withColumnRenamed(f, ticker + "_" + f)
return security
# Aggregation Loop
acc_df = filter_for_symbol(tickers[0])
for ticker in tickers[1:]:
security = filter_for_symbol(ticker)
acc_df = acc_df.join(security, on="Date", how="outer")
What would be a better way of doing what I'm doing in terms of speed and/or memory management?
UPDATE
What I have, stocks_df: dim = (8000 time series * 3125 entries) x 9 features
+-------------+--------+------+-----+------+
| Date | Symbol | Open | ... | Close|
+-------------+--------+------+-----+------+
| 2020/01/01 | TS1 | 10.0 | ... | 12.2 |
| 2020/01/02 | TS1 | 12.2 | ... | 13.2 |
| ... | ... | ... | ... | ... |
+-------------+--------+------+-----+------+
| 2020/01/01 | TS2 | 29.9 | ... | 32.2 |
| 2020/01/02 | TS2 | 32.2 | ... | 19.2 |
| ... | ... | ... | ... | ... |
+-------------+--------+------+-----+------+
| ... | ... | ... | ... | ... |
+-------------+--------+------+-----+------+
What I would like to get: dim = 3125 x (8000 * 9)
+-------------+----------+-------+-----------+-----------+--------+
| Date | TS1_Open : ... : TS1_Close | TS2_Close | TS2_..|
+-------------+----------+-------+-----------+-----------+--------+
| 2020/01/01 | 10.0 : ... : 12.2 | 12.2 : ... |
| 2020/01/02 | 12.2 : ... : 13.2 | 13.2 : ... |
| ... | ... : ... : ... | ... : ... |
+-------------+----------+-------+-----------+-----------+--------+
What I get instead: java.lang.OOM