Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Cannot insert NaN and non-finite float values using Client.insert_rows() #696

Closed

Description

@wmdrichards

Trying to insert a NaN using Client.insert_rows fails. I tracked it down to

def _float_to_json(value):
    """Coerce 'value' to an JSON-compatible representation."""
    return value if value is None else float(value)

which should return "NaN" to be compatible with the REST API. Currently it returns an actual nan float value. Probably similar issues occur with other non-finite floats.

Environment details

  • OS type and version:
  • Python version: Python 3.6.8
  • pip version: pip 21.1
  • google-cloud-bigquery version: 2.20.0

Steps to reproduce

  1. Try to insert a NaN value into existing table

Code example

from google.cloud.bigquery import Client

client = Client(project='bar')
client.insert_rows(table=client.get_table('foo'), rows=[{'bar': float('NaN')}])

Stack trace

google.api_core.exceptions.BadRequest: 400 POST (REDACTED): Invalid JSON payload received. Unexpected token.
json": {"bar": NaN}, "insertId": "1

Activity

  1. added
    priority: p2Moderately-important priority. Fix may not be included in next release.
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    on Jun 11, 2021
  2. wmdrichards commented on Jun 11, 2021

    @wmdrichards
    Author

    For anyone else running into this, using insert_rows_json is a valid workaround, as long as the row is already json-compatible

  3. self-assigned this
    on Jun 17, 2021
  4. changed the title [-]Cannot insert NaN values using Client.insert_rows[/-] [+]Cannot insert NaN and non-finite float values using Client.insert_rows()[/+] on Jun 29, 2021
  5. plamut commented on Jun 29, 2021

    @plamut
    Contributor

    Thanks for reporting and researching that. It turned out that non-finite floats were indeed problematic, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: bigqueryIssues related to the googleapis/python-bigquery API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions