Almost every quant task starts the same way: a file of prices, and a need to look at it before doing anything else. How many rows? Which columns, and of what type? Are the dates dates or just text? Polars answers these in a line each, and the answers come back fast even for a file with millions of rows, because the table is stored column by column in Arrow memory.
The idea in plain words
Where we are. The first topic of the Polars skill and a beginner one. It needs only Python. The next topics transform the columns with expressions, summarise by group, join tables and handle time. If you know pandas, you will find the same ideas with a different style: columns are typed strictly, there is no row index, and work is described as expressions rather than done cell by cell.
A table of named columns, each holding values of one type, with all columns the same length. In Polars the columns are Arrow arrays, there is no row index, and a column on its own is called a Series.
Read a file and look at it
pl.read_csv(path, try_parse_dates=True) reads the file and turns date-looking text into real Date values, which is what lets later topics group by month or join by time. Without the flag the column stays text and everything date-related fails quietly. describe_frame collects the shape and the dtype of each column, the first things to check on any new file.
# The shape and the type of every column of the bars tablerows, columns, fields = describe_frame(df)print(rows, "rows,", columns, "columns")for name, dtype in fields: print(f"{name:8} {dtype}")
33493 rows, 7 columns symbol String date Date open Float64 high Float64 low Float64 close Float64 volume Int64
# Write KO's rows to a CSV and read them back with the dates parseddf.filter(pl.col("symbol") == "KO").write_csv("ko_copy.csv")ko = read_bars_file("ko_copy.csv")print(ko.shape, "| date column type:", ko.schema["date"])print(pl.read_csv("ko_copy.csv").schema["date"], "<- without try_parse_dates")
(6699, 7) | date column type: Date String <- without try_parse_dates
Look at rows and summarise a column
head_rows shows the first rows as tuples, one value per column, which is the quickest way to see whether the data looks right. column_summary takes one column, a Series, and asks it for its minimum, maximum, mean and number of nulls. These are methods on the column itself, computed in compiled code over the whole column at once. Min, max and mean skip nulls, so the null count is how you find out how many values were missing.
# The first two rows, and a summary of the closing pricesfor row in head_rows(df, 2): print(row)summary = column_summary(df, "close")print({key: round(value, 3) if isinstance(value, float) else value for key, value in summary.items()})
('AAPL', datetime.date(2000, 1, 3), 0.785568, 0.842382, 0.761624, 0.83826, 638640787)
('AAPL', datetime.date(2000, 1, 4), 0.810884, 0.828742, 0.758094, 0.76771, 610726329)
{'min': 0.197, 'max': 365.18, 'mean': 61.862, 'nulls': 0}A price column that arrives as String because one cell holds a dash, or a volume that is Float64 because of a stray decimal, will not behave like the numbers you expect, and the errors show up much later. Look at the schema of every new file first. In Polars a column never mixes types, so a wrong dtype is a signal about the data, not something to ignore.
What the tasks ask
- 1`read_bars_file`
pl.read_csv(path, try_parse_dates=True). - 2`describe_frame` and `head_rows`
df.shape,df.schema.items()withstr(dtype);df.head(n).rows(). - 3`column_summary`
df[column]is a Series:.min(),.max(),.mean(),.null_count().
pl.read_csv("bars.csv") with no other options, df.schema["date"] shows String. What is the fix?Lab · what's graded
4 tasks · 100 pts- Read a CSV with dates:
read_bars_file25 pts - Shape and types:
describe_frame25 pts - The first rows:
head_rows25 pts - Summarise a column:
column_summary25 pts