从 pandas 移植代码在 Polars 中不起作用,为什么?

问题描述 投票:0回答:1

我是第一次尝试极地,它似乎是一个替代品。但是,在很多地方都卡住了,例如下面的代码片段在 pandas 中效果很好,但在 Polars 中则不然

product_sold_out_mask = ((df_product_data['quantity_available'] == 0) & (df_product_data['reorder_threshold'] >= 0))

df_product_data.loc[product_sold_out_mask, 'sold_out_date'] = df_product_data['transaction_date'] - pd.to_timedelta(df_product_data['reorder_threshold'], unit='D')
df_product_data.loc[~product_sold_out_mask, 'sold_out_date'] = pd.NaT

df_product_data.loc[df_product_data['sold_price'] <= 0, 'sold_price'] = -999
df_product_data.loc[df_product_data['sold_price'] > 2.0 * df_product_data['current_price'], 'sold_price'] = df_product_data['current_price']

示例:

import polars as pl

data = {
    'transaction_date': ['2023-06-01', '2023-06-02', '2023-06-03'],
    'quantity_available': [0, 2, 1],
    'reorder_threshold': [5, 1, 3],
    'current_price': [10.0, 12.0, 8.0],
    'sold_price': [0.0, 15.0, 20.0],
}

df_product_data = pl.DataFrame(data)

product_sold_out_mask = (
    (df_product_data['quantity_available'] == 0)
    & (df_product_data['reorder_threshold'] >= 0)
)

df_product_data = df_product_data.with_columns(
    pl.when(product_sold_out_mask)
    .then(pl.date("transaction_date") - pl.date("reorder_threshold").cast(pl.Date))
    .otherwise(pl.lit(None))
    .alias("sold_out_date")
)

df_product_data = df_product_data.with_columns(
    pl.when(df_product_data['sold_price'] <= 0).then(-999)
    .when(df_product_data['sold_price'] > 2.0 * df_product_data['current_price']).then(df_product_data['current_price'])
    .otherwise(df_product_data['sold_price'])
    .alias('sold_price')
)
python python-polars
1个回答
1
投票

至于你的极坐标示例,

pl.date()
的使用是错误的。

您需要将

transaction_date
转换为可以使用
.str.to_date()

执行的日期
.with_columns(pl.col('transaction_date').str.to_date())

reorder_threshold
是天数,您可以使用
pl.duration(days=)
将其转换为可以减去的持续时间。

  • 你应该使用
    pl.col('name')
    而不是
    df_product_name['name']
  • .otherwise(None)
    是默认设置 - 您可以将其省略
data = {
    'transaction_date': ['2023-06-01', '2023-06-02', '2023-06-03'],
    'quantity_available': [0, 2, 1],
    'reorder_threshold': [5, 1, 3],
    'current_price': [10.0, 12.0, 8.0],
    'sold_price': [0.0, 15.0, 20.0],
}


product_sold_out_mask = pl.all_horizontal(
    pl.col('quantity_available') == 0,
    pl.col('reorder_threshold') >= 0
)

(pl.DataFrame(data)
   .with_columns(pl.col('transaction_date').str.to_date())
   .with_columns(
       pl.when(product_sold_out_mask)
         .then(pl.col('transaction_date') - pl.duration(days = 'reorder_threshold'))
         .alias('sold_out_date')
   )
   .with_columns(
       pl.when(pl.col('sold_price') <= 0)
         .then(-999)
         .when(pl.col('sold_price') > 2.0 * pl.col('current_price'))
         .then(pl.col('current_price'))
         .otherwise(pl.col('sold_price'))
         .alias('sold_price')
   )
)
shape: (3, 6)
┌──────────────────┬────────────────────┬───────────────────┬───────────────┬────────────┬───────────────┐
│ transaction_date ┆ quantity_available ┆ reorder_threshold ┆ current_price ┆ sold_price ┆ sold_out_date │
│ ---              ┆ ---                ┆ ---               ┆ ---           ┆ ---        ┆ ---           │
│ date             ┆ i64                ┆ i64               ┆ f64           ┆ f64        ┆ date          │
╞══════════════════╪════════════════════╪═══════════════════╪═══════════════╪════════════╪═══════════════╡
│ 2023-06-01       ┆ 0                  ┆ 5                 ┆ 10.0          ┆ -999.0     ┆ 2023-05-27    │
│ 2023-06-02       ┆ 2                  ┆ 1                 ┆ 12.0          ┆ 15.0       ┆ null          │
│ 2023-06-03       ┆ 1                  ┆ 3                 ┆ 8.0           ┆ 8.0        ┆ null          │
└──────────────────┴────────────────────┴───────────────────┴───────────────┴────────────┴───────────────┘
© www.soinside.com 2019 - 2024. All rights reserved.