Skip to content

Commit e43996f

Browse files
simonjayhawkinsproost
authored andcommitted
TYPING: --check-untyped-defs for Index.__new__ (pandas-dev#28141)
1 parent c1c2c10 commit e43996f

File tree

1 file changed

+14
-42
lines changed

1 file changed

+14
-42
lines changed

pandas/core/indexes/base.py

+14-42
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pandas._libs.join as libjoin
1111
from pandas._libs.lib import is_datetime_array
1212
from pandas._libs.tslibs import OutOfBoundsDatetime, Timestamp
13+
from pandas._libs.tslibs.period import IncompatibleFrequency
1314
from pandas._libs.tslibs.timezones import tz_compare
1415
from pandas.compat import set_function_name
1516
from pandas.compat.numpy import function as nv
@@ -262,7 +263,13 @@ def __new__(
262263
fastpath=None,
263264
tupleize_cols=True,
264265
**kwargs
265-
):
266+
) -> "Index":
267+
268+
from .range import RangeIndex
269+
from pandas import PeriodIndex, DatetimeIndex, TimedeltaIndex
270+
from .numeric import Float64Index, Int64Index, UInt64Index
271+
from .interval import IntervalIndex
272+
from .category import CategoricalIndex
266273

267274
if name is None and hasattr(data, "name"):
268275
name = data.name
@@ -277,8 +284,6 @@ def __new__(
277284
if fastpath:
278285
return cls._simple_new(data, name)
279286

280-
from .range import RangeIndex
281-
282287
if isinstance(data, ABCPandasArray):
283288
# ensure users don't accidentally put a PandasArray in an index.
284289
data = data.to_numpy()
@@ -291,16 +296,12 @@ def __new__(
291296

292297
# categorical
293298
elif is_categorical_dtype(data) or is_categorical_dtype(dtype):
294-
from .category import CategoricalIndex
295-
296299
return CategoricalIndex(data, dtype=dtype, copy=copy, name=name, **kwargs)
297300

298301
# interval
299302
elif (
300303
is_interval_dtype(data) or is_interval_dtype(dtype)
301304
) and not is_object_dtype(dtype):
302-
from .interval import IntervalIndex
303-
304305
closed = kwargs.get("closed", None)
305306
return IntervalIndex(data, dtype=dtype, name=name, copy=copy, closed=closed)
306307

@@ -309,42 +310,31 @@ def __new__(
309310
or is_datetime64_any_dtype(dtype)
310311
or "tz" in kwargs
311312
):
312-
from pandas import DatetimeIndex
313-
314313
if is_dtype_equal(_o_dtype, dtype):
315314
# GH#23524 passing `dtype=object` to DatetimeIndex is invalid,
316315
# will raise in the where `data` is already tz-aware. So
317316
# we leave it out of this step and cast to object-dtype after
318317
# the DatetimeIndex construction.
319318
# Note we can pass copy=False because the .astype below
320319
# will always make a copy
321-
result = DatetimeIndex(data, copy=False, name=name, **kwargs)
320+
result = DatetimeIndex(
321+
data, copy=False, name=name, **kwargs
322+
) # type: "Index"
322323
return result.astype(object)
323324
else:
324-
result = DatetimeIndex(
325-
data, copy=copy, name=name, dtype=dtype, **kwargs
326-
)
327-
return result
325+
return DatetimeIndex(data, copy=copy, name=name, dtype=dtype, **kwargs)
328326

329327
elif is_timedelta64_dtype(data) or is_timedelta64_dtype(dtype):
330-
from pandas import TimedeltaIndex
331-
332328
if is_dtype_equal(_o_dtype, dtype):
333329
# Note we can pass copy=False because the .astype below
334330
# will always make a copy
335331
result = TimedeltaIndex(data, copy=False, name=name, **kwargs)
336332
return result.astype(object)
337333
else:
338-
result = TimedeltaIndex(
339-
data, copy=copy, name=name, dtype=dtype, **kwargs
340-
)
341-
return result
334+
return TimedeltaIndex(data, copy=copy, name=name, dtype=dtype, **kwargs)
342335

343336
elif is_period_dtype(data) and not is_object_dtype(dtype):
344-
from pandas import PeriodIndex
345-
346-
result = PeriodIndex(data, copy=copy, name=name, **kwargs)
347-
return result
337+
return PeriodIndex(data, copy=copy, name=name, **kwargs)
348338

349339
# extension dtype
350340
elif is_extension_array_dtype(data) or is_extension_array_dtype(dtype):
@@ -387,8 +377,6 @@ def __new__(
387377
pass
388378

389379
# Return an actual float index.
390-
from .numeric import Float64Index
391-
392380
return Float64Index(data, copy=copy, dtype=dtype, name=name)
393381

394382
elif inferred == "string":
@@ -405,19 +393,11 @@ def __new__(
405393
data = np.array(data, dtype=dtype, copy=copy)
406394

407395
# maybe coerce to a sub-class
408-
from pandas.core.indexes.period import PeriodIndex, IncompatibleFrequency
409-
410396
if is_signed_integer_dtype(data.dtype):
411-
from .numeric import Int64Index
412-
413397
return Int64Index(data, copy=copy, dtype=dtype, name=name)
414398
elif is_unsigned_integer_dtype(data.dtype):
415-
from .numeric import UInt64Index
416-
417399
return UInt64Index(data, copy=copy, dtype=dtype, name=name)
418400
elif is_float_dtype(data.dtype):
419-
from .numeric import Float64Index
420-
421401
return Float64Index(data, copy=copy, dtype=dtype, name=name)
422402
elif issubclass(data.dtype.type, np.bool) or is_bool_dtype(data):
423403
subarr = data.astype("object")
@@ -440,12 +420,8 @@ def __new__(
440420
return Index(subarr, copy=copy, dtype=object, name=name)
441421
elif inferred in ["floating", "mixed-integer-float", "integer-na"]:
442422
# TODO: Returns IntegerArray for integer-na case in the future
443-
from .numeric import Float64Index
444-
445423
return Float64Index(subarr, copy=copy, name=name)
446424
elif inferred == "interval":
447-
from .interval import IntervalIndex
448-
449425
try:
450426
return IntervalIndex(subarr, name=name, copy=copy)
451427
except ValueError:
@@ -456,8 +432,6 @@ def __new__(
456432
pass
457433
elif inferred != "string":
458434
if inferred.startswith("datetime"):
459-
from pandas import DatetimeIndex
460-
461435
try:
462436
return DatetimeIndex(subarr, copy=copy, name=name, **kwargs)
463437
except (ValueError, OutOfBoundsDatetime):
@@ -467,8 +441,6 @@ def __new__(
467441
pass
468442

469443
elif inferred.startswith("timedelta"):
470-
from pandas import TimedeltaIndex
471-
472444
return TimedeltaIndex(subarr, copy=copy, name=name, **kwargs)
473445
elif inferred == "period":
474446
try:

0 commit comments

Comments
 (0)