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

Method from_tuples

pandas/core/indexes/multi.py:515–591  ·  view source on GitHub ↗

Convert list of tuples to MultiIndex. Parameters ---------- tuples : list / sequence of tuple-likes Each tuple is the index of one row/column. sortorder : int or None Level of sortedness (must be lexicographically sorted by that

(
        cls,
        tuples: Iterable[tuple[Hashable, ...]],
        sortorder: int | None = None,
        names: Sequence[Hashable] | Hashable | None = None,
    )

Source from the content-addressed store, hash-verified

513 @classmethod
514 @names_compat
515 def from_tuples(
516 cls,
517 tuples: Iterable[tuple[Hashable, ...]],
518 sortorder: int | None = None,
519 names: Sequence[Hashable] | Hashable | None = None,
520 ) -> MultiIndex:
521 """
522 Convert list of tuples to MultiIndex.
523
524 Parameters
525 ----------
526 tuples : list / sequence of tuple-likes
527 Each tuple is the index of one row/column.
528 sortorder : int or None
529 Level of sortedness (must be lexicographically sorted by that
530 level).
531 names : list / sequence of str, optional
532 Names for the levels in the index.
533
534 Returns
535 -------
536 MultiIndex
537
538 See Also
539 --------
540 MultiIndex.from_arrays : Convert list of arrays to MultiIndex.
541 MultiIndex.from_product : Make a MultiIndex from cartesian product
542 of iterables.
543 MultiIndex.from_frame : Make a MultiIndex from a DataFrame.
544
545 Examples
546 --------
547 >>> tuples = [(1, "red"), (1, "blue"), (2, "red"), (2, "blue")]
548 >>> pd.MultiIndex.from_tuples(tuples, names=("number", "color"))
549 MultiIndex([(1, 'red'),
550 (1, 'blue'),
551 (2, 'red'),
552 (2, 'blue')],
553 names=['number', 'color'])
554 """
555 if not is_list_like(tuples):
556 raise TypeError("Input must be a list / sequence of tuple-likes.")
557 if is_iterator(tuples):
558 tuples = list(tuples)
559 tuples = cast(Collection[tuple[Hashable, ...]], tuples)
560
561 # handling the empty tuple cases
562 if len(tuples) and all(isinstance(e, tuple) and not e for e in tuples):
563 codes = [np.zeros(len(tuples))]
564 levels = [Index(com.asarray_tuplesafe(tuples, dtype=np.dtype("object")))]
565 return cls(
566 levels=levels,
567 codes=codes,
568 sortorder=sortorder,
569 names=names,
570 verify_integrity=False,
571 )
572

Callers 15

setupMethod · 0.45
time_from_tuplesMethod · 0.45
conftest.pyFile · 0.45
index_with_missingFunction · 0.45
create_indexMethod · 0.45
_wrap_resultMethod · 0.45
str_extractallFunction · 0.45
_shallow_copyMethod · 0.45
appendMethod · 0.45
_wrap_reindex_resultMethod · 0.45
_convert_can_do_setopMethod · 0.45

Calls 4

IndexClass · 0.90
clsFunction · 0.85
dtypeMethod · 0.45
from_arraysMethod · 0.45

Tested by 15

index_with_missingFunction · 0.36
test_alignmentMethod · 0.36
test_index_returnedMethod · 0.36
test_get_dummies_indexFunction · 0.36
test_extractallFunction · 0.36