Skip to content

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

Closed
@jschendel

Description

@jschendel

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]')

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions