Reading all data in a csv to any other type than pl.String likely fails with a lot of null values. We can use expressions to declare how we want to deal with those null values.
If you read a csv with infer_schema_length=0, polars does not know the schema and will read all columns as pl.String as that is a super type of all polars types.
When read as String we can use expressions to cast all columns.
(pl.read_csv("test.csv", infer_schema_length=0)
.with_columns(pl.all().cast(pl.Int32, strict=False))
Update: infer_schema=False was added in 1.2.0 as a more user-friendly name for this feature.
pl.read_csv("test.csv", infer_schema=False) # read all as pl.String
Answer from ritchie46 on Stack OverflowReading all data in a csv to any other type than pl.String likely fails with a lot of null values. We can use expressions to declare how we want to deal with those null values.
If you read a csv with infer_schema_length=0, polars does not know the schema and will read all columns as pl.String as that is a super type of all polars types.
When read as String we can use expressions to cast all columns.
(pl.read_csv("test.csv", infer_schema_length=0)
.with_columns(pl.all().cast(pl.Int32, strict=False))
Update: infer_schema=False was added in 1.2.0 as a more user-friendly name for this feature.
pl.read_csv("test.csv", infer_schema=False) # read all as pl.String
If you want to read all columns as str (pl.String in polars) set infer_schema=False as polars uses string as default type when reading csvs.
pl.read_csv('sample.csv', infer_schema=False)
This is the TLDR of ritchie46's more detailed answer. I broke it out into a separate answer as his code snippet solves the general case for any datatype and not the special but common case of reading all as strings.