Skip to content

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.


clean_index function

clean_index(
    index,
    drop_duplicates=None,
    keep=None,
    drop_redundant=None
)

Clean index.

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().


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,
    clean_index_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,
    mapper,
    strict=True
)

Rename levels in index by mapper.

Mapper can be a single or multiple levels to rename to, or a dictionary that maps old level names to new level names.


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,
    **clean_index_kwargs
)

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


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(
    *args,
    **kwargs
)

Class for grouping except one or more levels.

Superclasses

Inherited members


value class variable

One or more level positions or names.


IndexApplier class

IndexApplier()

Abstract class that can apply a function on an index.

Subclasses


add_levels method

IndexApplier.add_levels(
    *indexes,
    on_top=True,
    drop_duplicates=None,
    keep=None,
    drop_redundant=None,
    **kwargs
)

Append or prepend levels using stack_indexes().

Set on_top to False to stack at bottom.

See IndexApplier.apply_to_index() for other keyword arguments.


apply_to_index method

IndexApplier.apply_to_index(
    apply_func,
    *args,
    **kwargs
)

Apply function apply_func on the index of the instance and return a new instance.


drop_duplicate_levels method

IndexApplier.drop_duplicate_levels(
    keep=None,
    **kwargs
)

Drop any duplicate levels using drop_duplicate_levels().

See IndexApplier.apply_to_index() for other keyword arguments.


drop_levels method

IndexApplier.drop_levels(
    levels,
    strict=True,
    **kwargs
)

Drop levels using drop_levels().

See IndexApplier.apply_to_index() for other keyword arguments.


drop_redundant_levels method

IndexApplier.drop_redundant_levels(
    **kwargs
)

Drop any redundant levels using drop_redundant_levels().

See IndexApplier.apply_to_index() for other keyword arguments.


rename_levels method

IndexApplier.rename_levels(
    mapper,
    strict=True,
    **kwargs
)

Rename levels using rename_levels().

See IndexApplier.apply_to_index() for other keyword arguments.


select_levels method

IndexApplier.select_levels(
    level_names,
    strict=True,
    **kwargs
)

Select levels using select_levels().

See IndexApplier.apply_to_index() for other keyword arguments.