Skip to content

Commit 37dc249

Browse files
eendebakptpre-commit-ci[bot]dcherian
authored andcommitted
Improve performance of dataset indexing (pydata#11276)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
1 parent 58eb68b commit 37dc249

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

doc/whats-new.rst

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,25 @@ Breaking Changes
102102

103103
Deprecations
104104
~~~~~~~~~~~~
105-
105+
- Following pandas, on xarray's
106+
:py:class:`~xarray.core.accessor_dt.DatetimeAccessor`,
107+
:py:attr:`~xarray.core.accessor_dt.DatetimeAccessor.daysinmonth`
108+
is deprecated in favor of
109+
:py:attr:`~xarray.core.accessor_dt.DatetimeAccessor.days_in_month`;
110+
:py:attr:`~xarray.core.accessor_dt.DatetimeAccessor.dayofweek` and
111+
:py:attr:`~xarray.core.accessor_dt.DatetimeAccessor.weekday` are deprecated
112+
in favor of :py:attr:`~xarray.core.accessor_dt.DatetimeAccessor.day_of_week`;
113+
and :py:attr:`~xarray.core.accessor_dt.DatetimeAccessor.dayofyear` is
114+
deprecated in favor of
115+
:py:attr:`~xarray.core.accessor_dt.DatetimeAccessor.day_of_year`
116+
(:issue:`11268`, :pull:`11270`). By `Spencer Clark
117+
<https://github.com/spencerkclark>`_.
118+
- Following pandas, on xarray's :py:class:`~xarray.CFTimeIndex`,
119+
:py:attr:`~xarray.CFTimeIndex.dayofweek` and
120+
:py:attr:`~xarray.CFTimeIndex.dayofyear` are deprecated in favor of
121+
:py:attr:`~xarray.CFTimeIndex.day_of_week` and
122+
:py:attr:`~xarray.CFTimeIndex.day_of_year`, respectively (:pull:`11270`).
123+
By `Spencer Clark <https://github.com/spencerkclark>`_.
106124

107125
Bug Fixes
108126
~~~~~~~~~

xarray/core/dataset_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ def _get_virtual_variable(
6767
dim_sizes = {}
6868

6969
if key in dim_sizes:
70-
data = pd.Index(range(dim_sizes[key]), name=key)
71-
variable = IndexVariable((key,), data)
70+
data = pd.RangeIndex(dim_sizes[key], name=key)
71+
variable = IndexVariable((key,), data, fastpath=True)
7272
return key, key, variable
7373

7474
if not isinstance(key, str):

xarray/core/indexes.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,9 @@ def create_variables(
807807
encoding = None
808808

809809
data = PandasIndexingAdapter(self.index, dtype=self.coord_dtype)
810-
var = IndexVariable(self.dim, data, attrs=attrs, encoding=encoding)
810+
var = IndexVariable(
811+
self.dim, data, attrs=attrs, encoding=encoding, fastpath=True
812+
)
811813
return {name: var}
812814

813815
def to_pandas_index(self) -> pd.Index:
@@ -829,6 +831,9 @@ def isel(
829831
# scalar indexer: drop index
830832
return None
831833

834+
if isinstance(indxr, slice) and indxr == slice(None):
835+
return self
836+
832837
return self._replace(self.index[indxr]) # type: ignore[index,unused-ignore]
833838

834839
def sel(
@@ -2164,7 +2169,10 @@ def _apply_indexes_fast(indexes: Indexes[Index], args: Mapping[Any, Any], func:
21642169
new_index = getattr(index, func)(index_args)
21652170
if new_index is not None:
21662171
new_indexes.update(dict.fromkeys(index_vars, new_index))
2167-
new_index_vars = new_index.create_variables(index_vars)
2172+
if new_index is index:
2173+
new_index_vars = index_vars
2174+
else:
2175+
new_index_vars = new_index.create_variables(index_vars)
21682176
new_index_variables.update(new_index_vars)
21692177
else:
21702178
for k in index_vars:

xarray/core/variable.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,18 @@ def _broadcast_indexes(self, key):
634634
positions.
635635
"""
636636
key = self._item_key_to_tuple(key) # key is a tuple
637+
# Fast path: key is already a tuple of the right length with only
638+
# ints and slices (the common case from Variable.isel)
639+
if (
640+
isinstance(key, tuple)
641+
and len(key) == self.ndim
642+
and all(
643+
not isinstance(k, bool) and isinstance(k, BASIC_INDEXING_TYPES)
644+
for k in key
645+
)
646+
):
647+
return self._broadcast_indexes_basic(key)
648+
637649
# key is a tuple of full size
638650
key = indexing.expanded_indexer(key, self.ndim)
639651
# Convert a scalar Variable to a 0d-array

0 commit comments

Comments
 (0)