Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Bug Fixes
- Fix a major performance regression in :py:meth:`Coordinates.to_index` (and
consequently :py:meth:`Dataset.to_dataframe`) caused by converting the cached
code ndarrays into Python lists (:issue:`11305`).
- Preserve the requested spacing when :py:meth:`xarray.indexes.RangeIndex.arange`
is called with a step that does not evenly divide the interval (:issue:`11325`).


Documentation
Expand Down
7 changes: 4 additions & 3 deletions xarray/indexes/range_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ def slice(self, sl: slice) -> "RangeCoordinateTransform":
self.dim,
dtype=self.dtype,
)
if new_size == 0:
# For empty slices, preserve step from parent
result._step = self.step
# Preserve the logical slice spacing without carrying small binary
# multiplication artifacts into materialized coordinates.
result._step = round(self.step * new_range.step, 15)
return result


Expand Down Expand Up @@ -281,6 +281,7 @@ def arange(
transform = RangeCoordinateTransform(
start, stop, size, coord_name, dim, dtype=dtype
)
transform._step = step
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assigning a different value than would be computed to a property's cache seems brittle


return cls(transform)

Expand Down
1 change: 1 addition & 0 deletions xarray/tests/test_range_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def create_dataset_arange(
((), {"start": 2.0, "stop": 10.0}),
((2.0, 10.0, 2.0), {}),
((), {"start": 2.0, "stop": 10.0, "step": 2.0}),
((0.0, 1.0, 0.3), {}),
],
)
def test_range_index_arange(args, kwargs) -> None:
Expand Down