I am using a corporate snowflake license. Typically I do everything in a sql file, I am not trying to work in a .ipynb file. The following code in sql works as I expect:
USE DATABASE DECISION_ANALYTICS;
USE SCHEMA MJ;
create or replace temporary table base as
select *
from DECISION_ANALYTICS.MJ.suspect_counts;
When I start a python block the following code gets an error:
import pandas as pd
base.head()
df = base.to_pandas()
NameError Traceback (most recent call last)
Cell In[47], line 3
1 import pandas as pd
----> 3 base.head()
4 #df = base.to_pandas()
NameError: name 'base' is not defined
I have tried using the full name DECISION_ANALYTICS.MJ.suspect_counts as wekk as base. Thanks in advance for any help.
Welcome to the community and thanks for your question!
It seems likely that the error is because the variable base is not defined in your Python environment—SQL statements like CREATE TABLE only affect the database, not your Python namespace. In Python, you need to explicitly load the table into a variable using your Snowflake connection/session before you can call methods like .head() or .to_pandas().
For example, if you’re using Snowpark in a Python block, you should do something like:
from snowflake.snowpark.context import get_active_session
session = get_active_session()
base = session.table("DECISION_ANALYTICS.MJ.suspect_counts")
df = base.to_pandas()
df.head()
This will assign the Snowpark DataFrame to base, which you can then convert to a pandas DataFrame. My understanding is that you can’t reference SQL tables directly as Python variables without this step, as described in the Streamlit Snowflake tutorial.
Sources: