astronomer / docs

This repository contains all content and code for Astro and Astronomer Software documentation.
53 stars 60 forks source link

astro.table.Temp Table has no object 'loc' #3866

Open Aman0807 opened 2 weeks ago

Aman0807 commented 2 weeks ago

I'm trying to get hands-on with astro python sdk and was following this official tutorial

https://www.astronomer.io/docs/learn/astro-python-sdk

One of the transformation tasks is defined as

@aql.dataframe
def transform_dataframe(df: DataFrame):
    purchase_dates = df.loc[:, "purchase_date"]
    print("purchase dates:", purchase_dates)
    return DataFrame(purchase_dates)

I'm getting the following error in the transform_dataframe logs.

AttributeError: 'TempTable' object has no attribute 'loc'

Is there any intermediate step I need to follow to convert the temporary snowflake table to pandas dataframe ?

TJaniF commented 1 week ago

Hello @Aman0807

Thanks for reporting this issue! We've seen it happen before but in the past only if the type of df was not specified as pd.DataFrame in the function definition that in theory should cause the @aql.dataframe decorator to convert the TempTable object to a pandas DataFrame and avoid the error.

While I wasn't able to replicate getting the error with the code snippet you shared that does have the type specification, I think I found a potential workaround for you:

You can force the conversion of the TempTable object to a DataFrame by using from astro.sql.operators.dataframe import _get_dataframe. So the transformation task would be:

@aql.dataframe
def transform_dataframe(df: DataFrame):

    from astro.sql.operators.dataframe import _get_dataframe
    df = _get_dataframe(df)  

    purchase_dates = df.loc[:, "purchase_date"]
    print("purchase dates:", purchase_dates)
    return DataFrame(purchase_dates)

I hope this works for you :)