civis.io.read_civis_sql
- civis.io.read_civis_sql(sql: str, database: str | int, return_as: str = 'list', use_pandas=DEPRECATED, sql_params_arguments: dict | None = None, encoding: str | None = None, job_name: str | None = None, client: APIClient | None = None, credential_id: int | None = None, polling_interval: int | float | None = None, hidden: bool = True, **kwargs) list | pd.DataFrame | pl.DataFrame[source]
Read data from Civis using a custom SQL string.
If no data is expected to return from the query, consider
query_civis()instead.- Parameters:
- sqlstr
The SQL select string to be executed.
- databasestr or int
Execute the query against this database. Can be the database name or ID.
- return_asstr, {“list”, “pandas”, “polars”}
If
"list"(the default), return a list. If"pandas", return apandas.DataFrame. If"polars", return apolars.DataFrame.- use_pandasbool, optional
If
True, return apandas.DataFrame. Otherwise, return a list of results fromcsv.reader().Deprecated since version 2.6.0:
use_pandaswill be removed at civis-python v3.0.0. Please usereturn_asinstead.- sql_params_argumentsdict, optional
A dictionary of SQL query parameters to pass directly to
civis.APIClient.scripts.post_sql. The only allowed keys are"params"(whose value is a list[dict]) and"arguments"(whose value is a dict). Please refer to the linked API documentation for how to format these two keys’ values.- encodingstr, optional
If
return_asis"pandas"or"polars", this parameter is passed to theencodingkwarg ofpandas.read_csv()orpolars.read_csv(), respectively. Ifreturn_asis"list", and if this parameter isn’t provided, then the UTF-8 encoding is assumed. In case you encounter aUnicodeDecodeError, consider choosing an encoding suitable for your data; see the list of standard encodings.- job_namestr, optional
A name to give the job. If omitted, a random job name will be used.
- client
civis.APIClient, optional If not provided, an
civis.APIClientobject will be created from theCIVIS_API_KEY.- credential_idstr or int, optional
The database credential ID. If
None, the default credential will be used.- polling_intervalint or float, optional
Number of seconds to wait between checks for query completion.
- hiddenbool, optional
If
True(the default), this job will not appear in the Civis UI.- **kwargskwargs
Extra keyword arguments are passed into
pandas.read_csv()ifreturn_asis"pandas", or passed intocsv.reader()ifreturn_asis"list", or passed intopolars.read_csv()ifreturn_asis"polars".
- Returns:
- datalist |
pandas.DataFrame|polars.DataFrame A list of rows (with header as first row) if
return_asis"list", orpandas.DataFrameifreturn_asis"pandas", orpolars.DataFrameifreturn_asis"polars". Note that ifreturn_asis"list", no parsing of types is performed and each row will be a list of strings.
- datalist |
- Raises:
- ImportError
If
return_asis"pandas"and pandas is not installed. Ifreturn_asis"polars"and polars is not installed.- EmptyResultError
If no rows were returned as a result of the query.
See also
civis.io.read_civisRead directly into memory without SQL.
civis.io.civis_to_csvWrite directly to a CSV file.
Notes
This reads the data into memory.
Examples
>>> import civis >>> sql = "SELECT * FROM schema.table" >>> df = civis.io.read_civis_sql(sql, "my_database", return_as="pandas") >>> col_a = df["column_a"]
>>> data = civis.io.read_civis_sql(sql, "my_database") >>> columns = data.pop(0) >>> col_a_index = columns.index("column_a") >>> col_a = [row[col_a_index] for row in data]