-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
Closed
Closed
Copy link
Labels
Milestone
Description
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:
pandas/pandas/core/indexes/interval.py
Lines 700 to 702 in 8acdf80
| 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]')