Skip to content
forked from pydata/xarray

Commit

Permalink
Bad idea to deep copy indexes.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcherian committed Nov 17, 2019
1 parent 1bffa83 commit aefa5e3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
4 changes: 2 additions & 2 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ def _to_dataset_whole(
if shallow_copy:
for k in variables:
variables[k] = variables[k].copy(deep=False)
indexes = copy_indexes(self._indexes, deep=(not shallow_copy))
indexes = copy_indexes(self._indexes)

coord_names = set(self._coords)
dataset = Dataset._from_vars_and_coord_names(
Expand Down Expand Up @@ -943,7 +943,7 @@ def copy(self, deep: bool = True, data: Any = None) -> "DataArray":
"""
variable = self.variable.copy(deep=deep, data=data)
coords = {k: v.copy(deep=deep) for k, v in self._coords.items()}
indexes = copy_indexes(self._indexes, deep=deep)
indexes = copy_indexes(self._indexes)
return self._replace(variable, coords, indexes=indexes)

def __copy__(self) -> "DataArray":
Expand Down
10 changes: 4 additions & 6 deletions xarray/core/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,18 @@ def roll_index(index: pd.Index, count: int, axis: int = 0) -> pd.Index:


def copy_indexes(
indexes: Optional[Dict[Hashable, pd.Index]],
deep: bool = True,
exclude: Optional[Any] = None,
indexes: Optional[Dict[Hashable, pd.Index]], exclude: Optional[Any] = None
) -> Optional[Dict[Hashable, pd.Index]]:
""" Creates new indexes dict from existing dict optionally excluding some dimensions.
"""
if exclude is None:
exclude = ()

if is_scalar(exclude):
exclude = (exclude,)

if indexes is not None:
new_indexes = {
k: v.copy(deep=deep) for k, v in indexes.items() if k not in exclude
}
new_indexes = {k: v for k, v in indexes.items() if k not in exclude}
else:
new_indexes = None # type: ignore

Expand Down

0 comments on commit aefa5e3

Please sign in to comment.