Sub topic 1
DataFrames and Series: the basic containers
Prerequisites
This chapter assumes you know basic Python: functions, lists, dictionaries and method chaining. You should also be comfortable with tabular data (rows, columns, CSV files). Experience with pandas or SQL helps but is not required. Install Polars with pip install polars.
The two containers
Polars stores data in two structures:
- A Series is a one-dimensional, named sequence of values that all share one data type.
- A DataFrame is an ordered collection of Series that all have the same length. Each Series is a column.
Polars is column-oriented. Each column lives in its own contiguous block of memory, so an operation on one column does not touch the others. This layout is the reason the rest of the tutorial talks about "thinking in columns".
Creating a Series
Pass a name and a list of values. Polars infers the data type from the values.
import polars as pl
s = pl.Series("salary", [120, 100, 80, 90])
print(s)
print(s.dtype)
print(s.mean())The name is the first argument. The dtype here is Int64, because all values are integers. A Series method such as mean() returns a plain Python value.
Creating a DataFrame
The most common way is a dictionary that maps column names to lists.
df = pl.DataFrame(
{
"name": ["Ana", "Ben", "Cy", "Dee"],
"dept": ["eng", "eng", "ops", "ops"],
"salary": [120, 100, 80, 90],
}
)
print(df)Polars prints the shape, the column names, the dtype of each column and the first values. Every list must have the same length, or Polars raises an error.
Inspecting a DataFrame
A few attributes answer most first questions about a DataFrame.
| Attribute or method | Result |
|---|---|
| `df.shape` | A tuple of (rows, columns) |
| `df.columns` | A list of column names |
| `df.dtypes` | A list of data types |
| `df.schema` | A mapping of column name to data type |
| `df.head(n)` | The first `n` rows |
print(df.shape)
print(df.columns)
print(df.schema)
print(df["salary"])Indexing with a column name, as in df["salary"], returns a Series. The schema is the contract for the frame: it records the name and dtype of every column.
Getting from a DataFrame to a Series and back
You can pull a column out as a Series with df.get_column("salary"), and you can build a DataFrame from Series with pl.DataFrame([s1, s2]). Because each Series carries its own name, the names become the column names.
names = pl.Series("name", ["Ana", "Ben"])
floors = pl.Series("floor", [3, 1])
small = pl.DataFrame([names, floors])
print(small)Common errors
- Unequal lengths.
pl.DataFrame({"a": [1, 2], "b": [1]})fails because the columns differ in length. - Expecting a row index. Polars has no index. Rows are identified by position only, so there is no
df.locand no index alignment. - Mutating in place. Methods return a new DataFrame. Calling
df.select(...)without assigning the result changes nothing.
Exercise
You build pl.DataFrame({"x": [1, 2, 3], "y": ["a", "b"]}) and Polars raises an error. Why, and how do you fix it?
Show answer
A DataFrame needs all columns to have the same length. Column x has three values and column y has two. Fix it by giving y a third value, for example ["a", "b", "c"], or by removing the extra value from x.
Exercise
Which attribute returns the column names paired with their data types?
Show answer
df.schema.