1
1
""" define the IntervalIndex """
2
2
3
3
import numpy as np
4
- import pandas as pd
5
4
6
5
from pandas .types .missing import notnull , isnull
7
6
from pandas .types .common import (_ensure_platform_int ,
8
7
is_datetime_or_timedelta_dtype ,
9
8
is_integer_dtype ,
9
+ is_object_dtype ,
10
+ is_categorical_dtype ,
10
11
is_float_dtype ,
11
12
is_interval_dtype )
12
13
from pandas .indexes .base import (Index , _ensure_index ,
13
14
default_pprint , _index_shared_docs )
15
+ from pandas .tslib import Timestamp , Timedelta
14
16
from pandas .indexes .multi import MultiIndex
15
17
from pandas .compat .numpy import function as nv
16
18
from pandas .core import common as com
24
26
25
27
def _get_next_label (label ):
26
28
dtype = getattr (label , 'dtype' , type (label ))
27
- if isinstance (label , (pd . Timestamp , pd . Timedelta )):
29
+ if isinstance (label , (Timestamp , Timedelta )):
28
30
dtype = 'datetime64'
29
31
if is_datetime_or_timedelta_dtype (dtype ):
30
32
return label + np .timedelta64 (1 , 'ns' )
@@ -39,7 +41,7 @@ def _get_next_label(label):
39
41
40
42
def _get_prev_label (label ):
41
43
dtype = getattr (label , 'dtype' , type (label ))
42
- if isinstance (label , (pd . Timestamp , pd . Timedelta )):
44
+ if isinstance (label , (Timestamp , Timedelta )):
43
45
dtype = 'datetime64'
44
46
if is_datetime_or_timedelta_dtype (dtype ):
45
47
return label - np .timedelta64 (1 , 'ns' )
@@ -340,6 +342,19 @@ def copy(self, deep=False, name=None):
340
342
name = name if name is not None else self .name
341
343
return self ._shallow_copy (left , right , name = name )
342
344
345
+ @Appender (_index_shared_docs ['astype' ])
346
+ def astype (self , dtype , copy = True ):
347
+ if is_interval_dtype (dtype ):
348
+ if copy :
349
+ self = self .copy ()
350
+ return self
351
+ elif is_object_dtype (dtype ):
352
+ return Index (self .values , dtype = object )
353
+ elif is_categorical_dtype (dtype ):
354
+ from pandas import Categorical
355
+ return Categorical (self , ordered = True )
356
+ raise ValueError ('Cannot cast IntervalIndex to dtype %s' % dtype )
357
+
343
358
@cache_readonly
344
359
def dtype (self ):
345
360
return np .dtype ('O' )
@@ -513,6 +528,26 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
513
528
else :
514
529
return self ._tree .get_indexer (target )
515
530
531
+ def sort_values (self , return_indexer = False , ascending = True ):
532
+ """
533
+ Return sorted copy of Index
534
+ """
535
+ mask = self ._mask
536
+
537
+ # nans are sorted to the highest values
538
+ _as = self .argsort ()
539
+ _as [mask ] = - 1
540
+
541
+ if not ascending :
542
+ _as = _as [::- 1 ]
543
+
544
+ sorted_index = self .take (_as )
545
+
546
+ if return_indexer :
547
+ return sorted_index , _as
548
+ else :
549
+ return sorted_index
550
+
516
551
def where (self , cond , other = None ):
517
552
raise NotImplementedError
518
553
0 commit comments