Skip to content
New: Star-import, signal unraveling, Python 3.12 support, and more

# indexes module

Functions for working with indexes: index and columns.

They perform operations on index objects, such as stacking, combining, and cleansing MultiIndex levels.

Note

"Index" in pandas context is referred to both index and columns.

## align_arr_indices_nb function

    align_arr_indices_nb(
        a,
        b
    )

Return indices required to align `a` to `b`.

## align_index_to function

    align_index_to(
        index1,
        index2,
        jitted=None
    )

Align `index1` to have the same shape as `index2` if they have any levels in common.

Returns index slice for the aligning.

## align_indexes function

    align_indexes(
        *indexes,
        return_new_index=False,
        **kwargs
    )

Align multiple indexes to each other with align_index_to().

## block_index_product_nb function

    block_index_product_nb(
        block_group_map1,
        block_group_map2,
        factorized1,
        factorized2
    )

Return indices required for building a block-wise Cartesian product of two factorized indexes.

## combine_index_with_keys function

    combine_index_with_keys(
        index,
        keys,
        lens,
        **kwargs
    )

Build keys based on index lengths.

## combine_indexes function

    combine_indexes(
        *indexes,
        **kwargs
    )

Combine each index in `indexes` using Cartesian product.

Keyword arguments will be passed to stack_indexes().

## concat_indexes function

    concat_indexes(
        *indexes,
        index_concat_method='append',
        keys=None,
        index_stack_kwargs=None,
        verify_integrity=True,
        axis=1
    )

Concatenate indexes.

The following index concatenation methods are supported:

- 'append': append one index to another
- 'union': build a union of indexes
- 'pd_concat': convert indexes to Pandas Series or DataFrames and use `pd.concat`
- 'factorize': factorize the concatenated index
- 'factorize_each': factorize each index and concatenate while keeping numbers unique
- 'reset': reset the concatenated index without applying `keys`
- Callable: a custom callable that takes the indexes and returns the concatenated index

Argument `index_concat_method` also accepts a tuple of two options: the second option gets applied if the first one fails.

Use `keys` as an index with the same number of elements as there are indexes to add another index level on top of the concatenated indexes.

If `verify_integrity` is True and `keys` is None, performs various checks depending on the axis.

## cross_index_with function

    cross_index_with(
        index1,
        index2,
        return_new_index=False
    )

Build a Cartesian product of one index with another while taking into account levels they have in common.

Returns index slices for the aligning.

## cross_indexes function

    cross_indexes(
        *indexes,
        return_new_index=False
    )

Cross multiple indexes with cross_index_with().

## drop_duplicate_levels function

    drop_duplicate_levels(
        index,
        keep=None
    )

Drop levels in `index` with the same name and values.

Set `keep` to 'last' to keep last levels, otherwise 'first'.

Set `keep` to None to use the default.

## drop_levels function

    drop_levels(
        index,
        levels,
        strict=True
    )

Drop `levels` in `index` by their name(s)/position(s).

Provide `levels` as an instance of ExceptLevel to drop everything apart from the specified levels.

## drop_redundant_levels function

    drop_redundant_levels(
        index
    )

Drop levels in `index` that either have a single unnamed value or a range from 0 to n.

## find_first_occurrence function

    find_first_occurrence(
        index_value,
        index
    )

Return index of the first occurrence in `index`.

## get_index function

    get_index(
        arg,
        axis
    )

Get index of `arg` by `axis`.

## index_from_values function

    index_from_values(
        values,
        single_value=False,
        name=None
    )

Create a new `pd.Index` with `name` by parsing an iterable `values`.

Each in `values` will correspond to an element in the new index.

## pick_levels function

    pick_levels(
        index,
        required_levels=None,
        optional_levels=None
    )

Pick optional and required levels and return their indices.

Raises an exception if index has less or more levels than expected.

## rename_levels function

    rename_levels(
        index,
        name_dict,
        strict=True
    )

Rename levels in `index` by `name_dict`.

## repeat_index function

    repeat_index(
        index,
        n,
        ignore_ranges=None
    )

Repeat each element in `index` `n` times.

Set `ignore_ranges` to True to ignore indexes of type `pd.RangeIndex`.

## select_levels function

    select_levels(
        index,
        levels,
        strict=True
    )

Build a new index by selecting one or multiple `levels` from `index`.

Provide `levels` as an instance of ExceptLevel to select everything apart from the specified levels.

## stack_indexes function

    stack_indexes(
        *indexes,
        drop_duplicates=None,
        keep=None,
        drop_redundant=None
    )

Stack each index in `indexes` on top of each other, from top to bottom.

Set `drop_duplicates` to True to remove duplicate levels.

For details on `keep`, see drop_duplicate_levels().

Set `drop_redundant` to True to use drop_redundant_levels().

## tile_index function

    tile_index(
        index,
        n,
        ignore_ranges=None
    )

Tile the whole `index` `n` times.

Set `ignore_ranges` to True to ignore indexes of type `pd.RangeIndex`.

## to_any_index function

    to_any_index(
        index_like
    )

Convert any index-like object to an index.

Index objects are kept as-is.

## ExceptLevel class

    ExceptLevel(
        value
    )

Class for grouping except one or more levels.

Method generated by attrs for class ExceptLevel.

### value class variable

One or more level positions or names.

#### Cookie consent

We use cookies to recognize your repeated visits and preferences, as well as to measure the effectiveness of our documentation and whether users find what they're searching for. With your consent, you're helping us to make our documentation better.

- Google Analytics
- GitHub

Manage settings