ZetCode

Pandas Series

last modified November 17, 2026 Python 3.15

A Series is a one-dimensional labelled array in Pandas. It is one of the two core Pandas data structures; the other is the DataFrame, which is essentially a collection of Series objects sharing an index. Every element of a Series has a value and a label, and the labels are collectively called the index.

In this article we create Series objects, read their values by label and by position, perform arithmetic that aligns them by their labels, find and remove missing values, and use the .str and .dt accessors for text and dates. The examples were verified with Pandas 3.1.1.

Creating a Series

The pd.Series constructor accepts a list, a dictionary, or a NumPy array. The index parameter supplies the labels, name gives the Series a name, and dtype forces the data type. When labels are not provided, Pandas uses the integers 0, 1, 2, and so on.

series_create.py
import pandas as pd
import numpy as np

s1 = pd.Series([10, 20, 30, 40])
print('from list:')
print(s1)
print()

s2 = pd.Series([10, 20, 30], index=['a', 'b', 'c'], name='values', dtype='float64')
print('custom index, name, dtype:')
print(s2)
print()

s3 = pd.Series({'x': 1, 'y': 2, 'z': 3})
print('from dict:')
print(s3)
print()

s4 = pd.Series(np.array([1.5, 2.5, 3.5]), index=['p', 'q', 'r'])
print('from NumPy array:')
print(s4)

With a dictionary, the keys become the index automatically. The dtype line at the end of each printed Series describes the kind of data it holds; here float64 means 64-bit floating-point numbers and int64 means 64-bit integers. The Name: values line appears only for the Series where we set name.

$ python series_create.py
from list:
0    10
1    20
2    30
3    40
dtype: int64

custom index, name, dtype:
a    10.0
b    20.0
c    30.0
Name: values, dtype: float64

from dict:
x    1
y    2
z    3
dtype: int64

from NumPy array:
p    1.5
q    2.5
r    3.5
dtype: float64

Accessing elements

There are two ways to fetch a value. Label-based access uses [] or the .loc attribute and looks for the index label. Position-based access uses .iloc and counts from zero, just like a Python list. Both styles also work with slices.

series_access.py
import pandas as pd

s = pd.Series([10, 20, 30, 40], index=['a', 'b', 'c', 'd'])

print('by label:')
print(s['b'])
print(s.loc['c'])
print()

print('by position:')
print(s.iloc[0])
print(s.iloc[-1])
print()

print('slice by label:')
print(s['b':'d'])
print()

print('slice by position:')
print(s.iloc[1:3])

The expression s['b'] finds the element whose label is 'b', while s.iloc[0] returns the first element regardless of its label. With string labels, a label slice such as s['b':'d'] includes both endpoints; a position slice such as s.iloc[1:3] includes the start and excludes the end, exactly like ordinary Python slicing. Negative positions count from the end.

$ python series_access.py
by label:
20
30

by position:
10
40

slice by label:
b    20
c    30
d    40
dtype: int64

slice by position:
b    20
c    30
dtype: int64

Arithmetic and alignment

When two Series are combined, Pandas aligns them by their labels rather than by their positions. A label present in only one Series produces a missing value, shown as NaN (not a number). The add method accepts a fill_value that substitutes a number wherever a label is missing.

series_arithmetic.py
import pandas as pd

a = pd.Series([1, 2, 3], index=['x', 'y', 'z'])
b = pd.Series([10, 20, 30], index=['y', 'z', 'w'])

print('a:')
print(a)
print()
print('b:')
print(b)
print()

print('a + b:')
print(a + b)
print()

print('a.add(b, fill_value=0):')
print(a.add(b, fill_value=0))

Only the labels y and z appear in both Series, so a + b has real values only there. The labels x and w become NaN, which also forces the result to the floating-point type. Passing fill_value=0 treats a missing label as zero, so every label gets a value. This label alignment is the key difference between Series arithmetic and ordinary NumPy array arithmetic.

$ python series_arithmetic.py
a:
x    1
y    2
z    3
dtype: int64

b:
y    10
z    20
w    30
dtype: int64

a + b:
w     NaN
x     NaN
y    12.0
z    23.0
dtype: float64

a.add(b, fill_value=0):
w    30.0
x     1.0
y    12.0
z    23.0
dtype: float64

Missing values

Missing data is common in real datasets. The isna method returns a boolean Series that is True where a value is missing, and dropna returns a new Series with those entries removed. Neither method changes the original Series.

series_missing.py
import pandas as pd
import numpy as np

s = pd.Series([1.0, np.nan, 3.0, None, 5.0])
print('original:')
print(s)
print()

print('isna:')
print(s.isna())
print()

print('dropna:')
print(s.dropna())

Both numpy.nan and Python's None are treated as missing, so positions 1 and 3 show NaN in the original Series. The isna result has the bool data type. After dropna, the remaining labels keep their original values (0, 2, and 4) rather than being renumbered. The related article on dropping missing values covers DataFrames in more detail.

$ python series_missing.py
original:
0    1.0
1    NaN
2    3.0
3    NaN
4    5.0
dtype: float64

isna:
0    False
1     True
2    False
3     True
4    False
dtype: bool

dropna:
0    1.0
2    3.0
4    5.0
dtype: float64

String operations with .str

The .str accessor exposes string methods that are applied to every element of a Series. It is only available when the Series holds text. Missing values stay missing unless we tell a method how to handle them, as with na=False below.

series_str.py
import pandas as pd

s = pd.Series(['Apple', 'banana', 'Cherry', None])

print('upper:')
print(s.str.upper())
print()

print("contains 'a':")
print(s.str.contains('a', na=False))
print()

print('length:')
print(s.str.len())

The str.upper method converts each string to uppercase and leaves the missing entry as NaN. The str.contains method tests for a substring; passing na=False makes the missing entry report False instead of NaN. The str.len method counts characters and, because it cannot count a missing value, returns a floating-point Series with NaN in that position. The formatting columns article shows more ways to transform text in a DataFrame.

$ python series_str.py
upper:
0     APPLE
1    BANANA
2    CHERRY
3       NaN
dtype: str

contains 'a':
0    False
1     True
2    False
3    False
dtype: bool

length:
0    5.0
1    6.0
2    6.0
3    NaN
dtype: float64

Date operations with .dt

The .dt accessor works on a Series of datetime values. We first convert strings into real dates with pd.to_datetime, and then read out components such as the year, month, or weekday name.

series_dt.py
import pandas as pd

s = pd.Series(['2026-01-15', '2026-07-04', '2026-11-17'])
d = pd.to_datetime(s)

print('as datetime:')
print(d)
print()

print('year:')
print(d.dt.year)
print()

print('day name:')
print(d.dt.day_name())
print()

print('month:')
print(d.dt.month)

The converted Series has the datetime64[us] data type, meaning dates stored as microseconds since a fixed starting point. Each .dt attribute returns a new Series of the extracted component: integer years and months, and string weekday names. Pandas 3.1.1 uses microsecond resolution by default; other versions may show datetime64[ns], but the accessor results are the same.

$ python series_dt.py
as datetime:
0   2026-01-15
1   2026-07-04
2   2026-11-17
dtype: datetime64[us]

year:
0    2026
1    2026
2    2026
dtype: int32

day name:
0    Thursday
1    Saturday
2     Tuesday
dtype: str

month:
0     1
1     7
2    11
dtype: int32

Relationship to DataFrames

A DataFrame is a two-dimensional table, and each of its columns is a Series. When a DataFrame is built from a dictionary of Series, the Series index is used as the row index. Selecting one column returns a Series whose name is the column name.

series_df.py
import pandas as pd

s = pd.Series(['Alice', 'Bob', 'Charlie'], index=['a', 'b', 'c'], name='Name')
df = pd.DataFrame({'Name': s, 'Age': [30, 25, 35]})

print(df)
print()

col = df['Name']
print('type of df["Name"]:', type(col).__name__)
print('column name:', col.name)
print(col)

The Series s supplies the Name column and its labels a, b, and c become the row labels of the DataFrame. Selecting df['Name'] gives back a Series, as the printed type confirms, and its name is Name. This is why most operations described here also apply to a single DataFrame column. See the Pandas DataFrame article for the table side of the story.

$ python series_df.py
      Name  Age
a    Alice   30
b      Bob   25
c  Charlie   35

type of df["Name"]: Series
column name: Name
a      Alice
b        Bob
c    Charlie
Name: Name, dtype: str

Source

Pandas documentation - Series
Pandas user guide - 10 minutes to pandas
Pandas documentation - Series.str
Pandas documentation - Series.dt

In this article we have worked with the Pandas Series. We created Series from several kinds of input, read values by label and by position, aligned Series in arithmetic, handled missing values with isna and dropna, and used the .str and .dt accessors. See also the Pandas DataFrame, dropping missing values, and formatting columns articles.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Pandas tutorials.