What I’m trying to do:
I have a Pandas dataframe, generated from a crosstab.
ct = pd.crosstab(
[df.verwijzing_type],
[df.verwijzing_jaar],
margins=True)
I wanted to get this table to the client. Usually I would do this with ct.to_dict(‘records’) and usually works fine. The output of ct.to_dict(‘records’) is something like:
[{2012.0: 2896,
2013.0: 2825,
2014.0: 3179,
2015.0: 3638,
2016.0: 3670,
2017.0: 3956,
2018.0: 3117,
2019.0: 3265,
2020.0: 3170,
2021.0: 1120,
'All': 30836},
{2012.0: 1000,
2013.0: 1182,
2014.0: 896,
2015.0: 846,
2016.0: 956,
2017.0: 1076,
2018.0: 1135,
2019.0: 1124,
2020.0: 1025,
2021.0: 330,
'All': 9570},
:
:
This yields two problems:
First a very vague error (at first):
TypeError: '<' not supported between instances of 'str' and 'float'
Turns out that the column 'All", added by the crosstab margins=True, confuses Anvil in the serialisation step. This is related to the next problem.
By setting margins=False I removed the All column.
Then we get the error:
anvil.server.SerializationError: Cannot serialize return value from function. Cannot serialize dictionaries with keys that aren't strings at ...['crosstab_type_year'][0][2012.0]
In my crosstab the columns are years and are named as such. These turn out to be are floats: 2018.0, 2019.0, 2020.0 etc. Perfectly fine in python/Pandas.
But it seems Anvil gets confused.
I can probably work around this problem, but is there any reason for Anvil not to fully support python dicts?