Search code examples
pythonsqlpandassnowflake-cloud-data-platformsnowflake-connector

AttributeError: 'SnowflakeCursor' object has no attribute 'shape'


Is there a way to use "pd.read_sql_query" w/ Snowflake?

I am trying to read my snowflake query in a pandas dataframe, however I get an "Attribute Error" stating 'SnowflakeCursor' object has no attribute 'shape' (code below) ...

Snowflake python connector works. fetch_pandas_all() works. Once I try reading the query as a pandas dataframe, it breaks.

import pyodbc  
import sys  
import os  
import getpass  
import snowflake.connector  
import pyarrow  


conn = snowflake.connector.connect(user='***', password='***', account='***', warehouse='***', database='***', schema='***')
cur = conn.cursor()
tenure = cur.execute(
    """SELECT DISTINCT CT_ID, CT_STRT_DATE, MONTHS_BETWEEN(TO_DATE('2022-01-01'), TO_DATE(CT_STRT_DATE)) AS TENURE 
    FROM ***.***.****
    WHERE CT_SVC_FREQ != 'OT' AND CT_CNCL_DATE IS NULL AND CT_ID LIKE 'S|%'
    ORDER BY CT_STRT_DATE DESC""")


print(tenure.fetch_pandas_all()) --- This code works fine

Once I try to manipulate using pandas, I run into errors...

print(tenure.shape) -- breaks w/ "Attribute Error"

Solution

  • What about this way:

    df_new = tenure.fetch_pandas_all()
    total_rows = df_new.shape[0]
    

    Creating a new DF should allow you to run shape