BUG: Index dtype may not be applied properly · pandas-dev/pandas@ead3ca8 (original) (raw)

`@@ -117,7 +117,9 @@ def new(cls, data=None, dtype=None, copy=False, name=None, fastpath=False,

`

117

117

`if fastpath:

`

118

118

`return cls._simple_new(data, name)

`

119

119

``

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

+

121

123

`if isinstance(data, (np.ndarray, Index, ABCSeries)):

`

122

124

`if issubclass(data.dtype.type, np.datetime64) or is_datetimetz(data):

`

123

125

`from pandas.tseries.index import DatetimeIndex

`

`@@ -137,10 +139,11 @@ def new(cls, data=None, dtype=None, copy=False, name=None, fastpath=False,

`

137

139

`if dtype is not None:

`

138

140

`try:

`

139

141

`data = np.array(data, dtype=dtype, copy=copy)

`

140

``

`-

except TypeError:

`

``

142

`+

except (TypeError, ValueError):

`

141

143

`pass

`

142

144

``

143

145

`# maybe coerce to a sub-class

`

``

146

`+

from pandas.tseries.period import PeriodIndex

`

144

147

`if isinstance(data, PeriodIndex):

`

145

148

`return PeriodIndex(data, copy=copy, name=name, **kwargs)

`

146

149

`if issubclass(data.dtype.type, np.integer):

`

`@@ -149,8 +152,6 @@ def new(cls, data=None, dtype=None, copy=False, name=None, fastpath=False,

`

149

152

`return Float64Index(data, copy=copy, dtype=dtype, name=name)

`

150

153

`elif issubclass(data.dtype.type, np.bool) or is_bool_dtype(data):

`

151

154

`subarr = data.astype('object')

`

152

``

`-

elif is_categorical_dtype(data) or is_categorical_dtype(dtype):

`

153

``

`-

return CategoricalIndex(data, copy=copy, name=name, **kwargs)

`

154

155

`else:

`

155

156

`subarr = com._asarray_tuplesafe(data, dtype=object)

`

156

157

``

`@@ -159,8 +160,28 @@ def new(cls, data=None, dtype=None, copy=False, name=None, fastpath=False,

`

159

160

`if copy:

`

160

161

`subarr = subarr.copy()

`

161

162

``

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

+

164

185

`elif hasattr(data, 'array'):

`

165

186

`return Index(np.asarray(data), dtype=dtype, copy=copy, name=name,

`

166

187

`**kwargs)

`

`@@ -172,9 +193,7 @@ def new(cls, data=None, dtype=None, copy=False, name=None, fastpath=False,

`

172

193

`# we must be all tuples, otherwise don't construct

`

173

194

`# 10697

`

174

195

`if all( isinstance(e, tuple) for e in data ):

`

175

``

-

176

196

`try:

`

177

``

-

178

197

`# must be orderable in py3

`

179

198

`if compat.PY3:

`

180

199

`sorted(data)

`

`@@ -183,32 +202,9 @@ def new(cls, data=None, dtype=None, copy=False, name=None, fastpath=False,

`

183

202

`except (TypeError, KeyError):

`

184

203

`# python2 - MultiIndex fails on mixed types

`

185

204

`pass

`

186

``

-

187

205

`# other iterable of some kind

`

188

206

`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)

`

212

208

``

213

209

`@classmethod

`

214

210

`def _simple_new(cls, values, name=None, dtype=None, **kwargs):

`