Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify column types in python polars.read_csv()

While reading a csv file in pandas, we have option of dtype in read_csv(). Do we have similar option in polars?

import pandas as pd
import polars as pl

data_pd = pd.read_csv('file.csv',dtype={'col1':str, 'col2':str})
data_pl = pl.read_csv('file.csv',dtype={'col1':str, 'col2':str})

I get the following Polars error:

TypeError: read_csv() got an unexpected keyword argument 'dtype'
like image 359
Outlier Avatar asked Sep 02 '25 14:09

Outlier


1 Answers

The right way of choosing the data types when reading a CSV is using schema_overrides

Here is an example of how to use it:

df = pl.read_csv('file.csv', schema_overrides=[pl.String, pl.String])
like image 164
Luca Avatar answered Sep 05 '25 04:09

Luca