Timestamp error in serialization

I am trying to plot a time serie, but I am not sure how to proceed.
My timeserie is a pandas dataframe in the server module, I am trying to return data to the interface like:

return mydf.to_dict()

but I get:

anvil.server.SerializationError: Cannot serialize return value from function. Cannot serialize dictionaries with keys that aren't strings at msg['response']['ave_rev_rating'][Timestamp('2019-03-31 00:00:00+0000', tz='UTC')]

Any hints on how to approach the problem?

Hi there,

Converting your time stamps to strings might work. Server functions cannot return a timestamp object as far as I know.

1 Like

They can return datetime objects, though, so you can convert it to a datetime and return that!

2 Likes

Thanks for clarifying that for me.

that’s weird, because that colum is a datetime column:

df['published'] = pd.to_datetime(df['published'], utc=True)

so if I have a pandas dataframe with a datetime column, how should I return data to the interface?

Pandas’s to_datetime is misleadingly named – it actually produces Pandas Timestamp objects. You want to_pydatetime to get actual honest-to-goodness datetime objects!

1 Like

thanks, unfortunately I still have the issue:

df['published'] = pd.to_datetime(df['published'], utc=True)
df['published'] = df['published'].dt.to_pydatetime()

it still gives me:

anvil.server.SerializationError: Cannot serialize return value from function. Cannot serialize dictionaries with keys that aren't strings at msg['response']['ave_rev_rating'][Timestamp('2019-01-13 00:00:00+0000', tz='UTC')]
1 Like

I was able to send data to the interface doing:

result.index = result.index.strftime('%Y-%m-%d')
return result.to_dict()

but now on the interface I have strings and not date, and it turns out there is no strptime on the client

I am kind of lost at this point…

@meredydd it looks that it cannot handle datetime as well:

anvil.server.SerializationError: Cannot serialize return value from function. Cannot serialize dictionaries with keys that aren't strings at msg['response']['ave_rev_rating'][datetime.datetime(2019, 1, 13, 0, 0, tzinfo=<UTC>)]

It looks extremely painful to represent time series…

I don’t see any luck with honest-to-goodness datetime as well… are you sure?

Unfortunately, it looks as if you can’t use datetime objects as dict keys on the client side, due to current Skulpt limitations. This may be improving with the next Skulpt update - we’ll keep you posted if it does!