Skip to content

Commit

Permalink
chore: Type adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisjared committed Sep 21, 2023
1 parent eca06f7 commit d1f0564
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ Redistribution and use in source and binary forms, with or without modification,

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 1 addition & 1 deletion docs/source/_static/tables.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Force wide pandas tables to scroll */
.output.text_html {
overflow: scroll;
}
}
4 changes: 2 additions & 2 deletions docs/source/notebooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ This directory contains notebooks of examples for how to use `scmdata`.

We use [Jupytext](https://github.com/mwouts/jupytext) to encode the notebooks as standard .py files. This makes
it easier to version control notebooks as you get a clear, meaningful diffs. Jupytext also enables these notebooks
to be edited via Jupyter Lab or Jupyter Notebook.
to be edited via Jupyter Lab or Jupyter Notebook.

As part of the CI, these notebooks are run and checked for any errors.
As part of the CI, these notebooks are run and checked for any errors.
9 changes: 4 additions & 5 deletions src/scmdata/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import warnings
from collections.abc import Iterable
from typing import TYPE_CHECKING, Callable, Iterator, Optional, Sequence, Union
from typing import TYPE_CHECKING, Callable, Generic, Iterator, Optional, Sequence, Union

import numpy as np
import pandas as pd
Expand All @@ -12,13 +12,12 @@
from xarray.core.common import ImplementsArrayReduce

from scmdata._typing import MetadataValue
from scmdata.run import GenericRun

if TYPE_CHECKING:
from pandas.core.groupby.generic import DataFrameGroupBy
from typing_extensions import Concatenate, ParamSpec

Check warning on line 19 in src/scmdata/groupby.py

View check run for this annotation

Codecov / codecov/patch

src/scmdata/groupby.py#L18-L19

Added lines #L18 - L19 were not covered by tests

from scmdata.run import GenericRun

P = ParamSpec("P")

Check warning on line 21 in src/scmdata/groupby.py

View check run for this annotation

Codecov / codecov/patch

src/scmdata/groupby.py#L21

Added line #L21 was not covered by tests


Expand All @@ -36,7 +35,7 @@ def _maybe_wrap_array(original, new_array):
return new_array


class RunGroupBy(ImplementsArrayReduce, "Generic[GenericRun]"):
class RunGroupBy(ImplementsArrayReduce, Generic[GenericRun]):
"""
GroupBy object specialized to grouping ScmRun objects
"""
Expand Down Expand Up @@ -89,7 +88,7 @@ def _try_fill_value(v: MetadataValue) -> MetadataValue:
)
yield res

def __iter__(self) -> "Iterator[GenericRun]":
def __iter__(self) -> Iterator[GenericRun]:
"""
Iterate over the groups
"""
Expand Down
2 changes: 1 addition & 1 deletion src/scmdata/pyam_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dateutil import parser

try:
from pyam import IamDataFrame
from pyam import IamDataFrame # type: ignore

# mypy can't work out try-except block forces IamDataFrame to be here
class LongDatetimeIamDataFrame(IamDataFrame): # type: ignore
Expand Down
29 changes: 17 additions & 12 deletions src/scmdata/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
pattern_match,
years_match,
)
from .groupby import RunGroupBy
from .netcdf import inject_nc_methods
from .offsets import generate_range, to_offset
from .ops import inject_ops_methods
Expand All @@ -74,6 +73,8 @@
from numpy.typing import NDArray
from typing_extensions import Concatenate, ParamSpec

Check warning on line 74 in src/scmdata/run.py

View check run for this annotation

Codecov / codecov/patch

src/scmdata/run.py#L74

Added line #L74 was not covered by tests

from scmdata.groupby import RunGroupBy

Check warning on line 76 in src/scmdata/run.py

View check run for this annotation

Codecov / codecov/patch

src/scmdata/run.py#L76

Added line #L76 was not covered by tests

P = ParamSpec("P")

Check warning on line 78 in src/scmdata/run.py

View check run for this annotation

Codecov / codecov/patch

src/scmdata/run.py#L78

Added line #L78 was not covered by tests


Expand Down Expand Up @@ -690,11 +691,11 @@ def _indent(s):

def _binary_op(
self,
other: Self,
other: Self | pint.Quantity | float | int,
f: Callable[[pd.DataFrame, pd.DataFrame], pd.DataFrame],
reflexive: bool = False,
**kwargs: Any,
) -> pd.DataFrame:
) -> Self:
if isinstance(other, ScmRun):
return NotImplemented

Expand All @@ -713,18 +714,18 @@ def _binary_op(
f"operations with {other_ndim}d data are not supported"
)

def _perform_op(df):
def _perform_op(run: Self) -> Self:
if isinstance(other, pint.Quantity):
try:
data = df.values * ur(df.get_unique_meta("unit", True))
data = run.values * ur(run.get_unique_meta("unit", True))
use_pint = True
except KeyError: # pragma: no cover # emergency valve
raise KeyError( # noqa: TRY200
"No `unit` column in your metadata, cannot perform operations "
"with pint quantities"
)
else:
data = df.values
data = run.values
use_pint = False

res = []
Expand All @@ -736,11 +737,11 @@ def _perform_op(df):
res_stacked = np.vstack(res)

if use_pint:
df._df.values[:] = res_stacked.magnitude.T
df["unit"] = str(res_stacked.units)
run._df.values[:] = res_stacked.magnitude.T
run["unit"] = str(res_stacked.units)
else:
df._df.values[:] = res_stacked.T
return df
run._df.values[:] = res_stacked.T
return run

return self.copy().groupby("unit").apply(_perform_op)

Expand Down Expand Up @@ -1299,7 +1300,7 @@ def _day_match(self, values):

return day_match(days, values)

def head(self, *args: Any, **kwargs: Any) -> pd.DataFrame[float]:
def head(self, *args: Any, **kwargs: Any) -> pd.DataFrame:
"""
Return head of :func:`self.timeseries()`.
Expand All @@ -1318,7 +1319,7 @@ def head(self, *args: Any, **kwargs: Any) -> pd.DataFrame[float]:
"""
return self.timeseries().head(*args, **kwargs) # type: ignore

def tail(self, *args: Any, **kwargs: Any) -> pd.DataFrame[float]:
def tail(self, *args: Any, **kwargs: Any) -> pd.DataFrame:
"""
Return tail of :func:`self.timeseries()`.
Expand Down Expand Up @@ -1941,6 +1942,8 @@ def groupby(self, *group: str | Iterable[str]) -> RunGroupBy[Self]:
:class:`RunGroupBy`
See the documentation for :class:`RunGroupBy` for more information
"""
from .groupby import RunGroupBy

group = self._check_groupby_input(group)

return RunGroupBy(self, group)
Expand Down Expand Up @@ -2028,6 +2031,8 @@ def groupby_all_except(self, *not_group: str) -> RunGroupBy[Self]:
:class:`RunGroupBy`
See the documentation for :class:`RunGroupBy` for more information
"""
from .groupby import RunGroupBy

group = self.get_meta_columns_except(not_group)

return RunGroupBy(self, group)
Expand Down
Loading

0 comments on commit d1f0564

Please sign in to comment.