我有以下代码,可以正常工作.
import numpy as np
import polars as pl
data = {
"date": ["2021-01-01", "2021-01-02", "2021-01-03", "2021-01-04", "2021-01-05", "2021-01-06", "2021-01-07", "2021-01-08", "2021-01-09", "2021-01-10", "2021-01-11", "2021-01-12", "2021-01-13", "2021-01-14", "2021-01-15", "2021-01-16", "2021-01-17", "2021-01-18", "2021-01-19", "2021-01-20"],
"close": np.random.randint(100, 110, 10).tolist() + np.random.randint(200, 210, 10).tolist(),
"company": ["A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B"]
}
df = pl.DataFrame(data).with_columns(date = pl.col("date").cast(pl.Date))
# Calculate Returns
R = pl.col("close").pct_change()
# Calculate Gains and Losses
G = pl.when(R > 0).then(R).otherwise(0).alias("gain")
L = pl.when(R < 0).then(R).otherwise(0).alias("loss")
# Calculate Moving Averages for Gains and Losses
window = 3
MA_G = G.rolling_mean(window).alias("MA_gain")
MA_L = L.rolling_mean(window).alias("MA_loss")
# Calculate Relative Strength Index based on Moving Averages
RSI = (100 - (100 / (1 + MA_G / MA_L))).alias("RSI")
df = df.with_columns(R, G, L, MA_G, MA_L, RSI)
df.head()
我喜欢使用polars
组成不同步骤的能力,因为它保持了代码的可读性和易维护性(与方法链接相反).请注意,归根结底,计算更加复杂.
然而,现在我想计算上面的列,但按"公司"分组.我试着在相关的地方加了.over("company")
.然而,这并不管用.
# Calculate Returns
R = pl.col("close").pct_change().over("company")
# Calculate Gains and Losses
G = pl.when(R > 0).then(R).otherwise(0).alias("gain")
L = pl.when(R < 0).then(R).otherwise(0).alias("loss")
# Calculate Moving Averages for Gains and Losses
window = 3
MA_G = G.rolling_mean(window).alias("MA_gain").over("company")
MA_L = L.rolling_mean(window).alias("MA_loss").over("company")
# Calculate Relative Strength Index based on Moving Averages
RSI = (100 - (100 / (1 + MA_G / MA_L))).over("company").alias("RSI")
df = df.with_columns(R, G, L, MA_G, MA_L, RSI)
df.head()
Questions
1.)在保持上述代码方法的同时,修复此"window expression not allowed in aggregation"
错误的最佳方法是什么?
2.)相关问题:为什么聚合中不允许使用窗口表达式?从技术Angular 来看,这有什么问题?有人能用通俗易懂的语言给我解释一下吗?
谢谢!