pandas.Index#

class pandas.Index(data=None, dtype=None, copy=False, name=None, tupleize_cols=True)[source]#

Immutable sequence used for indexing and alignment.

The basic object storing axis labels for all pandas objects.

Changed in version 2.0.0: Index can hold all numpy numeric dtypes (except float16). Previously only int64/uint64/float64 dtypes were accepted.

Parameters:
dataarray-like (1-dimensional)
dtypestr, numpy.dtype, or ExtensionDtype, optional

Data type for the output Index. If not specified, this will be inferred from data. See the user guide for more usages.

copybool, default False

Copy input data.

nameobject

Name to be stored in the index.

tupleize_colsbool (default: True)

When True, attempt to create a MultiIndex if possible.

See also

RangeIndex

Index implementing a monotonic integer range.

CategoricalIndex

Index of Categorical s.

MultiIndex

A multi-level, or hierarchical Index.

IntervalIndex

An Index of Interval s.

DatetimeIndex

Index of datetime64 data.

TimedeltaIndex

Index of timedelta64 data.

PeriodIndex

Index of Period data.

Notes

An Index instance can only contain hashable objects. An Index instance can not hold numpy float16 dtype.

Examples

>>> pd.Index([1, 2, 3])
Index([1, 2, 3], dtype='int64')
>>> pd.Index(list('abc'))
Index(['a', 'b', 'c'], dtype='object')
>>> pd.Index([1, 2, 3], dtype="uint8")
Index([1, 2, 3], dtype='uint8')

Attributes

T

Return the transpose, which is by definition self.

array

The ExtensionArray of the data backing this Series or Index.

dtype

Return the dtype object of the underlying data.

empty

has_duplicates

Check if the Index has duplicate values.

hasnans

Return True if there are any NaNs.

inferred_type

Return a string of the type inferred from the values.

is_monotonic_decreasing

Return a boolean if the values are equal or decreasing.

is_monotonic_increasing

Return a boolean if the values are equal or increasing.

is_unique

Return if the index has unique values.

name

Return Index or MultiIndex name.

names

nbytes

Return the number of bytes in the underlying data.

ndim

Number of dimensions of the underlying data, by definition 1.

nlevels

Number of levels.

shape

Return a tuple of the shape of the underlying data.

size

Return the number of elements in the underlying data.

values

Return an array representing the data in the Index.

Methods

all(*args, **kwargs)

Return whether all elements are Truthy.

any(*args, **kwargs)

Return whether any element is Truthy.

append(other)

Append a collection of Index options together.

argmax([axis, skipna])

Return int position of the largest value in the Series.

argmin([axis, skipna])

Return int position of the smallest value in the Series.

argsort(*args, **kwargs)

Return the integer indices that would sort the index.

asof(label)

Return the label from the index, or, if not present, the previous one.

asof_locs(where, mask)

Return the locations (indices) of labels in the index.

astype(dtype[, copy])

Create an Index with values cast to dtypes.

copy([name, deep])

Make a copy of this object.

delete(loc)

Make new Index with passed location(-s) deleted.

diff([periods])

Computes the difference between consecutive values in the Index object.

difference(other[, sort])

Return a new Index with elements of index not in other.

drop(labels[, errors])

Make new Index with passed list of labels deleted.

drop_duplicates(*[, keep])

Return Index with duplicate values removed.

droplevel([level])

Return index with requested level(s) removed.

dropna([how])

Return Index without NA/NaN values.

duplicated([keep])

Indicate duplicate index values.

equals(other)

Determine if two Index object are equal.

factorize([sort, use_na_sentinel])

Encode the object as an enumerated type or categorical variable.

fillna([value, downcast])

Fill NA/NaN values with the specified value.

format([name, formatter, na_rep])

Render a string representation of the Index.

get_indexer(target[, method, limit, tolerance])

Compute indexer and mask for new index given the current index.

get_indexer_for(target)

Guaranteed return of an indexer even when non-unique.

get_indexer_non_unique(target)

Compute indexer and mask for new index given the current index.

get_level_values(level)

Return an Index of values for requested level.

get_loc(key)

Get integer location, slice or boolean mask for requested label.

get_slice_bound(label, side)

Calculate slice bound that corresponds to given label.

groupby(values)

Group the index labels by a given array of values.

holds_integer()

(DEPRECATED) Whether the type is an integer type.

identical(other)

Similar to equals, but checks that object attributes and types are also equal.

infer_objects([copy])

If we have an object dtype, try to infer a non-object dtype.

insert(loc, item)

Make new Index inserting new item at location.

intersection(other[, sort])

Form the intersection of two Index objects.

is_(other)

More flexible, faster check like is but that works through views.

is_boolean()

(DEPRECATED) Check if the Index only consists of booleans.

is_categorical()

(DEPRECATED) Check if the Index holds categorical data.

is_floating()

(DEPRECATED) Check if the Index is a floating type.

is_integer()

(DEPRECATED) Check if the Index only consists of integers.

is_interval()

(DEPRECATED) Check if the Index holds Interval objects.

is_numeric()

(DEPRECATED) Check if the Index only consists of numeric data.

is_object()

(DEPRECATED) Check if the Index is of the object dtype.

isin(values[, level])

Return a boolean array where the index values are in values.

isna()

Detect missing values.

isnull()

Detect missing values.

item()

Return the first element of the underlying data as a Python scalar.

join(other, *[, how, level, ...])

Compute join_index and indexers to conform data structures to the new index.

map(mapper[, na_action])

Map values using an input mapping or function.

max([axis, skipna])

Return the maximum value of the Index.

memory_usage([deep])

Memory usage of the values.

min([axis, skipna])

Return the minimum value of the Index.

notna()

Detect existing (non-missing) values.

notnull()

Detect existing (non-missing) values.

nunique([dropna])

Return number of unique elements in the object.

putmask(mask, value)

Return a new Index of the values set with the mask.

ravel([order])

Return a view on self.

reindex(target[, method, level, limit, ...])

Create index with target's values.

rename(name, *[, inplace])

Alter Index or MultiIndex name.

repeat(repeats[, axis])

Repeat elements of a Index.

round([decimals])

Round each value in the Index to the given number of decimals.

searchsorted(value[, side, sorter])

Find indices where elements should be inserted to maintain order.

set_names(names, *[, level, inplace])

Set Index or MultiIndex name.

shift([periods, freq])

Shift index by desired number of time frequency increments.

slice_indexer([start, end, step])

Compute the slice indexer for input labels and step.

slice_locs([start, end, step])

Compute slice locations for input labels.

sort(*args, **kwargs)

Use sort_values instead.

sort_values(*[, return_indexer, ascending, ...])

Return a sorted copy of the index.

sortlevel([level, ascending, ...])

For internal compatibility with the Index API.

symmetric_difference(other[, result_name, sort])

Compute the symmetric difference of two Index objects.

take(indices[, axis, allow_fill, fill_value])

Return a new Index of the values selected by the indices.

to_flat_index()

Identity method.

to_frame([index, name])

Create a DataFrame with a column containing the Index.

to_list()

Return a list of the values.

to_numpy([dtype, copy, na_value])

A NumPy ndarray representing the values in this Series or Index.

to_series([index, name])

Create a Series with both index and values equal to the index keys.

tolist()

Return a list of the values.

transpose(*args, **kwargs)

Return the transpose, which is by definition self.

union(other[, sort])

Form the union of two Index objects.

unique([level])

Return unique values in the index.

value_counts([normalize, sort, ascending, ...])

Return a Series containing counts of unique values.

view([cls])

where(cond[, other])

Replace values where the condition is False.