Skip to content

Allow IntervalIndex.astype to change subtypes when passed an IntervalDtype #19197

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

Closed
jschendel opened this issue Jan 12, 2018 · 0 comments · Fixed by #19231
Closed

Allow IntervalIndex.astype to change subtypes when passed an IntervalDtype #19197

jschendel opened this issue Jan 12, 2018 · 0 comments · Fixed by #19231
Labels
Milestone

Comments

@jschendel
Copy link
Member

Problem description

Currently IntervalIndex.astype doesn't do anything when passed an IntervalDtype:

In [2]: ii = pd.interval_range(0.0, 3.0)

In [3]: ii
Out[3]:
IntervalIndex([(0.0, 1.0], (1.0, 2.0], (2.0, 3.0]]
              closed='right',
              dtype='interval[float64]')

In [4]: dtype = IntervalDtype('int64')

In [5]: ii.astype(dtype)
Out[5]:
IntervalIndex([(0.0, 1.0], (1.0, 2.0], (2.0, 3.0]]
              closed='right',
              dtype='interval[float64]')

This is because the current implementation of IntervalIndex.astype doesn't distinguish between different IntervalDtype, and treats them all as equivalent to the existing dtype:

def astype(self, dtype, copy=True):
if is_interval_dtype(dtype):
return self.copy() if copy else self

Expected Output

I'd expect the subtype to be converted to 'int64':

Out[5]:
IntervalIndex([(0, 1], (1, 2], (2, 3]]
              closed='right',
              dtype='interval[int64]')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants