Skip to content

Commit

Permalink
Refactor (part of) dataset.py to use explicit indexes (#2696)
Browse files Browse the repository at this point in the history
* Refactor (part of) dataset.py to use explicit indexes

* Use copy.copy()

* Ensure coordinate order is deterministic
  • Loading branch information
shoyer authored Feb 6, 2019
1 parent 27cf53f commit e677b7a
Show file tree
Hide file tree
Showing 8 changed files with 372 additions and 174 deletions.
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

0 comments on commit e677b7a

Please sign in to comment.