Pandas error in reading .csv passed to a server module

I’m getting this error:
AttributeError: module ‘pandas.compat’ has no attribute ‘StringIO’ at [CHServerModule_one, line 33](javascript:void(0)) called from [asset_inventory_tool, line 33](javascript:void(0))

I’ve checked with alerts, the datatype of my file is text, I didn’t see any posts here or in the wild as to what might be causing this. The code below is from a clone of a sample from campopianoa

def LoadOffshoreAssets(file):

my_bytes=file.get_bytes()
my_string = str(my_bytes, ‘utf-8’)
df = pd.read_csv(pd.compat.StringIO(my_string))
df = df.replace({pd.np.nan: None})

The only references I see to the error refer to non-Anvil implementations and discuss pandas versions, etc.

EDIT: Woops I made a premature post there. This should make more sense.

You don’t need to convert to string first. You can directly take the data from bytes as such:

from io import BytesIO

def LoadOffshoreAssets(file):
    my_bytes=file.get_bytes()
    df = pd.read_csv(BytesIO(my_bytes))
    df = df.replace({pd.np.nan: None})

See if that works.

4 Likes

After days of trying to import a csv from googledrive to a pandas dataframe this did the trick.

You are awesome Robert.

1 Like

Glad it helped @fishfeedsat! And welcome to the forum.

1 Like