Skip to content

Fix for issue #57268 - ENH: Preserve input start/end type in interval… #57399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Feb 15, 2024

Conversation

VISWESWARAN1998
Copy link
Contributor

@VISWESWARAN1998 VISWESWARAN1998 commented Feb 13, 2024

…_range

Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. Needs a whatsnew note in v3.0.0.rst and a unit test.

Also could you fix this for float types too?

@mroeschke mroeschke added Dtype Conversions Unexpected or buggy dtype conversions Interval Interval data type labels Feb 13, 2024
@VISWESWARAN1998
Copy link
Contributor Author

ew note in v3.0.0.rst and a unit test.

Also could you fix this for float types too?

Can you give me an example for float type with expected results? Thank you!

@VISWESWARAN1998
Copy link
Contributor Author

I have added my implementations for supporting float types. But I see in base.py that pandas doesn't support float16 data types. Should I convert them to float64 or just let pandas thow Exception?

base.py:

elif dtype == np.float16:
    # float16 not supported (no indexing engine)
    raise NotImplementedError("float16 indexes are not supported")

For example:

interval_index_float = pd.interval_range(start=np.float32(0.0), end=np.float32(0.6), freq=0.5, closed="left")
print(interval_index_float)

Gives:

IntervalIndex([[0.0, 0.5)], dtype='interval[float32, left]')

But:

interval_index_float = pd.interval_range(start=np.float16(0.0), end=np.float16(0.6), freq=0.5, closed="left")
print(interval_index_float)

will raise:

NotImplementedError: float16 indexes are not supported

I have added implementation to ignore float16 like this and checked-in my changes. If you want to let it throw an exception let me know!

My implementation:

if isinstance(start, float | np.float16) or isinstance(
                end, float | np.float16
            ):
    dtype = np.dtype("float64")
else:
    dtype = start.dtype if start.dtype == end.dtype else np.dtype("float64")
breaks = np.arange(start, end + (freq * 0.1), freq, dtype=dtype)

Current implementation safely returns float64 if it finds float16, Just like current main release!

Copy link
Contributor Author

@VISWESWARAN1998 VISWESWARAN1998 left a comment

Choose a reason for hiding this comment

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

Completed as you asked!

@VISWESWARAN1998
Copy link
Contributor Author

VISWESWARAN1998 commented Feb 14, 2024

Changes have been completed as you asked!

@mroeschke mroeschke added this to the 3.0 milestone Feb 15, 2024
@mroeschke mroeschke merged commit 719e4a6 into pandas-dev:main Feb 15, 2024
@mroeschke
Copy link
Member

Thanks @VISWESWARAN1998

@VISWESWARAN1998
Copy link
Contributor Author

You are most welcome @mroeschke !

pmhatre1 pushed a commit to pmhatre1/pandas-pmhatre1 that referenced this pull request May 7, 2024
…n interval… (pandas-dev#57399)

* Fix for issue pandas-dev#57268 - ENH: Preserve input start/end type in interval_range

* issue pandas-dev#57268 - github actions resolution

* Use generated datatype from breaks

* Ruff - Pre-commit issue fix

* Fix for issue pandas-dev#57268 - floating point support

* int - float dtype compatability

* whatsnew documentation update

* OS based varaible access

* Fixing failed unit test cases

* pytest - interval passsed

* Python backwards compatability

* Pytest

* Fixing PyLint and mypy issues

* dtype specification

* Conditional statement simplification

* remove redundant code blocks

* Changing whatsnew to interval section

* Passing expected in parameterize

* Update doc/source/whatsnew/v3.0.0.rst

Co-authored-by: Matthew Roeschke <[email protected]>

* Update pandas/core/indexes/interval.py

Co-authored-by: Matthew Roeschke <[email protected]>

---------

Co-authored-by: Matthew Roeschke <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions Interval Interval data type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: Preserve input start/end type in interval_range
2 participants