Series Basics
Series from List
Build a labeled column by pairing each value in a list with its positional index. A loop creates (index, value) pairs explicitly, making the two-part structure of a Series concrete before the one-call pandas version.
By hand
Loop over each index i from 0 to len(values) - 1, building a list of
[i, value] pairs. Each pair represents one entry in the labeled column —
a position label and the value at that position.
naive.py
Replay: real traced execution (multi-file project)
values = [10, 20, 30, 40, 50]
pairs = []
for i in range(len(values)):
pairs.append([i, values[i]])
print('RESULT:', values)
values ← [10, 20, 30, 40, 50]
1values = [10, 20, 30, 40, 50]2pairs = []values this step[10, 20, 30, 40, 50]valuespairs ← []
1values = [10, 20, 30, 40, 50]2pairs = []3for i in range(len(values)):values this step[]pairsi ← 0
2pairs = []3for i in range(len(values)):4 pairs.append([i, values[i]])values this step0ipairs ← [[0, 10]]
3for i in range(len(values)):4 pairs.append([i, values[i]])5print('RESULT:', values)values this step[] → [[0, 10]]pairsi ← 1
2pairs = []3for i in range(len(values)):4 pairs.append([i, values[i]])values this step0 → 1ipairs ← [[0, 10], [1, 20]]
3for i in range(len(values)):4 pairs.append([i, values[i]])5print('RESULT:', values)values this step[[0, 10]] → [[0, 10], [1, 20]]pairsi ← 2
2pairs = []3for i in range(len(values)):4 pairs.append([i, values[i]])values this step1 → 2ipairs ← [[0, 10], [1, 20], [2, 30]]
3for i in range(len(values)):4 pairs.append([i, values[i]])5print('RESULT:', values)values this step[[0, 10], [1, 20]] → [[0, 10], [1, 20], [2, 30]]pairsi ← 3
2pairs = []3for i in range(len(values)):4 pairs.append([i, values[i]])values this step2 → 3ipairs ← [[0, 10], [1, 20], [2, 30], [3, 40]]
3for i in range(len(values)):4 pairs.append([i, values[i]])5print('RESULT:', values)values this step[[0, 10], [1, 20], [2, 30]] → [[0, 10], [1, 20], [2, 30], [3, 40]]pairsi ← 4
2pairs = []3for i in range(len(values)):4 pairs.append([i, values[i]])values this step3 → 4ipairs ← [[0, 10], [1, 20], [2, 30], [3, 40], [4, 50]]
3for i in range(len(values)):4 pairs.append([i, values[i]])5print('RESULT:', values)values this step[[0, 10], [1, 20], [2, 30], [3, 40]] → [[0, 10], [1, 20], [2, 30], [3, 40], [4, 50]]pairsfor i in range(len(values)):
2pairs = []3for i in range(len(values)):4 pairs.append([i, values[i]])stdout ← RESULT: [10, 20, 30, 40, 50]
4 pairs.append([i, values[i]])5print('RESULT:', values)values this stepRESULT: [10, 20, 30, 40, 50]stdout
With pandas
pd.Series(values) wraps the list in a Series with a default RangeIndex
(0, 1, 2, ...). The snapshot shows the index, values, and dtype as stable
labeled lines.
library.py
import pandas as pd
from dalib.display import set_display
set_display()
values = [10, 20, 30, 40, 50]
s = pd.Series(values)
print('index:', s.index.tolist())
print('values:', s.tolist())
print('dtype:', s.dtype)
print('RESULT:', s.tolist())
index: [0, 1, 2, 3, 4]
values: [10, 20, 30, 40, 50]
dtype: int64
RESULT: [10, 20, 30, 40, 50]
Implementation notes
- A Series has two components: an index (the labels) and a values
array. The default index is
RangeIndex(0, n), equivalent to the positional indices built in the hand-written loop. s.tolist()converts the values array to a plain Python list, giving a stable RESULT independent of pandas display options.s.dtypeis the dtype of the values array — the same numpy dtype system used in the NumPy chapters. Pandas stores column data as numpy arrays internally.- Index and values are shown via
.tolist()here because rawSeries.__repr__output varies with pandas display options (terminal width, float format, etc).