Skip to content

Refactor (part of) dataset.py to use explicit indexes #2696

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

Merged
merged 5 commits into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions xarray/core/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import warnings
from collections import OrderedDict, defaultdict
from contextlib import suppress
from typing import Any, Mapping, Optional

import numpy as np
import pandas as pd

from . import utils
from .indexing import get_indexer_nd
from .utils import is_dict_like, is_full_slice
from .variable import IndexVariable
from .variable import IndexVariable, Variable


def _get_joiner(join):
Expand Down Expand Up @@ -260,8 +262,15 @@ def reindex_like_indexers(target, other):
return indexers


def reindex_variables(variables, sizes, indexes, indexers, method=None,
tolerance=None, copy=True):
def reindex_variables(
variables: Mapping[Any, Variable],
sizes: Mapping[Any, int],
indexes: Mapping[Any, pd.Index],
indexers: Mapping,
method: Optional[str] = None,
tolerance: Any = None,
copy: bool = True,
) -> 'Tuple[OrderedDict[Any, Variable], OrderedDict[Any, pd.Index]]':
"""Conform a dictionary of aligned variables onto a new set of variables,
filling in missing values with NaN.

Expand All @@ -274,7 +283,7 @@ def reindex_variables(variables, sizes, indexes, indexers, method=None,
sizes : dict-like
Dictionary from dimension names to integer sizes.
indexes : dict-like
Dictionary of xarray.IndexVariable objects associated with variables.
Dictionary of indexes associated with variables.
indexers : dict
Dictionary with keys given by dimension names and values given by
arrays of coordinates tick labels. Any mis-matched coordinate values
Expand All @@ -300,13 +309,15 @@ def reindex_variables(variables, sizes, indexes, indexers, method=None,
Returns
-------
reindexed : OrderedDict
Another dict, with the items in variables but replaced indexes.
Dict of reindexed variables.
new_indexes : OrderedDict
Dict of indexes associated with the reindexed variables.
"""
from .dataarray import DataArray

# build up indexers for assignment along each dimension
int_indexers = {}
targets = {}
targets = OrderedDict()
masked_dims = set()
unchanged_dims = set()

Expand Down Expand Up @@ -359,7 +370,7 @@ def reindex_variables(variables, sizes, indexes, indexers, method=None,

if dim in variables:
var = variables[dim]
args = (var.attrs, var.encoding)
args = (var.attrs, var.encoding) # type: tuple
else:
args = ()
reindexed[dim] = IndexVariable((dim,), indexers[dim], *args)
Expand All @@ -384,7 +395,10 @@ def reindex_variables(variables, sizes, indexes, indexers, method=None,

reindexed[name] = new_var

return reindexed
new_indexes = OrderedDict(indexes)
new_indexes.update(targets)

return reindexed, new_indexes


def broadcast(*args, **kwargs):
Expand Down
Loading