Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 37 ยท Getting Started

Data Types

Understand Polars data types: integers, floats, strings, booleans, dates, categoricals, lists and structs, plus casting, schemas and choosing smaller types.

Every Polars column has exactly one data type (called a dtype). The type decides how values are stored, which operations are allowed, and how much memory the column uses. Getting types right is the cheapest way to make code both correct and fast, and Polars is strict about them, which prevents many silent bugs.

The main types

Numbers

Int8 to Int64 Signed whole numbers
UInt8 to UInt64 Never negative
Float32, Float64 Decimals

Default: Int64 and Float64

1 / 2
  • Integers: Int8, Int16, Int32, Int64 (signed) and UInt8 to UInt64 (unsigned). Python integers become Int64 by default.
  • Floats: Float32 and Float64. Python floats become Float64.
  • Decimal: Decimal stores exact decimal numbers (useful for money). It is still marked experimental in places, so check the docs of your version before relying on it.
  • String: text. In older versions this was called Utf8; the name String is the current one.
  • Boolean: True/False, plus null.
  • Temporal: Date (a day), Datetime (day and time, optionally with a time zone), Time (time of day) and Duration (a length of time).
  • Categorical and Enum: strings stored as small integer codes with a lookup table.
  • Nested: List (variable length), Array (fixed length) and Struct (named fields in one column).
  • Null and Object: Null is the type of an all-null column; Object wraps arbitrary Python objects and should be avoided.

Type inference

When you give Polars Python values, it infers types from them.

Python Inferred types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A list of ints with one None stays an integer column: Polars treats None as null in any type. A list with ints and floats mixed becomes Float64. A list with numbers and text raises an error, because no single type fits.

Choosing a type on purpose

Inference is convenient, but you can state the schema yourself with schema= (full control) or schema_overrides= (change just a few columns).

Python Explicit schemas

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Smaller integer types use less memory: Int8 takes one byte per value, Int64 takes eight. If you know ids never exceed 65,535, UInt16 is plenty. A value that does not fit (for example 300 in an Int8) raises an error when you build or cast the column; Polars will not silently wrap it.

Casting

cast converts a column from one type to another inside an expression.

Python Casting columns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

By default casting is strict: if any value cannot be converted, you get an error instead of silent damage. With strict=False, unconvertible values become null, which is handy for cleaning messy data when you then count or inspect the nulls. Casting a float to an integer truncates toward zero, it does not round, so use .round() first when you mean rounding.

Categorical and Enum

A column such as country repeats the same few strings thousands of times. A Categorical column stores each distinct string once and keeps a small integer code per row. That saves memory and speeds up grouping and joins. An Enum is the strict cousin: you list the allowed values up front, and anything else raises an error. Enum also gives the categories a fixed order, so sorting follows your order rather than the alphabet.

Python Enum for ordered categories

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Checking and selecting by type

Use the dtype to test or pick columns: df.schema, series.dtype, and the polars.selectors module (covered in Column Selectors) let you say "all numeric columns" without naming them.

Polars never guesses across a mismatch. Adding an Int64 column to a String column is an error, not a concatenation or a hidden conversion. Cast explicitly. It feels strict at first and saves you from subtle bugs later.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What type does Polars infer for the Python list [1, 2, None]?

    None becomes null, and nulls are allowed in every type. The non-null values are integers, so the column is Int64.
  2. 2. What happens by default when cast cannot convert a value, such as the text 'x30' to an integer?

    cast is strict by default. Pass strict=False if you want unconvertible values to become null.
  3. 3. Which type is best for a column of a few repeated labels where you also want a fixed sort order?

    An Enum stores a declared list of allowed strings in a fixed order, so sorting follows your order and invalid values are rejected.
  4. 4. What does casting the float 3.99 to Int64 give?

    Casting to an integer truncates toward zero. Use round() first if you want 4.

Summary

  • Each column has one dtype: integers, floats, String, Boolean, temporal types, Categorical/Enum and nested types.
  • Types are inferred from Python values, or set with schema= and schema_overrides=.
  • cast converts types and is strict unless you pass strict=False.
  • Smaller numeric types and Categorical columns save memory.

Next: Display Settings