Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One possible solution to this issue is to explicitly define the data types of each column using dtype parameter in read_sql query. By doing so, you can avoid any data type inference errors and make sure that the columns are correctly casted to the desired data type.

For example, let's say you have a table named mytable with two columns id and name. You can read this table using the following query:

import polars as pl
import pandas as pd
import sqlalchemy

engine = sqlalchemy.create_engine('postgresql://user:password@host:port/database')
query = 'SELECT * FROM mytable'
df = pd.read_sql_query(query, engine)
pl_df = pl.from_pandas(df, None)

If you face an error related to binary data, you can add a dtype parameter to the read_sql_query function to explicitly define the data types of each column, like this:

import polars as pl
import pandas as pd
import sqlalchemy

engine = sqlalchemy.create_engine('postgresql://user:password@host:port/database')
query = 'SELECT * FROM mytable'
dtype = {'id': pl.Int64, 'name': pl.Utf8}
df = pd.read_sql_query(query, engine, dtype=dtype)
pl_df = pl.from_pandas(df, None)

In this example, we have explicitly defined the data types of id column as Int64 and name column as Utf8. This will ensure that the columns are correctly casted and any binary data is avoided.