如何将pandas函数重写为polars? 我对 np.expm1 有一些问题,在极坐标中没有这样的功能。
def add_elasticity_predictions(data: pd.DataFrame) -> pd.DataFrame:
data["huber_elasticity"] = np.expm1(data["intercept"] + data["huber_elasticity_coefficient"] *
np.log1p(data["discount_price"]))
return data
例如
import polars as pl
data = pl.DataFrame({"elasticity_intercept": [-6.195475, -8.437139, -4.741667],
"elasticity_coefficient": [1.553659, 2.048057, 1.285653],
"discount_price": [126, 129, 115],
})
输出应为 [2.78384188, 3.62672375, 2.93456563]
来自 https://github.com/pola-rs/polars/issues/12891#issuecomment-1841872969:
numpy 函数https://numpy.org/doc/stable/reference/ generated/numpy.expm1.html#numpy.expm1
是一个ufunc,所以你可以直接使用它例如
df.with_columns(np.expm1(pl.col('values')))