MCPcopy
hub / github.com/pandas-dev/pandas / __new__

Method __new__

pandas/core/indexes/base.py:486–592  ·  view source on GitHub ↗
(
        cls,
        data=None,
        dtype=None,
        copy: bool | None = None,
        name=None,
        tupleize_cols: bool = True,
    )

Source from the content-addressed store, hash-verified

484 # Constructors
485
486 def __new__(
487 cls,
488 data=None,
489 dtype=None,
490 copy: bool | None = None,
491 name=None,
492 tupleize_cols: bool = True,
493 ) -> Self:
494 from pandas.core.indexes.range import RangeIndex
495
496 name = maybe_extract_name(name, data, cls)
497
498 if dtype is not None:
499 dtype = pandas_dtype(dtype)
500
501 data_dtype = getattr(data, "dtype", None)
502
503 refs = None
504 if not copy and isinstance(data, (ABCSeries, Index)):
505 refs = data._references
506
507 # GH 63306, GH 63388
508 data, copy = cls._maybe_copy_array_input(data, copy, dtype)
509
510 # range
511 if isinstance(data, (range, RangeIndex)):
512 result = RangeIndex(start=data, copy=bool(copy), name=name)
513 if dtype is not None:
514 return result.astype(dtype, copy=False)
515 # error: Incompatible return value type (got "MultiIndex",
516 # expected "Self")
517 return result # type: ignore[return-value]
518
519 elif is_ea_or_datetimelike_dtype(dtype):
520 # non-EA dtype indexes have special casting logic, so we punt here
521 if isinstance(data, (set, frozenset)):
522 data = list(data)
523
524 elif is_ea_or_datetimelike_dtype(data_dtype):
525 pass
526
527 elif isinstance(data, (np.ndarray, ABCMultiIndex)):
528 if isinstance(data, ABCMultiIndex):
529 data = data._values
530
531 if data.dtype.kind not in "iufcbmM":
532 # GH#11836 we need to avoid having numpy coerce
533 # things that look like ints/floats to ints unless
534 # they are actually ints, e.g. '0' and 0.0
535 # should not be coerced
536 data = com.asarray_tuplesafe(data, dtype=_dtype_obj)
537 elif isinstance(data, (ABCSeries, Index)):
538 # GH 56244: Avoid potential inference on object types
539 pass
540 elif is_scalar(data):
541 raise cls._raise_scalar_data_error(data)
542 elif hasattr(data, "__array__"):
543 return cls(np.asarray(data), dtype=dtype, copy=copy, name=name)

Callers 5

load_reduceMethod · 0.45
load_newobjMethod · 0.45
_from_mgrMethod · 0.45
_new_IndexFunction · 0.45
_simple_newMethod · 0.45

Calls 15

pandas_dtypeFunction · 0.90
RangeIndexClass · 0.90
sanitize_arrayFunction · 0.90
maybe_extract_nameFunction · 0.85
clsFunction · 0.85
_dtype_to_subclassMethod · 0.80
_ensure_arrayMethod · 0.80
astypeMethod · 0.45

Tested by

no test coverage detected