Skip to content

Commit

Permalink
Add unit handling w/ and w/o pint_pandas (#17)
Browse files Browse the repository at this point in the history
* Add unit handling w/ and w/o pint_pandas

* Keep track of metadata

* Fix bugs and add docs

* Make typing python 3.8 compatible

* Add unit tests

* Add units dependencies to tox

* Delay pint import

* Add accessor test

* Improve import order
  • Loading branch information
coroa authored Jun 24, 2023
1 parent a5fa5d9 commit a74b9ab
Show file tree
Hide file tree
Showing 8 changed files with 470 additions and 6 deletions.
7 changes: 7 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ selectors

.. automodule:: pandas_indexing.selectors
:members:


units
=====

.. automodule:: pandas_indexing.units
:members:
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ test = [
"hypothesis",
]

units = [
"pint>=0.21",
"openscm-units",
]

lint = [
"black",
"ruff",
Expand Down
1 change: 1 addition & 0 deletions src/pandas_indexing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
uniquelevel,
)
from .selectors import isin, ismatch
from .units import convert_unit, dequantify, quantify, set_openscm_registry_as_default


try:
Expand Down
34 changes: 31 additions & 3 deletions src/pandas_indexing/accessors.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""Registers convenience accessors into the ``idx`` namespace of each pandas
object.
Usage
-----
Examples
--------
>>> df.idx.project(["model", "scenario"])
>>> df.index.idx.assign(unit="Mt CO2")
>>> df.idx.multiply(other, how="left")
"""

from typing import Any, Literal, Optional, Sequence, Union
from typing import Any, Callable, Literal, Mapping, Optional, Sequence, Union

import pandas as pd
from pandas import DataFrame, Index, MultiIndex, Series
Expand All @@ -29,6 +29,7 @@
uniquelevel,
)
from .types import Axis, Data
from .units import convert_unit, dequantify, quantify
from .utils import doc


Expand Down Expand Up @@ -134,6 +135,33 @@ def add(self, other, **align_kwds):
def subtract(self, other, **align_kwds):
return arithmetics.subtract(self._obj, other, **align_kwds)

@doc(quantify, data="", example_call="s.idx.quantify()")
def quantify(
self,
level: str = "unit",
unit: Optional[str] = None,
axis: Axis = 0,
copy: bool = False,
):
return quantify(self._obj, level=level, unit=unit, axis=axis, copy=copy)

def dequantify(self, level: str = "unit", axis: Axis = 0, copy: bool = False):
return dequantify(self._obj, level=level, axis=axis, copy=copy)

@doc(
convert_unit,
data="",
convert_unit_s_km='s.idx.convert_unit("km")',
convert_unit_s_m_to_km='s.idx.convert_unit({"m": "km"})',
)
def convert_unit(
self,
unit: Union[str, Mapping[str, str], Callable[[str], str]],
level: Optional[str] = "unit",
axis: Axis = 0,
):
return convert_unit(self._obj, unit=unit, level=level, axis=axis)


@pd.api.extensions.register_dataframe_accessor("idx")
class DataFrameIdxAccessor(_DataIdxAccessor):
Expand Down
Loading

0 comments on commit a74b9ab

Please sign in to comment.