Here is my code.
In the excel file, it should have 4 tables, including date = 1/3,1/7,1/14,1/21.
I have run the query, it showed 4 results.
However, when I wrote to the excel, the file only has one table, which was date = 1/3.
I'm wondering how can I correct this, thanks!
import pyodbc
import pandas as pd
from datetime import datetime,timedelta
cnxn = pyodbc.connect('DRIVER=xx; SERVER=xx; DATABASE=xx; UID=xx; PWD=xx')
cursor = cnxn.cursor()
query="""
declare #START_ORDATE DATETIME
declare #END_ORDATE DATETIME
set #START_ORDATE ='2022-01-03 00:00:00:000'
set #END_ORDATE ='2022-01-24 00:00:00:000'
WHILE #START_ORDATE<=#END_ORDATE
BEGIN
select xx,xx,xx...
set #START_ORDATE = #START_ORDATE + 7
END
"""
df = pd.read_sql_query(query, cnxn)
writer = pd.ExcelWriter('test1.xlsx')
df.to_excel(writer, sheet_name='test000')
writer.save()
I solved it! Put while outside the SQL query, as the following.
import pyodbc
import pandas as pd
from datetime import datetime,timedelta
cnxn = pyodbc.connect('DRIVER=xx; SERVER=xx; DATABASE=xx; UID=xx; PWD=xx;MARS_Connection=yes')
cursor = cnxn.cursor()
start = datetime.strptime('2022-01-03','%Y-%m-%d')
end = datetime.strptime('2022-10-24','%Y-%m-%d')
query="""
...
"""
while start<=end:
cursor.execute(query,(start,start,start+timedelta(days=6)))
df = pd.DataFrame.from_records(cursor.fetchall(),columns=[desc[0] for desc in cursor.description])
start = start + timedelta(days=7)
You made need to use a for loop on the actual object to loop through what was returned. Let me know if this helps.
writer = pd.ExcelWriter('test1.xlsx')
for x in df:
df.to_excel(writer, sheet_name='test000')
writer.save()
Related
Is there a way to create a table in snowflake from a pandas dataframe in python just using the snowflake connector and pandas library? Main goal here is to just take a pandas dataframe and use the schema to create a new table in a specific data warehouse/database/schema in snowflake. I have seen examples of how to do this with sqlalchemy which I am trying to avoid but worst case I will just use that.
I have tried other methods inclduing sqlalchemy, using the snowflake uploading method using the PUT command but wanted to ask if anyone had an alternative that just uses the snowflake connector, pandas and does require me to save the data to my drive locally or use sqlalchemy.
Appreciate any input, or feedback on how to write better questions too.
*Note:
write_pandas - snowflake connector function can only append tables that already exist.
df.to_sql - only works with sqlalchemy or sqlite3 connections so don't think snowflake conn would work but could be wrong?
I have used the snowflake connector functions write_pandas(), and pd_writer() with the pandas function to_sql(). The issue here is that the pandas to_sql() function in the docs states that the connection can only be "consqlalchemy.engine.(Engine or Connection) or sqlite3.Connection". I would prefer to keep using the snowflake-connector for python. Without using sqlalchemy I know I could do the following:
.ini config file for connecting to database (named db.ini)
[database_name]
user = user7
pass = s$cret
acc = jhn675f
wh = 22jb7tyo5
db = dev_env546
db_schema = hubspot
python module for connecting to snowflake db and code to execute
import configparser
import pandas as pd
import snowflake.connector
config = configparser.ConfigParser()
config.read('db.ini')
sn_user = config['database_name']['user']
sn_password = config['database_name']['pass']
sn_account = config['database_name']['acc']
sn_warehouse = config['database_name']['wh']
sn_database = config['database_name']['db']
sn_schema= config['database_name']['db_schema']
ctx = snowflake.connector.connect(
user=sn_user ,
password = sn_password
account=sn_account ,
warehouse=sn_warehouse ,
database=sn_database ,
schema=sn_schema
)
cs = ctx.cursor()
query_extract = '''
select table1.field1,
table1.field2,
table1.field3,
table1.field4,
table1.field5,
table1.field6,
table1.field7,
table2.field2,
table2.field5,
table2.field7,
table2.field9,
table3.field1,
table3.field6
from database.schema.table1
left join database.schema.table2
on table1.field3 = table2.field1
left join database.schema.table3
on table1.field5 = table3.field1
'''
try:
cs.execute(query_extract)
df = cs.fetch_pandas_all()
except:
#throw exception here
# clean data in the dataframe and perform some calcs
# store results in new dataframe called df_final
# would like to just use df_final to create a table in snowflake based on df_final schema and datatypes
# right now I am not sure how to do that
Current methods or alternatives
import configparser
import pandas as pd
import snowflake.connector
config = configparser.ConfigParser()
config.read('db.ini')
sn_user = config['database_name']['user']
sn_password = config['database_name']['pass']
sn_account = config['database_name']['acc']
sn_warehouse = config['database_name']['wh']
sn_database = config['database_name']['db']
sn_schema= config['database_name']['db_schema']
ctx = snowflake.connector.connect(
user=sn_user ,
password = sn_password
account=sn_account ,
warehouse=sn_warehouse ,
database=sn_database ,
schema=sn_schema
)
cs = ctx.cursor()
query_extract = '''
select table1.field1,
table1.field2,
table1.field3,
table1.field4,
table1.field5,
table1.field6,
table1.field7,
table2.field2,
table2.field5,
table2.field7,
table2.field9,
table3.field1,
table3.field6
from database.schema.table1
left join database.schema.table2
on table1.field3 = table2.field1
left join database.schema.table3
on table1.field5 = table3.field1
'''
try:
cs.execute(query_extract)
df = cs.fetch_pandas_all()
except:
#throw exception here
df_final.to_csv('data/processed_data')
create_stage = '''create stage processed_data_stage
copy_options = (on_error='skip_file');'''
create_file_format = '''create or replace file format processed_data_stage
type = 'csv' field_delimiter = ',';'''
upload_file = '''put file:/data/processed_data.csv #processed_data_stage;'''
Other alternative is just embracing sqlalchemy and the pandas_to_sql function
from snowflake.connector.pandas_tools import pd_writer
import pandas as pd
from sqlalchemy import create_engine
account_identifier = '<account_identifier>'
user = '<user_login_name>'
password = '<password>'
database_name = '<database_name>'
schema_name = '<schema_name>'
conn_string = f"snowflake://{user}:{password}#{account_identifier}/{database_name}/{schema_name}"
engine = create_engine(conn_string)
#Create your DataFrame
table_name = 'cities'
df = pd.DataFrame(data=[['Stephen','Oslo'],['Jane','Stockholm']],columns=['Name','City'])
#What to do if the table exists? replace, append, or fail?
if_exists = 'replace'
#Write the data to Snowflake, using pd_writer to speed up loading
with engine.connect() as con:
df.to_sql(name=table_name.lower(), con=con, if_exists=if_exists, method=pd_writer)
So for anyone who is doing something similar I ended up finding a pandas function that is not documented but can extract the schema from a dataframe.
The pandas library has a function called pd.io.sql.get_schema() that can return a string formatted as an sql query based on a dataframe and table name. So you can do:
import configparser
import pandas as pd
import snowflake.connector
config = configparser.ConfigParser()
config.read('db.ini')
sn_user = config['database_name']['user']
sn_password = config['database_name']['pass']
sn_account = config['database_name']['acc']
sn_warehouse = config['database_name']['wh']
sn_database = config['database_name']['db']
sn_schema= config['database_name']['db_schema']
ctx = snowflake.connector.connect(
user=sn_user ,
password = sn_password
account=sn_account ,
warehouse=sn_warehouse ,
database=sn_database ,
schema=sn_schema
)
cs = ctx.cursor()
query_extract = '''
select table1.field1,
table1.field2,
table1.field3,
table1.field4,
table1.field5,
table1.field6,
table1.field7,
table2.field2,
table2.field5,
table2.field7,
table2.field9,
table3.field1,
table3.field6
from database.schema.table1
left join database.schema.table2
on table1.field3 = table2.field1
left join database.schema.table3
on table1.field5 = table3.field1
'''
try:
cs.execute(query_extract)
df = cs.fetch_pandas_all()
except:
#throw exception here
# clean data in the dataframe and perform some calcs
# store results in new dataframe called df_final
df_final.columns = map(lambda x: str(x).upper(), df_final.columns)
tb_name = 'NEW_TABLE'
df_schema = pd.io.sql.get_schema(df_final, tb_name)
df_schema = str(df_schema).replace('TEXT', 'VARCAHR(256)')
#use replace to remove characters and quotes that might give you syntax errors
cs.execute(df_schema)
success, nchunks, nrows, _ = write_pandas(ctx, df, tb_name)
cs.close()
ctx.close()
I skipped over parts but basically you can do:
establish connection to snowflake
extract tables from snowflake into pandas dataframe
clean dataframe and perform transformations and save into new dataframe
use pd.io.sql.get_schema to get a string sql query based on the pandas dataframe you want to load into snowflake
Use the string of the sql query with your connection and database cursor to execute the create table command based on df schema
use the snowflake write_pandas command to write your df to the newly created snowflake table
I am running redshift query which is having 40 millions of record. But when I am saving into csv file it is showing only 7 thousands of record. Could you please help me how to solve this?
Example:
Code:
conn = gcso_conn1()
with conn.cursor() as cur:
query = "select * from (select a.src_nm Source_System ,b.day_id Date,b.qty Market_Volume,b.cntng_unt Volume_Units,b.sls_in_lcl_crncy Market_Value,b.crncy_cd Value_Currency,a.panel Sales_Channel,a.cmpny Competitor_Name,a.lcl_mnfcr Local_Manufacturer ,a.src_systm_id SKU_PackID_ProductNumber,upper(a.mol_list) Molecule_Name,a.brnd_nm BrandName_Intl,a.lcl_prod_nm BrandName_Local,d.atc3_desc Brand_Indication,a.prsd_strngth_1_nbr Strength,a.prsd_strngth_1_unt Strength_Units,a.pck_desc Pack_Size_Number,a.prod_nm Product_Description,c.iso3_cntry_cd Country_ISO_Code,c.cntry_nm Country_Name from gcso_prd_cpy.dim_prod a join gcso_prd_cpy.fct_sales b on (a.SRC_NM='IMS' and b.SRC_NM='IMS' and a.prod_id = b.prod_id) join gcso_prd_cpy.dim_cntry c on (a.cntry_id = c.cntry_id) left outer join gcso_prd_cpy.dim_thrc_area d on (a.prod_id = d.prod_id) WHERE a.SRC_NM='IMS' and c.iso3_cntry_cd in ('JPN','IND','CAN','USA') and upper(a.mol_list) in ('AMBRISENTAN', 'BERAPROST','BOSENTAN') ORDER BY b.day_id ) a"
#print(query)
cur.execute(query)
result = cur.fetchall()
conn.commit()
column = [i[0] for i in cur.description]
sqldf = pd.DataFrame(result, columns= column)
print(sqldf.count())
#print(df3)
sqldf.to_csv(Output_Path, index= False, sep= '\001', encoding = 'utf-8')
Everything should work correctly. I think the main problem is debugging using count(). You expect number of records but docs says:
Count non-NA cells for each column or row.
Better to use when debugging DataFrame:
print(len(df))
print(df.shape)
print(df.info())
Also you can do it easier using read_sql:
import pandas as pd
from sqlalchemy import create_engine
header = True
for chunk in pd.read_sql(
'your query here - SELECT * FROM... ',
con=create_engine('creds', echo=True), # set creds - postgres+psycopg2://user:password#host:5432/db_name
chunksize=1000, # read by chunks
):
file_path = '/tmp/path_to_your.csv'
chunk.to_csv(
file_path,
header=header,
mode='a',
index=False,
)
header = False
Below is my code, but this is not writing anything in file
import os
import pymysql
import pandas as pd
import csv
host = os.getenv('MYSQL_HOST')
port = os.getenv('MYSQL_PORT')
user = os.getenv('MYSQL_USER')
password = os.getenv('MYSQL_PASSWORD')
database = os.getenv('MYSQL_DATABASE')
conn = pymysql.connect(
host=host,
port=int(3306),
user="root",
passwd="Pass200",
db="test_db",
charset='utf8mb4')
QUERY='SELECT * FROM act;'
df = pd.read_sql_query("SELECT * FROM act",conn)
df.tail(10)
Above code successfully print SQL DB data. I appended below code to above code
for writing all data alongwith column names in EXCEL Sheet:
cur=conn.cursor()
cur.execute(QUERY)
result=cur.fetchall()
c = csv.writer(open('test.csv', 'w'))
for x in result:
print(x);
c.writerow(x)
Kindly assist as I am new to this.
Since you are using pandas you can use df.to_csv('test.csv')
I'm working with an Oracle database. I can do this much:
import pandas as pd
import pandas.io.sql as psql
import cx_Oracle as odb
conn = odb.connect(_user +'/'+ _pass +'#'+ _dbenv)
sqlStr = "SELECT * FROM customers"
df = psql.frame_query(sqlStr, conn)
But I don't know how to handle bind variables, like so:
sqlStr = """SELECT * FROM customers
WHERE id BETWEEN :v1 AND :v2
"""
I've tried these variations:
params = (1234, 5678)
params2 = {"v1":1234, "v2":5678}
df = psql.frame_query((sqlStr,params), conn)
df = psql.frame_query((sqlStr,params2), conn)
df = psql.frame_query(sqlStr,params, conn)
df = psql.frame_query(sqlStr,params2, conn)
The following works:
curs = conn.cursor()
curs.execute(sqlStr, params)
df = pd.DataFrame(curs.fetchall())
df.columns = [rec[0] for rec in curs.description]
but this solution is just...inellegant. If I can, I'd like to do this without creating the cursor object. Is there a way to do the whole thing using just pandas?
Try using pandas.io.sql.read_sql_query. I used pandas version 0.20.1, I used it, it worked out:
import pandas as pd
import pandas.io.sql as psql
import cx_Oracle as odb
conn = odb.connect(_user +'/'+ _pass +'#'+ _dbenv)
sqlStr = """SELECT * FROM customers
WHERE id BETWEEN :v1 AND :v2
"""
pars = {"v1":1234, "v2":5678}
df = psql.frame_query(sqlStr, conn, params=pars)
As far as I can tell, pandas expects that the SQL string be completely formed prior to passing it along. With that in mind, I would (and always do) use string interpolation:
params = (1234, 5678)
sqlStr = """
SELECT * FROM customers
WHERE id BETWEEN %d AND %d
""" % params
print(sqlStr)
which gives
SELECT * FROM customers
WHERE id BETWEEN 1234 AND 5678
So that should feed into psql.frame_query just fine. (it does in my experience with postgres, mysql, and sql server).
Any help on this problem will be greatly appreciated.
So basically I want to run a query to my SQL database and store the returned data as Pandas data structure.
I have attached code for query.
I am reading the documentation on Pandas, but I have problem to identify the return type of my query.
I tried to print the query result, but it doesn't give any useful information.
Thanks!!!!
from sqlalchemy import create_engine
engine2 = create_engine('mysql://THE DATABASE I AM ACCESSING')
connection2 = engine2.connect()
dataid = 1022
resoverall = connection2.execute("
SELECT
sum(BLABLA) AS BLA,
sum(BLABLABLA2) AS BLABLABLA2,
sum(SOME_INT) AS SOME_INT,
sum(SOME_INT2) AS SOME_INT2,
100*sum(SOME_INT2)/sum(SOME_INT) AS ctr,
sum(SOME_INT2)/sum(SOME_INT) AS cpc
FROM daily_report_cooked
WHERE campaign_id = '%s'",
%dataid
)
So I sort of want to understand what's the format/datatype of my variable "resoverall" and how to put it with PANDAS data structure.
Here's the shortest code that will do the job:
from pandas import DataFrame
df = DataFrame(resoverall.fetchall())
df.columns = resoverall.keys()
You can go fancier and parse the types as in Paul's answer.
Edit: Mar. 2015
As noted below, pandas now uses SQLAlchemy to both read from (read_sql) and insert into (to_sql) a database. The following should work
import pandas as pd
df = pd.read_sql(sql, cnxn)
Previous answer:
Via mikebmassey from a similar question
import pyodbc
import pandas.io.sql as psql
cnxn = pyodbc.connect(connection_info)
cursor = cnxn.cursor()
sql = "SELECT * FROM TABLE"
df = psql.frame_query(sql, cnxn)
cnxn.close()
If you are using SQLAlchemy's ORM rather than the expression language, you might find yourself wanting to convert an object of type sqlalchemy.orm.query.Query to a Pandas data frame.
The cleanest approach is to get the generated SQL from the query's statement attribute, and then execute it with pandas's read_sql() method. E.g., starting with a Query object called query:
df = pd.read_sql(query.statement, query.session.bind)
Edit 2014-09-30:
pandas now has a read_sql function. You definitely want to use that instead.
Original answer:
I can't help you with SQLAlchemy -- I always use pyodbc, MySQLdb, or psychopg2 as needed. But when doing so, a function as simple as the one below tends to suit my needs:
import decimal
import pyodbc #just corrected a typo here
import numpy as np
import pandas
cnn, cur = myConnectToDBfunction()
cmd = "SELECT * FROM myTable"
cur.execute(cmd)
dataframe = __processCursor(cur, dataframe=True)
def __processCursor(cur, dataframe=False, index=None):
'''
Processes a database cursor with data on it into either
a structured numpy array or a pandas dataframe.
input:
cur - a pyodbc cursor that has just received data
dataframe - bool. if false, a numpy record array is returned
if true, return a pandas dataframe
index - list of column(s) to use as index in a pandas dataframe
'''
datatypes = []
colinfo = cur.description
for col in colinfo:
if col[1] == unicode:
datatypes.append((col[0], 'U%d' % col[3]))
elif col[1] == str:
datatypes.append((col[0], 'S%d' % col[3]))
elif col[1] in [float, decimal.Decimal]:
datatypes.append((col[0], 'f4'))
elif col[1] == datetime.datetime:
datatypes.append((col[0], 'O4'))
elif col[1] == int:
datatypes.append((col[0], 'i4'))
data = []
for row in cur:
data.append(tuple(row))
array = np.array(data, dtype=datatypes)
if dataframe:
output = pandas.DataFrame.from_records(array)
if index is not None:
output = output.set_index(index)
else:
output = array
return output
1. Using MySQL-connector-python
# pip install mysql-connector-python
import mysql.connector
import pandas as pd
mydb = mysql.connector.connect(
host = 'host',
user = 'username',
passwd = 'pass',
database = 'db_name'
)
query = 'select * from table_name'
df = pd.read_sql(query, con = mydb)
print(df)
2. Using SQLAlchemy
# pip install pymysql
# pip install sqlalchemy
import pandas as pd
import sqlalchemy
engine = sqlalchemy.create_engine('mysql+pymysql://username:password#localhost:3306/db_name')
query = '''
select * from table_name
'''
df = pd.read_sql_query(query, engine)
print(df)
MySQL Connector
For those that works with the mysql connector you can use this code as a start. (Thanks to #Daniel Velkov)
Used refs:
Querying Data Using Connector/Python
Connecting to MYSQL with Python in 3 steps
import pandas as pd
import mysql.connector
# Setup MySQL connection
db = mysql.connector.connect(
host="<IP>", # your host, usually localhost
user="<USER>", # your username
password="<PASS>", # your password
database="<DATABASE>" # name of the data base
)
# You must create a Cursor object. It will let you execute all the queries you need
cur = db.cursor()
# Use all the SQL you like
cur.execute("SELECT * FROM <TABLE>")
# Put it all to a data frame
sql_data = pd.DataFrame(cur.fetchall())
sql_data.columns = cur.column_names
# Close the session
db.close()
# Show the data
print(sql_data.head())
Here's the code I use. Hope this helps.
import pandas as pd
from sqlalchemy import create_engine
def getData():
# Parameters
ServerName = "my_server"
Database = "my_db"
UserPwd = "user:pwd"
Driver = "driver=SQL Server Native Client 11.0"
# Create the connection
engine = create_engine('mssql+pyodbc://' + UserPwd + '#' + ServerName + '/' + Database + "?" + Driver)
sql = "select * from mytable"
df = pd.read_sql(sql, engine)
return df
df2 = getData()
print(df2)
This is a short and crisp answer to your problem:
from __future__ import print_function
import MySQLdb
import numpy as np
import pandas as pd
import xlrd
# Connecting to MySQL Database
connection = MySQLdb.connect(
host="hostname",
port=0000,
user="userID",
passwd="password",
db="table_documents",
charset='utf8'
)
print(connection)
#getting data from database into a dataframe
sql_for_df = 'select * from tabledata'
df_from_database = pd.read_sql(sql_for_df , connection)
Like Nathan, I often want to dump the results of a sqlalchemy or sqlsoup Query into a Pandas data frame. My own solution for this is:
query = session.query(tbl.Field1, tbl.Field2)
DataFrame(query.all(), columns=[column['name'] for column in query.column_descriptions])
resoverall is a sqlalchemy ResultProxy object. You can read more about it in the sqlalchemy docs, the latter explains basic usage of working with Engines and Connections. Important here is that resoverall is dict like.
Pandas likes dict like objects to create its data structures, see the online docs
Good luck with sqlalchemy and pandas.
Simply use pandas and pyodbc together. You'll have to modify your connection string (connstr) according to your database specifications.
import pyodbc
import pandas as pd
# MSSQL Connection String Example
connstr = "Server=myServerAddress;Database=myDB;User Id=myUsername;Password=myPass;"
# Query Database and Create DataFrame Using Results
df = pd.read_sql("select * from myTable", pyodbc.connect(connstr))
I've used pyodbc with several enterprise databases (e.g. SQL Server, MySQL, MariaDB, IBM).
This question is old, but I wanted to add my two-cents. I read the question as " I want to run a query to my [my]SQL database and store the returned data as Pandas data structure [DataFrame]."
From the code it looks like you mean mysql database and assume you mean pandas DataFrame.
import MySQLdb as mdb
import pandas.io.sql as sql
from pandas import *
conn = mdb.connect('<server>','<user>','<pass>','<db>');
df = sql.read_frame('<query>', conn)
For example,
conn = mdb.connect('localhost','myname','mypass','testdb');
df = sql.read_frame('select * from testTable', conn)
This will import all rows of testTable into a DataFrame.
Long time from last post but maybe it helps someone...
Shorted way than Paul H:
my_dic = session.query(query.all())
my_df = pandas.DataFrame.from_dict(my_dic)
Here is mine. Just in case if you are using "pymysql":
import pymysql
from pandas import DataFrame
host = 'localhost'
port = 3306
user = 'yourUserName'
passwd = 'yourPassword'
db = 'yourDatabase'
cnx = pymysql.connect(host=host, port=port, user=user, passwd=passwd, db=db)
cur = cnx.cursor()
query = """ SELECT * FROM yourTable LIMIT 10"""
cur.execute(query)
field_names = [i[0] for i in cur.description]
get_data = [xx for xx in cur]
cur.close()
cnx.close()
df = DataFrame(get_data)
df.columns = field_names
pandas.io.sql.write_frame is DEPRECATED.
https://pandas.pydata.org/pandas-docs/version/0.15.2/generated/pandas.io.sql.write_frame.html
Should change to use pandas.DataFrame.to_sql
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html
There is another solution.
PYODBC to Pandas - DataFrame not working - Shape of passed values is (x,y), indices imply (w,z)
As of Pandas 0.12 (I believe) you can do:
import pandas
import pyodbc
sql = 'select * from table'
cnn = pyodbc.connect(...)
data = pandas.read_sql(sql, cnn)
Prior to 0.12, you could do:
import pandas
from pandas.io.sql import read_frame
import pyodbc
sql = 'select * from table'
cnn = pyodbc.connect(...)
data = read_frame(sql, cnn)
best way I do this
db.execute(query) where db=db_class() #database class
mydata=[x for x in db.fetchall()]
df=pd.DataFrame(data=mydata)
If the result type is ResultSet, you should convert it to dictionary first. Then the DataFrame columns will be collected automatically.
This works on my case:
df = pd.DataFrame([dict(r) for r in resoverall])
Here is a simple solution I like:
Put your DB connection info in a YAML file in a secure location (do not version it in the code repo).
---
host: 'hostname'
port: port_number_integer
database: 'databasename'
user: 'username'
password: 'password'
Then load the conf in a dictionary, open the db connection and load the result set of the SQL query in a data frame:
import yaml
import pymysql
import pandas as pd
db_conf_path = '/path/to/db-conf.yaml'
# Load DB conf
with open(db_conf_path) as db_conf_file:
db_conf = yaml.safe_load(db_conf_file)
# Connect to the DB
db_connection = pymysql.connect(**db_conf)
# Load the data into a DF
query = '''
SELECT *
FROM my_table
LIMIT 10
'''
df = pd.read_sql(query, con=db_connection)