I am provided with the following DataFrame with panel data containing Dates, Stocks and some feature for each stock.
+----------+------+-------------------+--------------------+
| _c0|symbol| stockPrice| roic|
+----------+------+-------------------+--------------------+
|2000-12-01| MTB| 0.0| 0.0|
|2000-12-01| XEL| 0.5599999999999987| 0.03831091180429823|
|2001-03-01| ADBE|-1.8799999999999968| 0.16664239102772385|
|2001-03-01| ALL| 3.020000000000003|0.015843499622776227|
|2001-03-01| XEL| 5.199999999999998| 0.01718300080156956|
|2001-03-01| BMY| -6.020000000000002| 0.1386651323360184|
|2001-03-01| CAT| 2.9000000000000017|-0.01871476386801...|
|2001-03-01| MRK| -5.030000000000001| 0.09202762084118016|
|2001-03-01| MTB| 21.55| 0.4299814503261493|
|2001-03-01| PVH| 2.1500000000000004|-0.08486335480927014|
|2001-03-01| BBY| 4.75| 0.27611001898177634|
|2001-06-01| ADBE| 17.610000000000007|-0.02235471561675301|
|2001-06-01| BMY| 0.0| 0.1386651323360184|
|2001-06-01| ALK| 7.65| 0.03139856533358338|
|2001-06-01| CAT| 1.6799999999999995|0.002447684500232757|
|2001-06-01| HUM| 0.25|-0.05092018990508895|
|2001-06-01| ALL| -7.170000000000003|-0.01125850244811...|
|2001-06-01| EXPD| 2.74| 0.09279634515913428|
|2001-06-01| IDXX| -0.92| 0.09939743655211153|
|2001-06-01| BBY| 13.420000000000002|-0.21821519283350074|
+----------+------+-------------------+--------------------+
I would like to create a new PySpark dataframe containing signals defined as follows: if in 2000-12-01 stock price for XEL > 0, then populatate the next N days for stock XEL with 1, if negative with -1 and i want them to be additive (if in the next trading day price is positive I want to add the signal 1 on the next N days). I want to do this for all the columns.
How can I achieve such result with PySpark DataFrames without iterating through the rows?
An example of the output is the following:
+----------+------+-------------------+--------------------+
| _c0|symbol| stockPrice| roic|
+----------+------+-------------------+--------------------+
|2000-12-01| MTB| 0.0| 0.0|
|2000-12-01| XEL| 1| 1|
|2001-03-01| ADBE| -1| 1|
|2001-03-01| ALL| 1| 1|
|2001-03-01| XEL| 2| 2|
|2001-03-01| BMY| -1| 1|
|2001-03-01| CAT| 1| -1|
|2001-03-01| MRK| -1| 1|
|2001-03-01| MTB| 1| 1|
|2001-03-01| PVH| 1| -1|
|2001-03-01| BBY| 1| 1|
+----------+------+-------------------+--------------------+