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

Method append

pandas/core/indexes/multi.py:2566–2640  ·  view source on GitHub ↗

Append a collection of Index options together. The `append` method is used to combine multiple `Index` objects into a single `Index`. This is particularly useful when dealing with multi-level indexing (MultiIndex) where you might need to concatenate different levels

(self, other)

Source from the content-addressed store, hash-verified

2564 )
2565
2566 def append(self, other):
2567 """
2568 Append a collection of Index options together.
2569
2570 The `append` method is used to combine multiple `Index` objects into a single
2571 `Index`. This is particularly useful when dealing with multi-level indexing
2572 (MultiIndex) where you might need to concatenate different levels of indices.
2573 The method handles the alignment of the levels and codes of the indices being
2574 appended to ensure consistency in the resulting `MultiIndex`.
2575
2576 Parameters
2577 ----------
2578 other : Index or list/tuple of indices
2579 Index or list/tuple of Index objects to be appended.
2580
2581 Returns
2582 -------
2583 Index
2584 The combined index.
2585
2586 See Also
2587 --------
2588 MultiIndex: A multi-level, or hierarchical, index object for pandas objects.
2589 Index.append : Append a collection of Index options together.
2590 concat : Concatenate pandas objects along a particular axis.
2591
2592 Examples
2593 --------
2594 >>> mi = pd.MultiIndex.from_arrays([["a"], ["b"]])
2595 >>> mi
2596 MultiIndex([('a', 'b')],
2597 )
2598 >>> mi.append(mi)
2599 MultiIndex([('a', 'b'), ('a', 'b')],
2600 )
2601 """
2602 if not isinstance(other, (list, tuple)):
2603 other = [other]
2604
2605 if all(
2606 (isinstance(o, MultiIndex) and o.nlevels >= self.nlevels) for o in other
2607 ):
2608 codes = []
2609 levels = []
2610 names = []
2611 for i in range(self.nlevels):
2612 level_values = self.levels[i]
2613 for mi in other:
2614 level_values = level_values.union(mi.levels[i])
2615 level_codes = [
2616 recode_for_categories(
2617 mi.codes[i], mi.levels[i], level_values, copy=False
2618 )
2619 for mi in ([self, *other])
2620 ]
2621 level_name = self.names[i]
2622 if any(mi.names[i] != level_name for mi in other):
2623 level_name = None

Callers 14

_unionMethod · 0.95
_verify_integrityMethod · 0.45
_valuesMethod · 0.45
_get_values_for_csvMethod · 0.45
_format_multiMethod · 0.45
remove_unused_levelsMethod · 0.45
__getitem__Method · 0.45
takeMethod · 0.45
dropMethod · 0.45
_maybe_match_namesMethod · 0.45
putmaskMethod · 0.45

Calls 5

recode_for_categoriesFunction · 0.90
IndexClass · 0.90
MultiIndexClass · 0.85
unionMethod · 0.45
from_tuplesMethod · 0.45

Tested by

no test coverage detected