Skip to content

Commit e677b7a

Browse files
authored
Refactor (part of) dataset.py to use explicit indexes (#2696)
* Refactor (part of) dataset.py to use explicit indexes * Use copy.copy() * Ensure coordinate order is deterministic
1 parent 27cf53f commit e677b7a

File tree

8 files changed

+372
-174
lines changed

8 files changed

+372
-174
lines changed

xarray/core/alignment.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import warnings
44
from collections import OrderedDict, defaultdict
55
from contextlib import suppress
6+
from typing import Any, Mapping, Optional
67

78
import numpy as np
9+
import pandas as pd
810

911
from . import utils
1012
from .indexing import get_indexer_nd
1113
from .utils import is_dict_like, is_full_slice
12-
from .variable import IndexVariable
14+
from .variable import IndexVariable, Variable
1315

1416

1517
def _get_joiner(join):
@@ -260,8 +262,15 @@ def reindex_like_indexers(target, other):
260262
return indexers
261263

262264

263-
def reindex_variables(variables, sizes, indexes, indexers, method=None,
264-
tolerance=None, copy=True):
265+
def reindex_variables(
266+
variables: Mapping[Any, Variable],
267+
sizes: Mapping[Any, int],
268+
indexes: Mapping[Any, pd.Index],
269+
indexers: Mapping,
270+
method: Optional[str] = None,
271+
tolerance: Any = None,
272+
copy: bool = True,
273+
) -> 'Tuple[OrderedDict[Any, Variable], OrderedDict[Any, pd.Index]]':
265274
"""Conform a dictionary of aligned variables onto a new set of variables,
266275
filling in missing values with NaN.
267276
@@ -274,7 +283,7 @@ def reindex_variables(variables, sizes, indexes, indexers, method=None,
274283
sizes : dict-like
275284
Dictionary from dimension names to integer sizes.
276285
indexes : dict-like
277-
Dictionary of xarray.IndexVariable objects associated with variables.
286+
Dictionary of indexes associated with variables.
278287
indexers : dict
279288
Dictionary with keys given by dimension names and values given by
280289
arrays of coordinates tick labels. Any mis-matched coordinate values
@@ -300,13 +309,15 @@ def reindex_variables(variables, sizes, indexes, indexers, method=None,
300309
Returns
301310
-------
302311
reindexed : OrderedDict
303-
Another dict, with the items in variables but replaced indexes.
312+
Dict of reindexed variables.
313+
new_indexes : OrderedDict
314+
Dict of indexes associated with the reindexed variables.
304315
"""
305316
from .dataarray import DataArray
306317

307318
# build up indexers for assignment along each dimension
308319
int_indexers = {}
309-
targets = {}
320+
targets = OrderedDict()
310321
masked_dims = set()
311322
unchanged_dims = set()
312323

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

360371
if dim in variables:
361372
var = variables[dim]
362-
args = (var.attrs, var.encoding)
373+
args = (var.attrs, var.encoding) # type: tuple
363374
else:
364375
args = ()
365376
reindexed[dim] = IndexVariable((dim,), indexers[dim], *args)
@@ -384,7 +395,10 @@ def reindex_variables(variables, sizes, indexes, indexers, method=None,
384395

385396
reindexed[name] = new_var
386397

387-
return reindexed
398+
new_indexes = OrderedDict(indexes)
399+
new_indexes.update(targets)
400+
401+
return reindexed, new_indexes
388402

389403

390404
def broadcast(*args, **kwargs):

0 commit comments

Comments
 (0)