SerializationError when passing Dictionary from Uplink Method

What I’m trying to do:
I have a Portable Class called Indicator() that has one method Calculate (the class is basically a Factory Class, calculating different indicators). Calculate calls a server function called method_indicator. I’ve included a snippet of what method_indicator should be returning.

Basically, the indicators are calculated using Pandas Series and the method_indicator converts it to a dictionary object which should be able to be sent back to the Anvil Server from my Uplink Server. However, I am getting this error:

anvil.server.SerializationError: Cannot serialize arguments to function. Cannot serialize <class 'pandas.core.series.Series'> object at msg['args'][1]

I have run the code locally and it is properly returning a dictionary. The only thing I see that is still related to the series is nan values that can appear. Not sure if that’s a problem. Any idea where I may be going wrong?

Code Sample:

  # Return the items for each series that exists in the return value 'indicator_data' in JSON convertable format
    if type(indicator_data[0]) is tuple:
        data_stored = [{'label':series[0],'values':list(series[1].to_dict().items())} for series in list(indicator_data)]
        indicator_data = {'id':indicator, 'data': data_stored}
        return indicator_data
    else:
        return {'id': indicator, 'data':[{'label':indicator_data[0],'values':list(indicator_data[1].to_dict().items())}]}

sample output
{'id': 'Indicator_1_ID', 'data': [{'label': 'Indicator_1_output', 'values': [(0, nan), (1, 6.9518243590442e-310), (2, 3.333333333333334), (3, 3.333333333333332), (4, 3.055555555555557), (5, 3.657407407407412), (6, 4.205246913580247), (7, -0.4385288065843582), (8, -1.8996484910836742), (9, -0.17661894147233426), (10, 3.836092297287003), (11, 1.3928467379845628), (12, -4.645309767894034), (13, -5.769899262909121), (14, -2.534031091108595)]}, {'label': 'Indicator_2_output', 'values': [(0, nan), (1, 3.333333333333332), (2, 3.055555555555557), (3, 3.657407407407412), (4, 4.205246913580247), (5, -0.4385288065843582), (6, -1.8996484910836742), (7, -0.17661894147233426), (8, 3.836092297287003), (9, 1.3928467379845628), (10, -4.645309767894034), (11, -5.769899262909121), (12, -2.534031091108595), (13, 3.0), (14, 12.0)]}, {'label': 'Indicator_3_output', 'values': [(0, nan), (1, -3.333333333333332), (2, 0.2777777777777768), (3, -0.32407407407407973), (4, -1.1496913580246897), (5, 4.09593621399177), (6, 6.104895404663921), (7, -0.26190986511202397), (8, -5.735740788370677), (9, -1.569465679456897), (10, 8.481402065181037), (11, 7.162746000893684), (12, -2.111278676785439), (13, nan), (14, 3.509779422807088e+64)]}]}

It looks like your hunch is correct, since the nan objects don’t appear as strings in your sample output.

I would expect these values to be actually of type pandas.core.series.Series, and your output when looking at it is just that types repr.
In other words, what it looks like is represented as the letters nan when you print it to the screen, instead of something like:

What happens if you take your indicator_data[1].to_dict() and wrap it in a json encode, and then decode it back into a python dict? (It might convert the nan objects properly)

This is not recommended for production since it could result in slower performance, but it would help you debug.

1 Like

Thanks for the advice! I found out the issue was actually my input. One of my other functions wrapped a list to a Pandas Series causing the issue. Changing it back to a list resolved the issue!

1 Like