Skip to content

ENH: Generalize RangeIndex to support floats #46484

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

Open
TomAugspurger opened this issue Mar 23, 2022 · 4 comments
Open

ENH: Generalize RangeIndex to support floats #46484

TomAugspurger opened this issue Mar 23, 2022 · 4 comments
Assignees
Labels
Enhancement Index Related to the Index class or subclasses

Comments

@TomAugspurger
Copy link
Contributor

Is your feature request related to a problem?

When working with geospatial data in xarray, it's common to have regularly-spaced, floating-point coordinates. Using a RangeIndex with floating-point start, stop, and step would cut down on the memory usage. https://github.com/gjoseph92/stackstac/blob/1c6553a5b6b2acbb89388bd8b1c2d280ea2f3bd5/stackstac/prepare.py#L412

Describe the solution you'd like

Let RangeIndex accept floating point start, stop, and step. Will need to change a handful of other methods to support the generalization (e.g. inferred_type, probably some of the hashtable stuff).

API breaking implications

None.

Describe alternatives you've considered

Reimplementing all of the index classes to support an "encoding" scheme, which would subsume range encoding, categorical encoding, ... But that's a ton of work :)

Additional context

>>> pd.RangeIndex(0.0, 1.0, 0.1)
RangeIndex(start=0.0, stop=1.0, step=0.1)  # same valuesas np.arange(0.0, 1.0, 0.1)
@TomAugspurger TomAugspurger added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 23, 2022
@TomAugspurger TomAugspurger changed the title ENH: Generalize RangeIndex to support float ENH: Generalize RangeIndex to support floats Mar 23, 2022
@marianna13
Copy link

marianna13 commented Mar 23, 2022

Hi! In order to make this happen I think we should modify Python's built-in function range.

@jbrockmendel
Copy link
Member

+1 on the idea.

FWIW A while back I tried to introduce a RangeEngine to allow for getting more code out of RangeIndex. Ran into a problem that cython doesn't recognize 'range' as a type, xref cython/cython#4040

@rhshadrach rhshadrach added Index Related to the Index class or subclasses and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 24, 2022
@marianna13
Copy link

marianna13 commented Mar 24, 2022

This modification of range function works with floats:

class float_range():
  def __init__(self, start=0, stop=None, step=1):
    self.start = start
    self.current = start - step
    self.stop = stop
    self.step = step
  
  def __iter__(self):
    return self

  def __next__(self):
    self.current += self.step
    if self.current < self.stop:
      return self.current
    raise StopIteration

Example:

rng = float_range(0, 2, 0.5)
for r in rng:
  print(r)

Output:

0.0
0.5
1.0
1.5

marianna13 added a commit to marianna13/pandas that referenced this issue Mar 28, 2022
@kapiliyer
Copy link
Contributor

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Index Related to the Index class or subclasses
Projects
None yet
Development

No branches or pull requests

5 participants