GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Client.insert_rows throws exception when coercing string to float from release 2.21.0 · Issue #818 · googleapis/python-bigquery

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.

Client.insert_rows throws exception when coercing string to float from release 2.21.0 #818

Copy link
Copy link
Closed
#824
Closed
Client.insert_rows throws exception when coercing string to float from release 2.21.0#818
#824
Copy link
Assignees
plamut
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.

Description

@Alan-Robinson
Alan-Robinson
opened on Jul 27, 2021
Issue body actions

The behaviour of Client.insert_rows changed between release 2.20.0 and 2.21.0.

In 2.20.0, where the schema defines a field to be of type FLOAT, a string passed in would be coerced to a float in the resulting JSON.

In 2.21.0, where the schema defines a field to be of type FLOAT, a string passed in throws an exception.

I've tracked this down to #728 , where a check is added for isnan or isinf. However, those functions require a float to be provided so throw an exception if a string is provided. This check occurs before you get to the code that coerces the input to a float.

def _float_to_json(value):
    """Coerce 'value' to an JSON-compatible representation."""
    if value is None:
        return None
    elif math.isnan(value) or math.isinf(value):
        return str(value)
    else:
        return float(value)

Environment details

  • OS type and version:
  • Python version: Python 3.6.10
  • pip version: pip 21.2.1
  • google-cloud-bigquery version: 2.22.1

Steps to reproduce

  1. Setup table with a field defined to be FLOAT
  2. Pass a string to Client.insert_rows() for that field

Code example

from google.cloud.bigquery import Client

client = Client(project='redacted')
client.insert_rows(table=client.get_table('python_test.test'), rows=[{'bar': "0.01"}])

Stack trace

  File "site-packages/google/cloud/bigquery/client.py", line 3329, in insert_rows
    json_rows = [_record_field_to_json(schema, row) for row in rows]
  File "site-packages/google/cloud/bigquery/client.py", line 3329, in <listcomp>
    json_rows = [_record_field_to_json(schema, row) for row in rows]
  File "site-packages/google/cloud/bigquery/_helpers.py", line 522, in _record_field_to_json
    record[subname] = _field_to_json(subfield, subvalue)
  File "site-packages/google/cloud/bigquery/_helpers.py", line 589, in _field_to_json
    return _single_field_to_json(field, row_value)
  File "site-packages/google/cloud/bigquery/_helpers.py", line 565, in _single_field_to_json
    return _scalar_field_to_json(field, row_value)
  File "site-packages/google/cloud/bigquery/_helpers.py", line 464, in _scalar_field_to_json
    return converter(row_value)
  File "site-packages/google/cloud/bigquery/_helpers.py", line 345, in _float_to_json
    elif math.isnan(value) or math.isinf(value):
TypeError: must be real number, not str

Activity

  1. product-auto-label
    added
    api: bigqueryIssues related to the googleapis/python-bigquery API.
    on Jul 27, 2021
  2. tswast commented on Jul 27, 2021

    @tswast
    tswast
    on Jul 27, 2021
    Contributor
    More actions

    Does insert_rows_json work for you? It doesn't do any type conversion.

  3. tswast
    added
    type: questionRequest for information or clarification. Not an issue.
    on Jul 27, 2021
  4. plamut
    self-assigned this
    on Jul 28, 2021
  5. plamut commented on Jul 28, 2021

    @plamut
    plamut
    on Jul 28, 2021
    Contributor
    More actions

    I was able to reproduce this and it's indeed the change in in #728 that caused this.

    Previously, the string values were simply converted to floats, but then extra checks were added to handle special float values (Nan, inf...). Unfortunately, those checks do not like strings.

    I'll submit a fix soon.

  6. plamut
    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.
    and removed
    type: questionRequest for information or clarification. Not an issue.
    on Jul 28, 2021
  7. tswast
    closed this as completedin #824on Jul 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @plamut
    plamut

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

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.