Series Basics
Series with Label Index
Map string labels to values with a dict loop, then look up by label. A Series
with a named index works the same way but also preserves order, supports
arithmetic alignment, and integrates with DataFrames. The trace shows data
filling label by label before lookup isolates a single entry.
By hand
Build data as a dict mapping each label to its value. Look up data['b']
to retrieve the value for label 'b'.
naive.py
Replay: real traced execution (multi-file project)
labels = ['a', 'b', 'c', 'd']
values = [10, 20, 30, 40]
data = {}
for i in range(len(labels)):
data[labels[i]] = values[i]
lookup = data['b']
print('RESULT:', (lookup, values))
labels ← ['a', 'b', 'c', 'd']
1labels = ['a', 'b', 'c', 'd']2values = [10, 20, 30, 40]values this step['a', 'b', 'c', 'd']labelsvalues ← [10, 20, 30, 40]
1labels = ['a', 'b', 'c', 'd']2values = [10, 20, 30, 40]3data = {}values this step[10, 20, 30, 40]valuesdata ← {}
2values = [10, 20, 30, 40]3data = {}4for i in range(len(labels)):values this step{}datai ← 0
3data = {}4for i in range(len(labels)):5 data[labels[i]] = values[i]values this step0idata ← {'a': 10}
4for i in range(len(labels)):5 data[labels[i]] = values[i]6lookup = data['b']values this step{} → {'a': 10}datai ← 1
3data = {}4for i in range(len(labels)):5 data[labels[i]] = values[i]values this step0 → 1idata ← {'a': 10, 'b': 20}
4for i in range(len(labels)):5 data[labels[i]] = values[i]6lookup = data['b']values this step{'a': 10} → {'a': 10, 'b': 20}datai ← 2
3data = {}4for i in range(len(labels)):5 data[labels[i]] = values[i]values this step1 → 2idata ← {'a': 10, 'b': 20, 'c': 30}
4for i in range(len(labels)):5 data[labels[i]] = values[i]6lookup = data['b']values this step{'a': 10, 'b': 20} → {'a': 10, 'b': 20, 'c': 30}datai ← 3
3data = {}4for i in range(len(labels)):5 data[labels[i]] = values[i]values this step2 → 3idata ← {'a': 10, 'b': 20, 'c': 30, 'd': 40}
4for i in range(len(labels)):5 data[labels[i]] = values[i]6lookup = data['b']values this step{'a': 10, 'b': 20, 'c': 30} → {'a': 10, 'b': 20, 'c': 30, 'd': 40}datafor i in range(len(labels)):
3data = {}4for i in range(len(labels)):5 data[labels[i]] = values[i]lookup ← 20
5 data[labels[i]] = values[i]6lookup = data['b']7print('RESULT:', (lookup, values))values this step20lookupstdout ← RESULT: (20, [10, 20, 30, 40])
6lookup = data['b']7print('RESULT:', (lookup, values))values this stepRESULT: (20, [10, 20, 30, 40])stdout
With pandas
pd.Series(values, index=labels) assigns the string labels as the index.
s['b'] retrieves the value by label — the same dict-like lookup, but on a
typed, ordered structure.
library.py
import pandas as pd
from dalib.display import set_display
set_display()
labels = ['a', 'b', 'c', 'd']
values = [10, 20, 30, 40]
s = pd.Series(values, index=labels)
lookup = int(s['b'])
print('index:', s.index.tolist())
print('values:', s.tolist())
print('dtype:', s.dtype)
print('s[b]:', lookup)
print('RESULT:', (lookup, s.tolist()))
index: ['a', 'b', 'c', 'd']
values: [10, 20, 30, 40]
dtype: int64
s[b]: 20
RESULT: (20, [10, 20, 30, 40])
Implementation notes
- A Series goes beyond a plain dict in four ways: values are typed and
homogeneous (single
dtype); the index is an explicitIndexobject that participates in alignment; both label-based (s['b'],s.loc['b']) and positional access (s.iloc[1]) are available; and arithmetic between two Series aligns on index labels automatically (seeseries-arithmetic-align). int(s['b'])converts the returned numpy scalar to a plain Python int for a stable RESULT, avoiding repr differences across numpy versions.- When two Series share the same index, pandas aligns them by label during
arithmetic:
s1 + s2adds elements with matching labels, filling mismatches with NaN. - Index and values are shown via
.tolist()here because rawSeries.__repr__output varies with pandas display options.