{
  "id": 393797,
  "title": "A few naive questions regarding polars",
  "url": "/competitions/predict-student-performance-from-game-play/discussion/393797",
  "author_name": "",
  "post_date": "2023-03-10T19:08:53.233463300Z",
  "votes": 2,
  "comment_count": 2,
  "views": 0,
  "content": "<p>Hi, I'm trying to rewrite my code with polars, but it's not that easy(feeling I'm learning another type of language). Here are my questions.</p>\n<p>1 about NaN<br>\naggs = [<br>\n        *[pl.col(\"elapsed_time\").filter().apply(lambda x: x.max() - x.min()).<br>\n         alias(f\"\") for c in fqid],<br>\n]</p>\n<p>Here sometimes x.max() is Nonetype since filter() may return a null series. I want to fill with 0 in that case. I tried to use <br>\n'''lambda x: x.max() - x.min() if x.max() is not None else 0'''<br>\nlike this, but no idea how to make it correct since \"is not None\" doesn't work.</p>\n<p>2 how to apply my customized function</p>\n<p>for c in fqid:<br>\n    tmp = g[['fqid', \"index\", \"room_fqid\"]].apply(lambda x: calculate_time(x['fqid'].to_numpy(), x[\"room_fqid\"].to_numpy(), x[\"index\"].to_numpy(), c))</p>\n<p>I saw the apply function in polars does not supply for more than one column, maybe using struct instead. But I have no idea how to rewrite it and how to pass str parameters like c to my customized function.</p>",
  "messages": [
    {
      "id": "2176599",
      "postDate": "03/10/2023 19:08:53",
      "content": "<p>Hi, I'm trying to rewrite my code with polars, but it's not that easy(feeling I'm learning another type of language). Here are my questions.</p>\n<p>1 about NaN<br>\naggs = [<br>\n        *[pl.col(\"elapsed_time\").filter().apply(lambda x: x.max() - x.min()).<br>\n         alias(f\"\") for c in fqid],<br>\n]</p>\n<p>Here sometimes x.max() is Nonetype since filter() may return a null series. I want to fill with 0 in that case. I tried to use <br>\n'''lambda x: x.max() - x.min() if x.max() is not None else 0'''<br>\nlike this, but no idea how to make it correct since \"is not None\" doesn't work.</p>\n<p>2 how to apply my customized function</p>\n<p>for c in fqid:<br>\n    tmp = g[['fqid', \"index\", \"room_fqid\"]].apply(lambda x: calculate_time(x['fqid'].to_numpy(), x[\"room_fqid\"].to_numpy(), x[\"index\"].to_numpy(), c))</p>\n<p>I saw the apply function in polars does not supply for more than one column, maybe using struct instead. But I have no idea how to rewrite it and how to pass str parameters like c to my customized function.</p>",
      "rawMarkdown": "Hi, I'm trying to rewrite my code with polars, but it's not that easy(feeling I'm learning another type of language). Here are my questions.\n\n1 about NaN\naggs = [\n        *[pl.col(\"elapsed_time\").filter().apply(lambda x: x.max() - x.min()).\n         alias(f\"\") for c in fqid],\n]\n\nHere sometimes x.max() is Nonetype since filter() may return a null series. I want to fill with 0 in that case. I tried to use \n'''lambda x: x.max() - x.min() if x.max() is not None else 0'''\nlike this, but no idea how to make it correct since \"is not None\" doesn't work.\n\n2 how to apply my customized function\n\nfor c in fqid:\n    tmp = g[['fqid', \"index\", \"room_fqid\"]].apply(lambda x: calculate_time(x['fqid'].to_numpy(), x[\"room_fqid\"].to_numpy(), x[\"index\"].to_numpy(), c))\n\nI saw the apply function in polars does not supply for more than one column, maybe using struct instead. But I have no idea how to rewrite it and how to pass str parameters like c to my customized function.",
      "votes": null
    },
    {
      "id": "2178306",
      "postDate": "03/12/2023 10:06:37",
      "content": "<p>Hello , I think a solution for your problem is :</p>\n<ol>\n<li>to fillnull in polars just use : <code>.fill_null(value)</code></li>\n<li>to apply a customized function use (for example to multiply two cols)  : <code>pl.map([col1,col2],lambda x : x[0]*x[1] ).alias(col_name)</code></li>\n</ol>",
      "rawMarkdown": "Hello , I think a solution for your problem is :\n1. to fillnull in polars just use : `.fill_null(value)`\n2. to apply a customized function use (for example to multiply two cols)  : `pl.map([col1,col2],lambda x : x[0]*x[1] ).alias(col_name)`",
      "votes": null
    },
    {
      "id": "2178638",
      "postDate": "03/12/2023 15:24:46",
      "content": "<p>Thank you so much!</p>",
      "rawMarkdown": "Thank you so much!",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 2178306,
      "author_name": "ihebch",
      "author_url": "",
      "post_date": "03/12/2023 10:06:37",
      "content": "<p>Hello , I think a solution for your problem is :</p>\n<ol>\n<li>to fillnull in polars just use : <code>.fill_null(value)</code></li>\n<li>to apply a customized function use (for example to multiply two cols)  : <code>pl.map([col1,col2],lambda x : x[0]*x[1] ).alias(col_name)</code></li>\n</ol>",
      "votes": null,
      "replies": [
        {
          "id": 2178638,
          "author_name": "yunqicao",
          "author_url": "",
          "post_date": "03/12/2023 15:24:46",
          "content": "<p>Thank you so much!</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "2176599": "Hi, I'm trying to rewrite my code with polars, but it's not that easy(feeling I'm learning another type of language). Here are my questions.\n\n1 about NaN\naggs = [\n        *[pl.col(\"elapsed_time\").filter().apply(lambda x: x.max() - x.min()).\n         alias(f\"\") for c in fqid],\n]\n\nHere sometimes x.max() is Nonetype since filter() may return a null series. I want to fill with 0 in that case. I tried to use \n'''lambda x: x.max() - x.min() if x.max() is not None else 0'''\nlike this, but no idea how to make it correct since \"is not None\" doesn't work.\n\n2 how to apply my customized function\n\nfor c in fqid:\n    tmp = g[['fqid', \"index\", \"room_fqid\"]].apply(lambda x: calculate_time(x['fqid'].to_numpy(), x[\"room_fqid\"].to_numpy(), x[\"index\"].to_numpy(), c))\n\nI saw the apply function in polars does not supply for more than one column, maybe using struct instead. But I have no idea how to rewrite it and how to pass str parameters like c to my customized function.",
    "2178306": "Hello , I think a solution for your problem is :\n1. to fillnull in polars just use : `.fill_null(value)`\n2. to apply a customized function use (for example to multiply two cols)  : `pl.map([col1,col2],lambda x : x[0]*x[1] ).alias(col_name)`",
    "2178638": "Thank you so much!"
  },
  "source": "meta"
}