Skip to content

Conversation

khemkaran10
Copy link
Contributor

Fixes: #61917
Before FIx ❌:

idx_neg = pd.IntervalIndex.from_tuples([(-4, -3), (-4, -3), (-3, -2), (-3, -2), (-2, -1), (-2, -1)])
print(idx_neg.unique())

# Output:
# IntervalIndex([(-4, -3]], dtype='interval[int64, right]')

After Fix ✅:

idx_neg = pd.IntervalIndex.from_tuples([(-4, -3), (-4, -3), (-3, -2), (-3, -2), (-2, -1), (-2, -1)])
print(idx_neg.unique())

# output:
# IntervalIndex([(-4, -3], (-3, -2], (-2, -1]], dtype='interval[int64, right]')

@simonjayhawkins simonjayhawkins added Bug Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Interval Interval data type labels Jul 22, 2025
@simonjayhawkins simonjayhawkins changed the title FIX BUG: IntervalIndex.unique() only contains the first interval if all interval borders are negative BUG: IntervalIndex.unique() only contains the first interval if all interval borders are negative Jul 22, 2025
@il1sf4
Copy link

il1sf4 commented Jul 31, 2025

Hello @khemkaran10, I have tried your fix and I have the following issue. When i run with this branch this code:
import pandas as pd idx_neg = pd.IntervalIndex.from_tuples([(-4, -3), (-4, -3), (-3, -2), (-3, -2), (-2, -1), (-2, -1)]) print(idx_neg.unique())
I get from the last line: E ValueError: left side of interval must be <= right side
I have tried to debug the problem and I found that:
unique() from interval.py calls at the return self._from_combined(nc). the _from_combined() method from interval.py sets nc to nc = combined.view("i8").reshape(-1, 2). Here is the problem, because:
array([[-4.-3.j],
[-3.-2.j],
[-2.-1.j]])
gets transformed to:
array([[-4607182418800017408, -4609434218613702656],
[-4609434218613702656, -4611686018427387904],
[-4611686018427387904, -4616189618054758400]])
and then the method delivers an invalid Intervalindex, where left side is greater then the right side.

Gould you try to run the code again and assure, that idx_neg.unique() really returns "IntervalIndex([(-4, -3], (-3, -2], (-2, -1]], dtype='interval[int64, right]')"? In my tests the return is "IntervalIndex([([-4607182418800017408, -4609434218613702656], (-4609434218613702656, -4611686018427387904], (-4611686018427387904, -4616189618054758400]], dtype='interval[int64, right]')" which triggers the error: "E ValueError: left side of interval must be <= right side"

@khemkaran10
Copy link
Contributor Author

@il1sf4 Thanks for pointing out. I have update the PR.

)._from_sequence(nc[:, 1], dtype=dtype)
else:
assert isinstance(dtype, np.dtype)
nc = np.hstack(
Copy link
Member

Choose a reason for hiding this comment

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

is the hstack really necessary here since the next 2 lines is just splitting them back up?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks @jbrockmendel , It's not required. I'll change this.

Copy link
Contributor

github-actions bot commented Sep 1, 2025

This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this.

@github-actions github-actions bot added the Stale label Sep 1, 2025
Copy link

@il1sf4 il1sf4 left a comment

Choose a reason for hiding this comment

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

Issue is fixed, approved.

@il1sf4
Copy link

il1sf4 commented Sep 1, 2025

Dear reviewers @jbrockmendel , @mroeschke , please, check again the bugfix commits and if you have no other findings, please, approve the PR. I need this patch to be released ASAP for my project.

@il1sf4
Copy link

il1sf4 commented Sep 16, 2025

Why is taking so long this bugfix PR to be merged?

@johannes-mueller
Copy link
Contributor

@mroeschke is there anything blocking this? This fix is needed here. Anything that we can do to get this merged?

@johannes-mueller
Copy link
Contributor

@khemkaran10 are the last two comments by @jbrockmendel resolved?

@khemkaran10
Copy link
Contributor Author

@johannes-mueller yes both are resolved.

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

Gentle ping @jbrockmendel

@khemkaran10
Copy link
Contributor Author

@rhshadrach @jbrockmendel I have made the requested changes. I think it's good to merge now.

nc = unique(self._combined)
# Ensure nc is a numpy array for _from_combined
if not isinstance(nc, np.ndarray):
nc = np.asarray(nc)
Copy link
Member

Choose a reason for hiding this comment

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

Is this hit by the tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rhshadrach I added this check to handle mypy failure. should I add a separate test for this one?

Copy link
Contributor

@johannes-mueller johannes-mueller Oct 9, 2025

Choose a reason for hiding this comment

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

Is this check actually needed? I thought np.asarray() is doing such a check internally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Bug Interval Interval data type Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: IntervalIndex.unique() only contains the first interval if all interval borders are negative

8 participants