Convert arrays to MultiIndex. Parameters ---------- arrays : list / sequence of array-likes Each array-like gives one level's value for each data point. len(arrays) is the number of levels. sortorder : int or None Level of
(
cls,
arrays,
sortorder: int | None = None,
names: Sequence[Hashable] | Hashable | lib.NoDefault = lib.no_default,
)
| 441 | |
| 442 | @classmethod |
| 443 | def from_arrays( |
| 444 | cls, |
| 445 | arrays, |
| 446 | sortorder: int | None = None, |
| 447 | names: Sequence[Hashable] | Hashable | lib.NoDefault = lib.no_default, |
| 448 | ) -> MultiIndex: |
| 449 | """ |
| 450 | Convert arrays to MultiIndex. |
| 451 | |
| 452 | Parameters |
| 453 | ---------- |
| 454 | arrays : list / sequence of array-likes |
| 455 | Each array-like gives one level's value for each data point. |
| 456 | len(arrays) is the number of levels. |
| 457 | sortorder : int or None |
| 458 | Level of sortedness (must be lexicographically sorted by that |
| 459 | level). |
| 460 | names : list / sequence of str, optional |
| 461 | Names for the levels in the index. |
| 462 | |
| 463 | Returns |
| 464 | ------- |
| 465 | MultiIndex |
| 466 | |
| 467 | See Also |
| 468 | -------- |
| 469 | MultiIndex.from_tuples : Convert list of tuples to MultiIndex. |
| 470 | MultiIndex.from_product : Make a MultiIndex from cartesian product |
| 471 | of iterables. |
| 472 | MultiIndex.from_frame : Make a MultiIndex from a DataFrame. |
| 473 | |
| 474 | Examples |
| 475 | -------- |
| 476 | >>> arrays = [[1, 1, 2, 2], ["red", "blue", "red", "blue"]] |
| 477 | >>> pd.MultiIndex.from_arrays(arrays, names=("number", "color")) |
| 478 | MultiIndex([(1, 'red'), |
| 479 | (1, 'blue'), |
| 480 | (2, 'red'), |
| 481 | (2, 'blue')], |
| 482 | names=['number', 'color']) |
| 483 | """ |
| 484 | error_msg = "Input must be a list / sequence of array-likes." |
| 485 | if not is_list_like(arrays): |
| 486 | raise TypeError(error_msg) |
| 487 | if is_iterator(arrays): |
| 488 | arrays = list(arrays) |
| 489 | |
| 490 | # Check if elements of array are list-like |
| 491 | for array in arrays: |
| 492 | if not is_list_like(array): |
| 493 | raise TypeError(error_msg) |
| 494 | |
| 495 | # Check if lengths of all arrays are equal or not, |
| 496 | # raise ValueError, if not |
| 497 | for i in range(1, len(arrays)): |
| 498 | if len(arrays[i]) != len(arrays[i - 1]): |
| 499 | raise ValueError("all arrays must be same length") |
| 500 |