TIMESTAMP returns as float, which can't be handled in python-bigquery
What happened?
When I am using python-bigquery library, I encounter an issue handling timestamps. The bigquery-emulator returns value as floats, such as 1.0, 2.0, .... However, Python can't convert floats represented as strings into integers. For example int('1.0') returns in an error.
Could you fix this issue?
https://github.com/googleapis/python-bigquery/blob/b03a2afabde7f42be45f62fabd3dc0e6a9a493e1/google/cloud/bigquery/_helpers.py#L206-L210
What did you expect to happen?
Timestamp should be handled in python-bigquery library.
How can we reproduce it (as minimally and precisely as possible)?
Data like this can't be handled in python big-query clients.
- name: created_at
type: TIMESTAMP
data:
- id: 1
created_at: "2025-01-14T12:00:00"
Anything else we need to know?
No response
I'm using this workaround for my tests:
@pytest.fixture(scope="session", autouse=True)
def _patch_big_query_timestamp_from_json() -> Generator[None]:
from google.cloud.bigquery import _helpers
real_fn = _helpers._timestamp_from_json
def _wrapper(value, field):
try:
return real_fn(value, field)
except ValueError:
return datetime.fromtimestamp(float(value))
with patch.dict(_helpers._CELLDATA_FROM_JSON, TIMESTAMP=_wrapper):
yield
Thanks, I will try it :)
This is even worse than I expected. If I save this datetime(year=1970, month=1, day=1, microsecond=1, tzinfo=UTC), the real BigQuery will return 1, as expected. One microsecond has passed since epoch. The emulator returns a float instead, which I can work around. But the emulator should return 0.000001. Instead it returns 0.1. So there's no way to know if it is 1, 10, 100, 1000, 10000 or 100000 microsecond.
Any updates on this? This is still an issue for us.