{ "cells": [ { "cell_type": "markdown", "id": "641485c2-652f-43ce-9246-6f7a937e113f", "metadata": {}, "source": [ "# `scores` Pandas API\n", "While `scores` is primarily designed for use with `xarray` `Datasets/DataArrays`, some metrics can work out of the box with `pandas` while others can work with some wrapping code to convert the two types between each other.\n", "\n", "Therefore, to make it more clear what can be used with `pandas`, `scores` shows a `pandas` api point including only those methods which work with `pandas`. This API also defines type hints for Pandas rather than xarray.\n", "\n", "This notebook will be expanded to cover some more details in future, such as how to convert more complex examples to and from xarray should that be wanted. The API itself is also in an early stage, and so only has a small number of scores currently. This will be expanded based on community feedback or as additional use cases are encountered." ] }, { "cell_type": "code", "execution_count": 1, "id": "ab3026ed-b75e-4843-a5fb-34146ab1c144", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import scores\n", "from scores import sample_data" ] }, { "cell_type": "markdown", "id": "bb68512b-81e9-40c7-beb9-562162072d69", "metadata": {}, "source": [ "Retrieve some sample data with simple values. Fcst and Obs here are pandas.Series objects." ] }, { "cell_type": "code", "execution_count": 2, "id": "75e637fb-30a7-44c1-a762-69bc41a25537", "metadata": {}, "outputs": [], "source": [ "fcst = sample_data.simple_forecast_pandas()\n", "obs = sample_data.simple_observations_pandas()" ] }, { "cell_type": "code", "execution_count": 3, "id": "925ed84b-4934-4087-baab-c19e04b545ee", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 10\n", "1 10\n", "2 11\n", "3 13\n", "4 14\n", "5 17\n", "6 15\n", "7 14\n", "dtype: int64" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fcst" ] }, { "cell_type": "markdown", "id": "0b70a05f-3be1-43a5-afce-f9f8e96dee58", "metadata": {}, "source": [ "Available metrics:" ] }, { "cell_type": "code", "execution_count": 26, "id": "706e2ae2-936d-4945-8582-24eb260cfee7", "metadata": {}, "outputs": [], "source": [ "# Uncomment this to see help documentation on the module\n", "help(scores.pandas)" ] }, { "cell_type": "code", "execution_count": 4, "id": "382096b9-57ab-4b5d-ae19-770f17576585", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scores.pandas.continuous.mse(fcst, obs)" ] }, { "cell_type": "code", "execution_count": 5, "id": "041d3900-e815-4fc0-b7d1-b0dd32a23f92", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
fcstobs
01011
11011
21112
31314
41411
\n", "
" ], "text/plain": [ " fcst obs\n", "0 10 11\n", "1 10 11\n", "2 11 12\n", "3 13 14\n", "4 14 11" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Many times, data will be held in a data frame. These can be simply passed to `scores` as follows:\n", "df = pd.DataFrame({\"fcst\": fcst, \"obs\": obs})\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 6, "id": "f3a91e5b-b700-498a-875d-c13c34fba2ee", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scores.pandas.continuous.mse(df.fcst, df.obs)" ] }, { "cell_type": "code", "execution_count": null, "id": "25d189f2-4e95-4587-8c9a-382557f0d80e", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 5 }