Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack Trace - ValueError: cannot convert float NaN to integer - When a foreign key is null #149

Closed
saltlightscript opened this issue Dec 2, 2021 · 4 comments

Comments

@saltlightscript
Copy link

In this example sport.expendables is a queryset that has Django objects with regular fields and ForeignKey fields.

The foreignKey fields are nullable
user = models.ForeignKey(UserProfile,on_delete=models.SET_NULL,blank=True,null=True)

If a user gets deleted from the database, the user foreign key on this model would get set to null.

However when try to run the read_frame on the query set, it causes this Stack Trace.

This seems to have a similar root cause to #128 however it is causing a different issue, stack trace instead of all None, or convert to float.

Traceback (most recent call last):
  File "/code/concessions/views.py", line 526, in send_report
    pd_expendables = read_frame(sport.expendables)
  File "/usr/local/lib/python3.7/site-packages/django_pandas/io.py", line 136, in read_frame
    update_with_verbose(df, fieldnames, fields)
  File "/usr/local/lib/python3.7/site-packages/django_pandas/utils.py", line 83, in update_with_verbose
    df[fieldname] = function(df[fieldname])
  File "/usr/local/lib/python3.7/site-packages/django_pandas/utils.py", line 47, in inner
    get_cache_key_from_pk, convert_dtype=False)
  File "/usr/local/lib/python3.7/site-packages/pandas/core/series.py", line 4357, in apply
    return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
  File "/usr/local/lib/python3.7/site-packages/pandas/core/apply.py", line 1043, in apply
    return self.apply_standard()
  File "/usr/local/lib/python3.7/site-packages/pandas/core/apply.py", line 1102, in apply_standard
    convert=self.convert_dtype,
  File "pandas/_libs/lib.pyx", line 2859, in pandas._libs.lib.map_infer
  File "/usr/local/lib/python3.7/site-packages/django_pandas/utils.py", line 42, in get_cache_key_from_pk
    return None if pk is None else base_cache_key % int(pk)
ValueError: cannot convert float NaN to integer
@saltlightscript
Copy link
Author

This may be similar to #128 and or #93 However it seems to have a different result, Stack Trace, so filling an issue for it

@juliandehne
Copy link

juliandehne commented Jan 10, 2022

I can reproduce the error:

Exception Type: ValueError
cannot convert float NaN to integer
venv/lib/python3.8/site-packages/django_pandas/utils.py, line 42, in get_cache_key_from_pk

@juliandehne
Copy link

Fixed it with updating to django-pandas==0.6.6

@odoublewen
Copy link

@juliandehne Did updating to 0.6.6 really fix the underlying problem?

Re: my issue #128 I was also getting the same stack trace as you pasted above ("ValueError: cannot convert float NaN to integer"). I apparently wasn't getting that back at the end of 2020, so I presume something changed in Django, and now that I am using 3.2.7, I see the same traceback as you pasted.

So: I do think this issue is the same as #128.

But: What I see when I upgrade to 0.6.6 is that now, the exception is not getting triggered, but the underlying problem I described in #128 is still there: "if one null is present, all rows in the df become Null." (see my issue for an illustration).

It would be great if you could carefully check your situation to confirm or not confirm this observation -- thank you.

cc: @chrisdev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants