Skip to content

Commit

Permalink
feat (accessors): Import accessors implicitly (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
coroa authored Dec 12, 2023
1 parent 01e570e commit 82b57d9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 39 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
Changelog
=========

* Fix type hints of function :func:`~core.aggregatelevel`
* Switch from black to ruff for formatting and update pre-commit versions
v0.4.0 (2023-12-12)
------------------------------------------------------------
* **BREAKING** :mod:`accessors` is imported implicitly. User code does not need to
import it any longer :pull:`45`
* Fix type hints of function :func:`~core.aggregatelevel` :pull:`44`
* Switch from black to ruff for formatting and update pre-commit versions :pull:`43`

v0.3.1 (2023-09-18)
------------------------------------------------------------
Expand Down
109 changes: 75 additions & 34 deletions docs/notebooks/introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@
"source": [
"from pandas_indexing.datasets import remindhighre_power\n",
"\n",
"\n",
"df = remindhighre_power()\n",
"df.head()"
]
Expand All @@ -371,7 +372,6 @@
"2. convenience accessors that are hooking into pandas as extensions, like\n",
" \n",
" ```python\n",
" import pandas_indexing.accessors\n",
" df.pix.assign(unit=\"Mt CO2e/yr)\n",
" ```\n",
"\n",
Expand Down Expand Up @@ -403,6 +403,7 @@
"source": [
"from pandas_indexing.core import describelevel\n",
"\n",
"\n",
"describelevel(df) # or: df.idx"
]
},
Expand Down Expand Up @@ -432,8 +433,6 @@
}
],
"source": [
"import pandas_indexing.accessors\n",
"\n",
"df.idx"
]
},
Expand Down Expand Up @@ -468,7 +467,7 @@
"metadata": {},
"outputs": [],
"source": [
"from pandas_indexing import ismatch, isin # no .idx equivalents"
"from pandas_indexing import isin, ismatch # no .idx equivalents"
]
},
{
Expand Down Expand Up @@ -1518,6 +1517,7 @@
"source": [
"from pandas_indexing import extractlevel, formatlevel\n",
"\n",
"\n",
"splitdf = extractlevel(df, variable=\"Secondary Energy|{carrier}|{fuel}\", drop=True)\n",
"# or: df.pix.extract(variable=\"Secondary Energy|{carrier}|{fuel}\")\n",
"splitdf"
Expand Down Expand Up @@ -2712,6 +2712,7 @@
"source": [
"from pandas_indexing import semijoin\n",
"\n",
"\n",
"semijoin(\n",
" fossil_series, idx, how=\"right\"\n",
") # or: fossil_series.pix.semijoin(idx, how=\"right\")"
Expand Down Expand Up @@ -2795,6 +2796,7 @@
"source": [
"from pandas_indexing import projectlevel\n",
"\n",
"\n",
"simple_fossil_series = projectlevel(fossil_series, [\"variable\", \"year\"])\n",
"# or: fossil_series.pix.project([\"variable\", \"year\"])\n",
"simple_fossil_series"
Expand Down Expand Up @@ -3714,6 +3716,7 @@
"source": [
"from pandas_indexing import assignlevel\n",
"\n",
"\n",
"assignlevel(df, variable=\"Updated|\" + projectlevel(df.index, \"variable\"), unit=\"bla\")\n",
"# or: df.pix.assign(variable=df.index.pix.project(\"variable\"), unit=\"bla\")"
]
Expand Down Expand Up @@ -4585,6 +4588,7 @@
"source": [
"from pandas_indexing import formatlevel\n",
"\n",
"\n",
"formatlevel(df, variable=\"Updated|{variable}\", unit=\"bla\")\n",
"# or: df.pix.format(variable=...)"
]
Expand Down Expand Up @@ -5518,6 +5522,7 @@
"source": [
"from pandas_indexing import uniquelevel\n",
"\n",
"\n",
"uniquelevel(df, \"variable\")\n",
"# or: df.pix.unique(\"variable\")\n",
"# or in vanilla pandas: df.index.unique(\"variable\")"
Expand Down Expand Up @@ -5591,31 +5596,30 @@
{
"data": {
"text/plain": [
"variable year \n",
"Coal 2030 182.0149\n",
" 2040 1.0729\n",
" 2050 0.5029\n",
" 2060 0.1836\n",
"Gas 2030 1584.4033\n",
" 2040 1289.4777\n",
" 2050 562.8482\n",
" 2060 349.9091\n",
"Nuclear 2030 275.5920\n",
" 2040 214.4376\n",
" 2050 156.7766\n",
" 2060 92.0667\n",
"2030 Coal 182.0149\n",
"2040 Coal 1.0729\n",
"2050 Coal 0.5029\n",
"2060 Coal 0.1836\n",
"2030 Gas 1584.4033\n",
"2040 Gas 1289.4777\n",
"2050 Gas 562.8482\n",
"2060 Gas 349.9091\n",
"2030 Nuclear 275.5920\n",
"2040 Nuclear 214.4376\n",
"2050 Nuclear 156.7766\n",
"2060 Nuclear 92.0667\n",
"Coal 2030 182.0149\n",
" 2040 1.0729\n",
" 2050 0.5029\n",
" 2060 0.1836\n",
"Gas 2030 1584.4033\n",
" 2040 1289.4777\n",
" 2050 562.8482\n",
" 2060 349.9091\n",
"Nuclear 2030 275.5920\n",
" 2040 214.4376\n",
" 2050 156.7766\n",
" 2060 92.0667\n",
"2030 Coal 182.0149\n",
"2040 Coal 1.0729\n",
"2050 Coal 0.5029\n",
"2060 Coal 0.1836\n",
"2030 Gas 1584.4033\n",
"2040 Gas 1289.4777\n",
"2050 Gas 562.8482\n",
"2060 Gas 349.9091\n",
"2030 Nuclear 275.5920\n",
"2040 Nuclear 214.4376\n",
"2050 Nuclear 156.7766\n",
"2060 Nuclear 92.0667\n",
"dtype: float64"
]
},
Expand Down Expand Up @@ -5695,10 +5699,47 @@
"execution_count": 31,
"id": "ef1d54c6",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"variable year\n",
"Coal 2030 182.0149\n",
" 2040 1.0729\n",
" 2050 0.5029\n",
" 2060 0.1836\n",
"Gas 2030 1584.4033\n",
" 2040 1289.4777\n",
" 2050 562.8482\n",
" 2060 349.9091\n",
"Nuclear 2030 275.5920\n",
" 2040 214.4376\n",
" 2050 156.7766\n",
" 2060 92.0667\n",
"Coal 2030 182.0149\n",
" 2040 1.0729\n",
" 2050 0.5029\n",
" 2060 0.1836\n",
"Gas 2030 1584.4033\n",
" 2040 1289.4777\n",
" 2050 562.8482\n",
" 2060 349.9091\n",
"Nuclear 2030 275.5920\n",
" 2040 214.4376\n",
" 2050 156.7766\n",
" 2060 92.0667\n",
"dtype: float64"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from pandas_indexing import concat\n",
"\n",
"\n",
"concat([simple_fossil_series, simple_fossil_series.swaplevel()])"
]
},
Expand All @@ -5722,7 +5763,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 32,
"id": "5b7cb7db",
"metadata": {},
"outputs": [
Expand All @@ -5744,7 +5785,7 @@
" names=['model', 'scenario', 'variable'])"
]
},
"execution_count": 33,
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -5755,7 +5796,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 33,
"id": "5b7cb7db",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -5878,7 +5919,7 @@
"11 REMIND-MAgPIE 2.1-4.3 DeepElec_SSP2_HighRE_Budg900 Nuclear"
]
},
"execution_count": 34,
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
Expand Down
2 changes: 1 addition & 1 deletion src/pandas_indexing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from importlib.metadata import version as _version

from . import core, datasets
from . import accessors, core, datasets
from .arithmetics import add, divide, multiply, subtract
from .core import (
aggregatelevel,
Expand Down
2 changes: 0 additions & 2 deletions tests/test_accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from pandas import DataFrame, Index, MultiIndex
from pandas.testing import assert_frame_equal, assert_index_equal, assert_series_equal

import pandas_indexing.accessors # noqa: F401


def test_assign_index(midx: MultiIndex):
"""
Expand Down

0 comments on commit 82b57d9

Please sign in to comment.