pandas.MultiIndex.copy#
- MultiIndex.copy(names=None, deep=False, name=None)[source]#
Make a copy of this object. Names, dtype, levels and codes can be passed and will be set on new copy.
The copy method provides a mechanism to create a duplicate of an existing MultiIndex object. This is particularly useful in scenarios where modifications are required on an index, but the original MultiIndex should remain unchanged. By specifying the deep parameter, users can control whether the copy should be a deep or shallow copy, providing flexibility depending on the size and complexity of the MultiIndex.
- Parameters:
- namessequence, optional
Names to set on the new MultiIndex object.
- deepbool, default False
If False, the new object will be a shallow copy. If True, a deep copy will be attempted. Deep copying can be potentially expensive for large MultiIndex objects.
- nameLabel
Kept for compatibility with 1-dimensional Index. Should not be used.
- Returns:
- MultiIndex
A new MultiIndex object with the specified modifications.
See also
MultiIndex.from_arrays
Convert arrays to MultiIndex.
MultiIndex.from_tuples
Convert list of tuples to MultiIndex.
MultiIndex.from_frame
Convert DataFrame to MultiIndex.
Notes
In most cases, there should be no functional difference from using
deep
, but ifdeep
is passed it will attempt to deepcopy. This could be potentially expensive on large MultiIndex objects.Examples
>>> mi = pd.MultiIndex.from_arrays([["a"], ["b"], ["c"]]) >>> mi MultiIndex([('a', 'b', 'c')], ) >>> mi.copy() MultiIndex([('a', 'b', 'c')], )