Skip to content

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

@wmdrichards

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

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