{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "#"
      ],
      "id": "f2f4434f-b0a1-4dd2-b531-cc392d5effb4"
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {},
      "outputs": [],
      "source": [
        "import polars as pl\n",
        "import scipy as sp\n",
        "import numpy as np"
      ],
      "id": "a74b7e9a-88e4-4cfb-b950-54b52e6f6756"
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {},
      "outputs": [],
      "source": [
        "df = pl.DataFrame({\n",
        "    \"species\": [\"A\", \"B\", \"C\"],\n",
        "    \"N\": [57, 72, 53]\n",
        "})"
      ],
      "id": "96e0f621-13a8-43bb-a4f6-6cca49ac4c8d"
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/html": [
              "</div>"
            ]
          }
        }
      ],
      "source": [
        "df"
      ],
      "id": "99049d68-708d-4b3a-b974-31f7f5368eaf"
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {},
      "outputs": [],
      "source": [
        "def tdf(N):\n",
        "    return sp.stats.t(df = N - 1).ppf(0.975)"
      ],
      "id": "962def45-3c56-46a9-833f-20e06b4b753a"
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "np.float64(2.003240718847872)"
            ]
          }
        }
      ],
      "source": [
        "tdf(57)"
      ],
      "id": "9af3cb2e-b0bf-4970-9eaa-b553b50a76a9"
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/html": [
              "</div>"
            ]
          }
        }
      ],
      "source": [
        "(df\n",
        "    .with_columns(pl.col(\"N\").map_batches(tdf).alias(\"t\"))\n",
        "    .with_columns((pl.col(\"t\") + 1).alias(\"other\"))\n",
        ")"
      ],
      "id": "4645b0be-19f8-49ac-a49c-56973939c7bd"
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {},
      "outputs": [
        {
          "output_type": "display_data",
          "metadata": {},
          "data": {
            "text/plain": [
              "array([2.00404478, 2.00324072, 2.00171748])"
            ]
          }
        }
      ],
      "source": [
        "tdf(np.array([56, 57, 59]))"
      ],
      "id": "f43ec597-15bb-4061-af2d-7213e71fb559"
    }
  ],
  "nbformat": 4,
  "nbformat_minor": 5,
  "metadata": {
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3 (ipykernel)",
      "language": "python"
    },
    "language_info": {
      "name": "python",
      "codemirror_mode": {
        "name": "ipython",
        "version": "3"
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.12"
    }
  }
}