question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot serialize numpy.float32

See original GitHub issue

Hi,

I have an issue serialising a model that was fit with a column of type float32.

Version: fbprophet == 0.7.1 pystan == 2.19.1.1

Example code:

import pandas as pd
import numpy as np
from fbprophet import Prophet
from fbprophet.serialize import model_to_json

df = pd.DataFrame()
df["ds"] = pd.date_range(start="2020-01-01", end="2021-01-01", freq="1MS")
df["y"] = np.random.rand(df.shape[0])
df["y"] = df["y"].astype("float32")

model = Prophet()
model.fit(df)
model_json = model_to_json(model)

I did a bit of investigation:

# after returning the model_json before serialization:
import json
for key in model_json:
    try:
        json.dumps(model_json[key])
    except TypeError:
        print(key)

This returns only y_scale and type(model_json["y_scale"]) => numpy.float32.

Within serialize.py y_scale is in the list SIMPLE_ATTRIBUTES, as such it is serialised directly rather than using np.to_list()

This may relate to pull request #1656 and issue #1701

Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
stanton119commented, Mar 5, 2021

Yes! that’s the problem. I’m on Pandas 1.2.0 and I’m getting the .max() as a numpy type:

y_scale = df["y"].max()
print(type(y_scale))  # <class 'numpy.float32'>

I think your commit will solve it.

0reactions
blethamcommented, Apr 3, 2021

This fix has been pushed to pypi in the new v1.0 release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

jsonify() can't serialize numpy.float32() · Issue #4012 - GitHub
Flask version: 1.1.2. The following code throws error - TypeError: Object of type float32 is not JSON serializable. x = {'value': numpy.float32 ...
Read more >
numpy.float64 is JSON serializable but numpy.float32 is not
This led to the realization that the JSON module in Python can't serialize NumPy 32-bit floats. import numpy as np import json #...
Read more >
TypeError: Object of type 'float32' is not JSON serializable
I'm working with numpy.float32 numbers and they don't go into JSON . What's the right approach to overcome this issue? import numpy ...
Read more >
TypeError: Object of type float32 is not JSON serializable
The Python "TypeError: Object of type float32 is not JSON serializable" occurs when we try to convert a numpy float32 object to a...
Read more >
list is not json serializable - casavacanzelescuderie.it
To solve the error, convert the numpy float to a Python float before ... I wrote a serialize query but it doesnt work....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found