Creating a Series
Every way to build a Series — from a list, tuple, range, NumPy array, dictionary or a single scalar — plus custom index labels, a name and a forced dtype.
Watch the lesson
1 video on CodeWithMunnaX
A Series is the smallest building block in pandas. Every column of every DataFrame you will ever load is a Series, so knowing exactly how one is built — and what pandas does with your index, name and data type — pays off in every later chapter. Think of it as one column of a scorecard: runs scored by each batter, titles won by each team, economy rate of each bowler.
Definition
A Series is a one-dimensional, labelled array that holds a sequence of values of a single data type (dtype), where every value is paired with a label from the Series' index. It can optionally carry a name.
- One-dimensional — values form a single line (like a list), so a Series has a length but no columns.
- Labelled — every value has a label. If you do not provide labels, pandas uses a RangeIndex:
0, 1, 2, …. - Single data type — all values share one dtype (
int64,float64,str,bool,object…). If you mix types, pandas picks a type that can hold all of them. - Name — an optional title for the Series. When the Series becomes a DataFrame column, the name becomes the column name.
Syntax
python
pd.Series(data=None, index=None, dtype=None, name=None, copy=None)| Parameter | Meaning | Default |
|---|---|---|
data | the values: list, tuple, range, NumPy array, dict, or a single scalar | None → empty Series |
index | labels for the values; must have the same length as data (for dicts: selects keys) | RangeIndex(0 … n-1), or the dict keys |
dtype | force a data type, e.g. "float64", "int64", "str", "category" | inferred from the data |
name | name of the Series (becomes the column name inside a DataFrame) | None |
copy | copy the input data instead of sharing it | None (pandas decides; NumPy input is copied) |
Note:
pd.Serieshas a capital S.pd.series(...)raisesAttributeError.
Example — from a list (default index)
python
import pandas as pd
runs = pd.Series([45, 78, 12, 101, 33])
print(runs)text
0 45
1 78
2 12
3 101
4 33
dtype: int64How it works: pandas printed two columns — the index (0–4, created automatically) on the left and the values on the right — then a footer with the dtype. All values are whole numbers, so pandas chose int64 (64-bit integers). Tuples and ranges work exactly the same way: pd.Series(range(2008, 2012)) gives the values 2008…2011.
Example — custom index and a name
python
runs = pd.Series([45, 78, 12, 101, 33],
index=["Rohit", "Virat", "Gill", "Pant", "Hardik"],
name="runs")
print(runs)text
Rohit 45
Virat 78
Gill 12
Pant 101
Hardik 33
Name: runs, dtype: int64How it works: the index list gives each value a meaningful label, so you can later write runs["Virat"]. The footer now also shows Name: runs. The index and data are matched by position — the first label goes with the first value.
Tip: You can also pass the index as the second positional argument:
pd.Series([10, 20, 30], [2021, 2022, 2023])— the first list is the data, the second is the index. Writingindex=is clearer.
Example — from a NumPy array
python
import numpy as np
arr = np.array([4.5, 7.25, 8.0])
economy = pd.Series(arr, index=["Bumrah", "Shami", "Siraj"])
print(economy)text
Bumrah 4.50
Shami 7.25
Siraj 8.00
dtype: float64The array's dtype (float64) is kept. A Series is essentially "a NumPy array + an index".
Example — from a dictionary
python
titles = pd.Series({"MI": 5, "CSK": 5, "KKR": 3, "SRH": 1})
print(titles)text
MI 5
CSK 5
KKR 3
SRH 1
dtype: int64How it works: the dictionary keys become the index and the values become the data, in insertion order. If you also pass index=, pandas picks those keys — and a label that is not in the dict gets NaN (missing):
python
print(pd.Series({"MI": 5, "CSK": 5, "KKR": 3}, index=["CSK", "MI", "RCB"]))text
CSK 5.0
MI 5.0
RCB NaN
dtype: float64KKR was dropped (not requested), RCB became NaN, and because NaN is a float, the whole Series became float64.
Example — from a scalar
python
print(pd.Series(0, index=["Rohit", "Virat", "Gill"]))text
Rohit 0
Virat 0
Gill 0
dtype: int64A single value is repeated for every label — handy for initialising counters. (An index is required; otherwise you get a 1-element Series.)
How pandas chooses the dtype
| Data you pass | Resulting dtype | Example output |
|---|---|---|
| all integers | int64 | 45, 78 |
any float, or None/NaN among numbers | float64 | [50, None, 30] → 50.0, NaN, 30.0 |
| all text (or text + missing) | str | ["MI", "CSK"] |
True/False only | bool | True, False |
| mixed types (text + numbers) | object | ["Munna Kumar", 23, "CSE"] |
| lists inside a list | object (each value is a list) | [[10, 20], [30, 40]] |
nothing (pd.Series(dtype="float64")) | the dtype you give | Series([], dtype: float64) |
You can override the inferred type with dtype:
python
print(pd.Series([10, 20, 30], dtype="float64"))
print(pd.Series([10, 20, 30], dtype="str"))text
0 10.0
1 20.0
2 30.0
dtype: float64
0 10
1 20
2 30
dtype: strThe second Series looks the same, but its values are now the text "10", "20", "30" — you cannot do maths on them.
Note (pandas 3): text data gets the dedicated
strdtype. Old tutorials showdtype: objectfor the same code. A mixed Series such as["Munna Kumar", 23, "CSE"]is stillobject.
None vs NaN
python
print(pd.Series([50, None, 30]))text
0 50.0
1 NaN
2 30.0
dtype: float64None is Python's "no value" object; NaN ("Not a Number") is the floating-point marker pandas uses for missing numbers. In a numeric Series, pandas converts None to NaN, which forces the dtype to float64 (integers cannot hold NaN).
Common mistakes
- ❌
pd.Series([1, 2, 3], index=["a", "b"])→ValueError: Length of values (3) does not match length of index (2). ✅ The index must have one label per value. - ❌ Expecting integers to stay
int64when a value is missing. ✅ ANaNmakes the Seriesfloat64(45shows as45.0). - ❌
pd.Series({"MI": 5}, index=["mi"])and being surprised byNaN. ✅ With a dict,index=selects keys — labels are case-sensitive. - ❌ Creating with
dtype="str"and then tryings.sum()to add numbers. ✅ Keep numbers numeric; convert only when you need text.
Interview tip
Q: What happens when you create a Series from a dictionary and also pass an index?
A: The index acts as a selector: values are taken from the dict for labels that exist as keys, in the order of the index; labels not found in the dict get NaN, and keys not listed in the index are dropped.
Key points to remember
- pd.Series(data, index, dtype, name) builds a 1D labelled array with a single dtype.
- Without an index, pandas creates a RangeIndex 0, 1, 2, …
- From a dict, keys become the index; passing index= selects keys and fills missing ones with NaN.
- A scalar with an index is repeated for every label.
- Missing numbers (None/NaN) turn an integer Series into float64.
- Text gets the str dtype in pandas 3; mixed types fall back to object.
- The name of a Series becomes its column name inside a DataFrame.
Try it yourself
Series from a list, a NumPy array and a scalar
Series from a dictionary (with and without index)
How pandas picks the dtype
Practice: build your own season Series
Revision Flashcards
Tap a card to flip it and see the answer.
