@@ -117,7 +117,9 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None, fastpath=False,
117117 if fastpath :
118118 return cls ._simple_new (data , name )
119119
120- from pandas .tseries .period import PeriodIndex
120+ if is_categorical_dtype (data ) or is_categorical_dtype (dtype ):
121+ return CategoricalIndex (data , copy = copy , name = name , ** kwargs )
122+
121123 if isinstance (data , (np .ndarray , Index , ABCSeries )):
122124 if issubclass (data .dtype .type , np .datetime64 ) or is_datetimetz (data ):
123125 from pandas .tseries .index import DatetimeIndex
@@ -137,10 +139,11 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None, fastpath=False,
137139 if dtype is not None :
138140 try :
139141 data = np .array (data , dtype = dtype , copy = copy )
140- except TypeError :
142+ except ( TypeError , ValueError ) :
141143 pass
142144
143145 # maybe coerce to a sub-class
146+ from pandas .tseries .period import PeriodIndex
144147 if isinstance (data , PeriodIndex ):
145148 return PeriodIndex (data , copy = copy , name = name , ** kwargs )
146149 if issubclass (data .dtype .type , np .integer ):
@@ -149,8 +152,6 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None, fastpath=False,
149152 return Float64Index (data , copy = copy , dtype = dtype , name = name )
150153 elif issubclass (data .dtype .type , np .bool ) or is_bool_dtype (data ):
151154 subarr = data .astype ('object' )
152- elif is_categorical_dtype (data ) or is_categorical_dtype (dtype ):
153- return CategoricalIndex (data , copy = copy , name = name , ** kwargs )
154155 else :
155156 subarr = com ._asarray_tuplesafe (data , dtype = object )
156157
@@ -159,8 +160,28 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None, fastpath=False,
159160 if copy :
160161 subarr = subarr .copy ()
161162
162- elif is_categorical_dtype (data ) or is_categorical_dtype (dtype ):
163- return CategoricalIndex (data , copy = copy , name = name , ** kwargs )
163+ if dtype is None :
164+ inferred = lib .infer_dtype (subarr )
165+ if inferred == 'integer' :
166+ return Int64Index (subarr .astype ('i8' ), copy = copy , name = name )
167+ elif inferred in ['floating' , 'mixed-integer-float' ]:
168+ return Float64Index (subarr , copy = copy , name = name )
169+ elif inferred == 'boolean' :
170+ # don't support boolean explicity ATM
171+ pass
172+ elif inferred != 'string' :
173+ if (inferred .startswith ('datetime' ) or
174+ tslib .is_timestamp_array (subarr )):
175+ from pandas .tseries .index import DatetimeIndex
176+ return DatetimeIndex (subarr , copy = copy , name = name , ** kwargs )
177+ elif (inferred .startswith ('timedelta' ) or
178+ lib .is_timedelta_array (subarr )):
179+ from pandas .tseries .tdi import TimedeltaIndex
180+ return TimedeltaIndex (subarr , copy = copy , name = name , ** kwargs )
181+ elif inferred == 'period' :
182+ return PeriodIndex (subarr , name = name , ** kwargs )
183+ return cls ._simple_new (subarr , name )
184+
164185 elif hasattr (data , '__array__' ):
165186 return Index (np .asarray (data ), dtype = dtype , copy = copy , name = name ,
166187 ** kwargs )
@@ -172,9 +193,7 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None, fastpath=False,
172193 # we must be all tuples, otherwise don't construct
173194 # 10697
174195 if all ( isinstance (e , tuple ) for e in data ):
175-
176196 try :
177-
178197 # must be orderable in py3
179198 if compat .PY3 :
180199 sorted (data )
@@ -183,32 +202,9 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None, fastpath=False,
183202 except (TypeError , KeyError ):
184203 # python2 - MultiIndex fails on mixed types
185204 pass
186-
187205 # other iterable of some kind
188206 subarr = com ._asarray_tuplesafe (data , dtype = object )
189-
190- if dtype is None :
191- inferred = lib .infer_dtype (subarr )
192- if inferred == 'integer' :
193- return Int64Index (subarr .astype ('i8' ), copy = copy , name = name )
194- elif inferred in ['floating' , 'mixed-integer-float' ]:
195- return Float64Index (subarr , copy = copy , name = name )
196- elif inferred == 'boolean' :
197- # don't support boolean explicity ATM
198- pass
199- elif inferred != 'string' :
200- if (inferred .startswith ('datetime' ) or
201- tslib .is_timestamp_array (subarr )):
202- from pandas .tseries .index import DatetimeIndex
203- return DatetimeIndex (subarr , copy = copy , name = name , ** kwargs )
204- elif (inferred .startswith ('timedelta' ) or
205- lib .is_timedelta_array (subarr )):
206- from pandas .tseries .tdi import TimedeltaIndex
207- return TimedeltaIndex (subarr , copy = copy , name = name , ** kwargs )
208- elif inferred == 'period' :
209- return PeriodIndex (subarr , name = name , ** kwargs )
210-
211- return cls ._simple_new (subarr , name )
207+ return Index (subarr , dtype = dtype , copy = copy , name = name , ** kwargs )
212208
213209 @classmethod
214210 def _simple_new (cls , values , name = None , dtype = None , ** kwargs ):
0 commit comments