-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More annotations in Dataset #3112
Changes from 6 commits
5506eff
a537fbb
3b13dcb
6851c2f
29abedf
44cfb8e
b119ddc
9f71e0c
829f5ff
298f88b
52114a9
b0b6edc
e0036e7
7a36ff5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,16 @@ | |
import warnings | ||
from collections import OrderedDict, defaultdict | ||
from contextlib import suppress | ||
from typing import Any, Mapping, Optional, Tuple | ||
from typing import ( | ||
Any, | ||
Dict, | ||
Hashable, | ||
Mapping, | ||
Optional, | ||
Tuple, | ||
Union, | ||
TYPE_CHECKING, | ||
) | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
@@ -13,6 +22,10 @@ | |
from .utils import is_dict_like, is_full_slice | ||
from .variable import IndexVariable, Variable | ||
|
||
if TYPE_CHECKING: | ||
from .dataarray import DataArray | ||
from .dataset import Dataset | ||
|
||
|
||
def _get_joiner(join): | ||
if join == 'outer': | ||
|
@@ -169,8 +182,8 @@ def deep_align(objects, join='inner', copy=True, indexes=None, | |
|
||
This function is not public API. | ||
""" | ||
from .dataarray import DataArray | ||
from .dataset import Dataset | ||
from .dataarray import DataArray # noqa: F811 | ||
from .dataset import Dataset # noqa: F811 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a workaround to a flake8 limitation https://gitlab.com/pycqa/flake8/issues/553 |
||
|
||
if indexes is None: | ||
indexes = {} | ||
|
@@ -222,7 +235,10 @@ def is_alignable(obj): | |
return out | ||
|
||
|
||
def reindex_like_indexers(target, other): | ||
def reindex_like_indexers( | ||
target: Union['DataArray', 'Dataset'], | ||
other: Union['DataArray', 'Dataset'], | ||
) -> Dict[Hashable, pd.Index]: | ||
"""Extract indexers to align target with other. | ||
|
||
Not public API. | ||
|
@@ -236,7 +252,8 @@ def reindex_like_indexers(target, other): | |
|
||
Returns | ||
------- | ||
Dict[Any, pandas.Index] providing indexes for reindex keyword arguments. | ||
Dict[Hashable, pandas.Index] providing indexes for reindex keyword | ||
arguments. | ||
|
||
Raises | ||
------ | ||
|
@@ -310,7 +327,7 @@ def reindex_variables( | |
new_indexes : OrderedDict | ||
Dict of indexes associated with the reindexed variables. | ||
""" | ||
from .dataarray import DataArray | ||
from .dataarray import DataArray # noqa: F811 | ||
|
||
# create variables for the new dataset | ||
reindexed = OrderedDict() # type: OrderedDict[Any, Variable] | ||
|
@@ -463,8 +480,8 @@ def broadcast(*args, exclude=None): | |
a (x, y) int64 1 1 2 2 3 3 | ||
b (x, y) int64 5 6 5 6 5 6 | ||
""" | ||
from .dataarray import DataArray | ||
from .dataset import Dataset | ||
from .dataarray import DataArray # noqa: F811 | ||
from .dataset import Dataset # noqa: F811 | ||
|
||
if exclude is None: | ||
exclude = set() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1041,8 +1041,13 @@ def reindex_like(self, other: Union['DataArray', Dataset], | |
align | ||
""" | ||
indexers = reindex_like_indexers(self, other) | ||
return self.reindex(method=method, tolerance=tolerance, copy=copy, | ||
fill_value=fill_value, **indexers) | ||
return self.reindex( | ||
indexers=indexers, | ||
method=method, | ||
tolerance=tolerance, | ||
copy=copy, | ||
fill_value=fill_value, | ||
) | ||
|
||
def reindex(self, indexers: Optional[Mapping[Hashable, Any]] = None, | ||
method: Optional[str] = None, tolerance=None, | ||
|
@@ -1329,9 +1334,13 @@ def expand_dims(self, dim: Union[None, Hashable, Sequence[Hashable], | |
ds = self._to_temp_dataset().expand_dims(dim, axis) | ||
return self._from_temp_dataset(ds) | ||
|
||
def set_index(self, indexes: Optional[Mapping[Hashable, Any]] = None, | ||
append: bool = False, inplace: Optional[bool] = None, | ||
**indexes_kwargs: Any) -> Optional['DataArray']: | ||
def set_index( | ||
self, | ||
indexes: Mapping[Hashable, Union[Hashable, Sequence[Hashable]]] = None, | ||
append: bool = False, | ||
inplace: bool = None, | ||
**indexes_kwargs: Union[Hashable, Sequence[Hashable]] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing this is fine because mypy passes - my original thought was whether this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As of mypy 0.701, from typing import Optional
def f1(x: int = None) -> None:
pass
def f2(x: Optional[int] = None) -> None:
pass
def f3(x: int = 1) -> None:
pass
def f4(x: Optional[int] = 1) -> None:
pass
f1()
f2()
f3()
f4()
f1(1)
f2(1)
f3(1)
f4(1)
f1(None)
f2(None)
f3(None)
f4(None) mypy output:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @max-sixty I did a generalised cleanup of all already existing cases; I find it's much more readable now |
||
) -> Optional['DataArray']: | ||
"""Set DataArray (multi-)indexes using one or more existing | ||
coordinates. | ||
|
||
|
@@ -1370,9 +1379,12 @@ def set_index(self, indexes: Optional[Mapping[Hashable, Any]] = None, | |
else: | ||
return self._replace(coords=coords) | ||
|
||
def reset_index(self, dims_or_levels: Union[Hashable, Sequence[Hashable]], | ||
drop: bool = False, inplace: Optional[bool] = None | ||
) -> Optional['DataArray']: | ||
def reset_index( | ||
self, | ||
dims_or_levels: Union[Hashable, Sequence[Hashable]], | ||
drop: bool = False, | ||
inplace: bool = None | ||
) -> Optional['DataArray']: | ||
"""Reset the specified index(es) or multi-index level(s). | ||
|
||
Parameters | ||
|
@@ -1406,12 +1418,12 @@ def reset_index(self, dims_or_levels: Union[Hashable, Sequence[Hashable]], | |
else: | ||
return self._replace(coords=coords) | ||
|
||
def reorder_levels(self, | ||
dim_order: Optional[ | ||
Mapping[Hashable, Sequence[int]]] = None, | ||
inplace: Optional[bool] = None, | ||
**dim_order_kwargs: Sequence[int] | ||
) -> Optional['DataArray']: | ||
def reorder_levels( | ||
self, | ||
dim_order: Mapping[Hashable, Sequence[int]] = None, | ||
inplace: bool = None, | ||
**dim_order_kwargs: Sequence[int] | ||
) -> Optional['DataArray']: | ||
"""Rearrange index levels using input order. | ||
|
||
Parameters | ||
|
@@ -1452,9 +1464,11 @@ def reorder_levels(self, | |
else: | ||
return self._replace(coords=coords) | ||
|
||
def stack(self, dimensions: Optional[ | ||
Mapping[Hashable, Sequence[Hashable]]] = None, | ||
**dimensions_kwargs: Sequence[Hashable]) -> 'DataArray': | ||
def stack( | ||
self, | ||
dimensions: Mapping[Hashable, Sequence[Hashable]] = None, | ||
**dimensions_kwargs: Sequence[Hashable] | ||
) -> 'DataArray': | ||
""" | ||
Stack any number of existing dimensions into a single new dimension. | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move this up to 0.12.4?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done