Skip to content

Commit

Permalink
14k -> 14.5k even faster path
Browse files Browse the repository at this point in the history
  • Loading branch information
hmaarrfk committed May 5, 2024
1 parent 2e70989 commit 29d7064
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
28 changes: 19 additions & 9 deletions xarray/core/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,13 +575,19 @@ class PandasIndex(Index):

__slots__ = ("index", "dim", "coord_dtype")

def __init__(self, array: Any, dim: Hashable, coord_dtype: Any = None):
# make a shallow copy: cheap and because the index name may be updated
# here or in other constructors (cannot use pd.Index.rename as this
# constructor is also called from PandasMultiIndex)
index = safe_cast_to_index(array).copy()
def __init__(
self, array: Any, dim: Hashable, coord_dtype: Any = None, *, fastpath=False
):
if fastpath:
index = array
else:
index = safe_cast_to_index(array)

if index.name is None:
# make a shallow copy: cheap and because the index name may be updated
# here or in other constructors (cannot use pd.Index.rename as this
# constructor is also called from PandasMultiIndex)
index = index.copy()
index.name = dim

self.index = index
Expand All @@ -596,7 +602,7 @@ def _replace(self, index, dim=None, coord_dtype=None):
dim = self.dim
if coord_dtype is None:
coord_dtype = self.coord_dtype
return type(self)(index, dim, coord_dtype)
return type(self)(index, dim, coord_dtype, fastpath=True)

@classmethod
def from_variables(
Expand Down Expand Up @@ -641,6 +647,8 @@ def from_variables(

obj = cls(data, dim, coord_dtype=var.dtype)
assert not isinstance(obj.index, pd.MultiIndex)
# Rename safely
obj.index = obj.index.copy()
obj.index.name = name

return obj
Expand Down Expand Up @@ -684,7 +692,7 @@ def concat(
return cls(new_pd_index, dim=dim, coord_dtype=coord_dtype)

def create_variables(
self, variables: Mapping[Any, Variable] | None = None
self, variables: Mapping[Any, Variable] | None = None, *, fastpath=False
) -> IndexVars:
from xarray.core.variable import IndexVariable

Expand All @@ -701,7 +709,9 @@ def create_variables(
encoding = None

data = PandasIndexingAdapter(self.index, dtype=self.coord_dtype)
var = IndexVariable(self.dim, data, attrs=attrs, encoding=encoding)
var = IndexVariable(
self.dim, data, attrs=attrs, encoding=encoding, fastpath=fastpath
)
return {name: var}

def to_pandas_index(self) -> pd.Index:
Expand Down Expand Up @@ -1794,7 +1804,7 @@ def _apply_indexes_fast(indexes: Indexes[Index], args: Mapping[Any, Any], func:
new_index = getattr(index, func)(index_args)
if new_index is not None:
new_indexes.update({k: new_index for k in index_vars})
new_index_vars = new_index.create_variables(index_vars)
new_index_vars = new_index.create_variables(index_vars, fastpath=True)
new_index_variables.update(new_index_vars)
new_index_variables.update(new_index_vars)
else:
Expand Down
7 changes: 5 additions & 2 deletions xarray/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1662,10 +1662,13 @@ class PandasIndexingAdapter(ExplicitlyIndexedNDArrayMixin):

__slots__ = ("array", "_dtype")

def __init__(self, array: pd.Index, dtype: DTypeLike = None):
def __init__(self, array: pd.Index, dtype: DTypeLike = None, *, fastpath=False):
from xarray.core.indexes import safe_cast_to_index

self.array = safe_cast_to_index(array)
if fastpath:
self.array = array
else:
self.array = safe_cast_to_index(array)

if dtype is None:
self._dtype = get_valid_numpy_dtype(array)
Expand Down

0 comments on commit 29d7064

Please sign in to comment.