Skip to content

ENH: Disclose offending values in IntervalArray._validate #66807

Description

@SlowMo24

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

When using IntervalIndex.from_breaks one ends up at IntervalArray._validate which, among others asserts that the left end of the interval is <= to the right end.

Yet, it does not disclose the offending values and just raises a generic left side of interval must be <= right side ValueError. The provided values for the IntervalArray may be long so the user may not be able to easily spot the offenders.

Feature Description

At this point an Exception is raised so one can assume the the library has completed its task. Hence a slight overhead for a improved error message may be acceptable. Only challenge is to not overcharge the error message, in case there are many offenders.

One possible solution could be to change

        if not (left[left_mask] <= right[left_mask]).all():
            msg = "left side of interval must be <= right side"

to something like (pseudo code)

order_offenders = left[left_mask] > right[left_mask]
if order_offenders.any():
    msg = f"left side of interval must be <= right side. offending intervals: {zip(left[order_offenders], right[order_offenders]}"

Alternative Solutions

Alternatively, if this seems to risky or resource intensive, maybe just printing the first 5 offending indices is already helpful

(pseudocode)

order_offenders = left[left_mask] > right[left_mask]
if order_offenders.any():
    msg = f"left side of interval must be <= right side. offending locations: {order_offenders[order_offenders].index.head(5)}"

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions