Every Series and DataFrame has an Index object attached, whether it's the default RangeIndex (0, 1, 2, ...) or a custom one built from dates, strings, or other labels. The Index is immutable once created — you can't modify individual labels in place, only replace the whole index — and it's backed by a hash table internally, which is what makes label-based lookups, like looking up a row by its label, and automatic alignment between two differently-ordered Series or DataFrames fast rather than requiring a linear scan.
1Understanding pd.Index()
Every Series and DataFrame has an Index object attached, whether it's the default RangeIndex (0, 1, 2, ...) or a custom one built from dates, strings, or other labels. The Index is immutable once created — you can't modify individual labels in place, only replace the whole index — and it's backed by a hash table internally, which is what makes label-based lookups, like looking up a row by its label, and automatic alignment between two differently-ordered Series or DataFrames fast rather than requiring a linear scan.
Pandas automatically aligns operations between two Series/DataFrames by matching their index labels, not their positional order — if you add two Series with mismatched or reordered indices, pandas lines up matching labels first, which can silently introduce NaN for labels that only exist in one of them.
import pandas as pd
s = pd.Series([1, 2, 3], index=["x", "y", "z"])
print(s.index)2Practical Example
Here is a real-world application of pd.Index() showing how it is used in production Pandas code.
import pandas as pd
a = pd.Series([1, 2, 3], index=["x", "y", "z"])
b = pd.Series([10, 20, 30], index=["y", "z", "w"])
print(a + b)3Best Practices
Follow these guidelines when working with pd.Index():
1. Set a meaningful custom index, like a date or ID column, when that column is naturally how you'll look up or join data, instead of relying only on the default RangeIndex
2. Be aware that operations between two Series/DataFrames align by index label, not position, since mismatched indices produce NaN for unmatched labels rather than raising an error
3. Use df.reset_index() when you need to discard a custom index and go back to default integer positions, rather than manually rebuilding the DataFrame
Tip: Pandas automatically aligns operations between two Series/DataFrames by matching their index labels, not their positional order — if you add two Series with mismatched or reordered indices, pandas lines up matching labels first, which can silently introduce NaN for labels that only exist in one of them.
import pandas as pd
s = pd.Series([1, 2, 3], index=["x", "y", "z"])
print(s.index)