Sub topic 1
Series: one-dimensional labeled data
Prerequisites
This chapter assumes you know basic Python (lists, dictionaries, imports) and can run code in a script or notebook. You need pandas installed. NumPy knowledge helps but is not required.
What a Series is
A Series is a one-dimensional array of values with a label for each value. It has three parts:
- The values, stored in a typed array.
- The Index, which holds one label per value.
- An optional name, which becomes the column name when the Series is placed in a DataFrame.
Every Series has one dtype, the data type of all its values. All values share that dtype, which is what makes arithmetic on a Series fast.
Creating a Series
Pass a list and, optionally, an index and a name.
import pandas as pd
s = pd.Series([10, 20, 30], index=["a", "b", "c"], name="score")
print(s["b"]) # 20, selected by label
print(s.iloc[0]) # 10, selected by position
print(s.dtype) # int64If you leave out index, pandas uses the integers 0, 1, 2, and so on. You can also build a Series from a dictionary. The keys become the index and the values become the data.
import pandas as pd
pop = pd.Series({"Oslo": 0.7, "Lima": 10.7, "Pune": 3.1})
print(pop)
print(pop.index.tolist()) # ['Oslo', 'Lima', 'Pune']Label versus position
A Series can be read in two ways. s["b"] looks up the label "b". s.iloc[0] takes the item at integer position 0. Both return the same kind of result, but they answer different questions.
Prefer .loc for labels and .iloc for positions. Plain s[...] with integers is ambiguous when the index itself holds integers, so it is a common source of bugs.
import pandas as pd
t = pd.Series([5, 6, 7], index=[2, 1, 0])
print(t.loc[0]) # 7, the label 0
print(t.iloc[0]) # 5, the first positionUseful attributes
| Attribute | Meaning |
|---|---|
| `s.values` | The underlying data as an array |
| `s.index` | The Index object |
| `s.dtype` | The single dtype of all values |
| `s.name` | The label used when the Series becomes a column |
| `s.shape` | A tuple such as `(3,)` |
Common errors
- KeyError on a label.
s["z"]fails if"z"is not in the index. Check with"z" in s.index. - Unexpected dtype. Mixing numbers and strings produces the
objectdtype, which is slow and blocks numeric methods. Inspects.dtypeearly. - Integer upcasting. A missing value in an integer list makes pandas store floats, because the default integer dtype cannot hold NaN.
Why the labels matter
The Index is more than decoration. Later chapters show that arithmetic between two Series matches values by label, not by position. Treat the Index as part of the data.
Exercise
Given t = pd.Series([5, 6, 7], index=[2, 1, 0]), what do t.loc[2] and t.iloc[2] return?
Show answer
t.loc[2] returns 5, because the label 2 belongs to the first value. t.iloc[2] returns 7, because position 2 is the last value.
Exercise
Why does pd.Series([1, 2, None]) have the dtype float64 and not int64?
Show answer
The None becomes NaN, a floating-point value. An int64 array cannot hold NaN, so pandas converts all values to float64.