Fix: handle num=1 in RangeIndex.linspace to match numpy behavior#11403
Open
syu-toutousai wants to merge 1 commit into
Open
Fix: handle num=1 in RangeIndex.linspace to match numpy behavior#11403syu-toutousai wants to merge 1 commit into
syu-toutousai wants to merge 1 commit into
Conversation
When num=1, RangeIndex.linspace raised ZeroDivisionError because it divided by (num - 1). NumPy's linspace returns a single-element array at the start value in this case. This fix aligns the behavior. Fixes pydata#11397
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
RangeIndex.linspaceraisesZeroDivisionErrorwhen called withnum=1because it divides by(num - 1)whenendpoint=True.numpy.linspacehandles this case by returning a single-element array at thestartvalue.This fix adds an early return when
num == 1, settingstop = startto produce a single-element RangeIndex, matching numpy's behavior.Changes
xarray/indexes/range_index.py: Addednum == 1guard inlinspacemethodTesting
Fixes #11397
Payment: PayPal n6085530@gmail.com