Types and Parsing
Cast Numeric Strings
Convert a list of clean numeric strings to integers. By hand, loop and call
int(s) on each element. With pandas, Series.astype(int) converts the
entire column in one call, changing the dtype from object to int64.
By hand
Loop over strings and call int(s) to convert each element, appending to
result. Every iteration shows s being consumed and the growing list of
integers.
naive.py
Replay: real traced execution (multi-file project)
strings = ['12', '7', '45', '3', '28']
result = []
for s in strings:
result.append(int(s))
print('RESULT:', result)
strings ← ['12', '7', '45', '3', '28']
1strings = ['12', '7', '45', '3', '28']2result = []values this step['12', '7', '45', '3', '28']stringsresult ← []
1strings = ['12', '7', '45', '3', '28']2result = []3for s in strings:values this step[]results ← '12'
2result = []3for s in strings:4 result.append(int(s))values this step'12'sresult ← [12]
3for s in strings:4 result.append(int(s))5print('RESULT:', result)values this step[] → [12]results ← '7'
2result = []3for s in strings:4 result.append(int(s))values this step'12' → '7'sresult ← [12, 7]
3for s in strings:4 result.append(int(s))5print('RESULT:', result)values this step[12] → [12, 7]results ← '45'
2result = []3for s in strings:4 result.append(int(s))values this step'7' → '45'sresult ← [12, 7, 45]
3for s in strings:4 result.append(int(s))5print('RESULT:', result)values this step[12, 7] → [12, 7, 45]results ← '3'
2result = []3for s in strings:4 result.append(int(s))values this step'45' → '3'sresult ← [12, 7, 45, 3]
3for s in strings:4 result.append(int(s))5print('RESULT:', result)values this step[12, 7, 45] → [12, 7, 45, 3]results ← '28'
2result = []3for s in strings:4 result.append(int(s))values this step'3' → '28'sresult ← [12, 7, 45, 3, 28]
3for s in strings:4 result.append(int(s))5print('RESULT:', result)values this step[12, 7, 45, 3] → [12, 7, 45, 3, 28]resultfor s in strings:
2result = []3for s in strings:4 result.append(int(s))stdout ← RESULT: [12, 7, 45, 3, 28]
4 result.append(int(s))5print('RESULT:', result)values this stepRESULT: [12, 7, 45, 3, 28]stdout
With pandas
df['x'] starts as dtype object (string column). .astype(int) returns a
new int64 Series. The snapshot shows both dtypes so the conversion is
explicit.
library.py
import pandas as pd
from dalib.display import set_display
set_display()
strings = ['12', '7', '45', '3', '28']
df = pd.DataFrame({'x': strings})
s = df['x'].astype(int)
result = s.tolist()
print('index:', s.index.tolist())
print('dtype before:', df['x'].dtype)
print('dtype after:', s.dtype)
print('RESULT:', result)
index: [0, 1, 2, 3, 4]
dtype before: object
dtype after: int64
RESULT: [12, 7, 45, 3, 28]
Implementation notes
astype(int)raisesValueErroron any non-numeric string — it is a strict cast, not a coercion. Use it only when all values are known to be clean.- To cast to float instead, use
astype(float). For large datasets where memory matters, specify a smaller dtype:astype('int32')orastype('int16'). - Cross-reference:
coerce-bad-numbers(this chapter) for handling strings that may fail conversion without raising.