【问题标题】:Loading python datetime into Google BigQuery将 python 日期时间加载到 Google BigQuery 中
【发布时间】:2021-01-12 11:53:15
【问题描述】:

我正在将 Pandas 中的日期时间字段加载到 Google BigQuery DATETIME 并收到以下错误:

google.api_core.exceptions.BadRequest: 400 Error while reading data, error message: Invalid datetime value 1594835746000000 for field 'my field name' of type 'INT64' (logical type 'TIMESTAMP_MICROS'): generic::out_of_range: Cannot return an invalid datetime value of 1594835746000000 microseconds relative to the Unix epoch. The range of valid datetime values is [0001-01-1 00:00:00, 9999-12-31 23:59:59.999999]

在 Pandas 中,该对象绝对是 datetime.datetime 对象,并且具有有效日期,当我从 1594835746000000 到 https://www.epochconverter.com/ 时,它返回一个有效日期。

我正在通过调用将数据加载到 BigQuery:

job_config = bigquery.LoadJobConfig(schema = schema_fieldlist)
job = bigquery_client.load_table_from_dataframe(df, f'{dataset}.{tablename}', job_config)
job.result()

在哪里 schema_fieldlist 是一个数组,对于相关字段,定义为:

bigquery.SchemaField('my field name', 'DATETIME')

我没有做任何聪明的事情 - 任何人都可以建议他们是否可以让这个工作以及如何工作?我已经看到与返回时间戳以及标准和旧 SQL 方言之间有效范围的变化有关的其他问题

【问题讨论】:

  • 我相信这可能是当前持续存在的问题。检查this 类似的问题跟踪器。我建议尝试使用 TIMESTAMP 类型作为解决方法,看看它是否适用于您的情况。
  • 谢谢。我同意看起来一样,并且切换时间戳有效
  • pandas-gbq 使用 CSV 序列化而不是 Parquet。您也可以考虑使用它。

标签: python-3.x google-bigquery


【解决方案1】:

我在尝试使用 bigquery.client.Client.load_table_from_dataframe 将 pandas datetime.time 列插入 BigQuery 时遇到了类似问题。

错误:

BadRequest: 400 Error while reading data, error message: Invalid time value 64176000000 for column 'time': generic::out_of_range: Cannot return an invalid time value of 64176000000 microseconds relative to the Unix epoch. The range of valid time values is [00:00:00, 23:59:59.999999]

我在这里找到了解决方案:https://github.com/googleapis/python-bigquery/issues/382

source_format=bigquery.SourceFormat.CSV 添加到job_config

bq = bigquery.Client()
job_config = bigquery.LoadJobConfig(source_format=bigquery.SourceFormat.CSV)
job = bq.load_table_from_dataframe(df, "wb_dev_us.time_test", job_config=job_config)
job.result()

【讨论】:

    【解决方案2】:

    我也遇到了 DATETIME 和 TIME 的问题(报告了 https://issuetracker.google.com/issues/169230812)。

    以下是我正在使用的相关软件包版本:

       pyarrow==1.0.1
       pandas==1.1.1
       google-cloud-bigquery==1.28.0
       numpy==1.19.1
    

    TIMESTAMP 可以作为 DATETIME 的替代品,尽管它带有隐含的时区。但是,它不是 TIME 的好替代品。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多