首页 > 解决方案 > 在 BigQuery 中 DateTime 被强制为 UnixTime

问题描述

我在上传到 BQ 时遇到了一些问题,所以我认为在这里发帖征求意见是个好主意。我有一个医疗信息数据集(是的,我已获准将其放在 BQ 上)。数据集包含测试的总体发现和其他字符串类型的信息,BQ 可以毫无问题地接受这些信息。这是我们目前在 BQ 中期望的 Schema

a                       STRING       NULLABLE   
b                       STRING       NULLABLE   
c                       STRING       NULLABLE   
d                       STRING       NULLABLE   
reportDate              DATETIME     NULLABLE   
f                       STRING       NULLABLE   

这是我导出的 Pandas 数据框中每一列的类型,其中object只是字符串

a                                object
b                                object
c                                object
d                                object
reportDate               datetime64[ns]
f                                object

但是,它真的不喜欢我拥有的 Datatime。它只会接受它我将 Schema 中的 TYPE 设置为“INTEGER”,或者如果它让 BQ 推断类型,在这种情况下它会将其转换为整数本身。其他任何事情,它都会失败。这是我告诉它接受 datetime 作为字段类型时遇到的 3 个错误

 Error while reading data, error message: JSON table encountered too many errors, giving up. Rows: 1; errors: 1. Please look into the errors[] collection for more details. 
 Error while reading data, error message: JSON processing encountered too many errors, giving up. Rows: 1; errors: 1; max bad: 0; error percent: 0 
 Error while reading data, error message: JSON parsing error in row starting at position 0: Could not convert non-string JSON value to DATETIME type. Field: reportDate; Value: 1501874143000 

我的团队希望它作为日期时间,因为他们更容易以这种格式工作。有人对该怎么做有任何建议吗?

我已经包含了我使用的 Pandas 系列的前 5 个元素,我们可以看到 dtype 是 datetime64。我什至只过滤了这 5 个要上传到 BQ 的示例,但这个问题仍然存在。

 0   2017-08-04 19:15:43 
 1   2017-08-04 16:36:46 
 2   2017-08-04 19:15:47 
 3   2017-08-04 16:36:51 
 4   2017-08-04 17:42:21 
Name: reportDate, dtype: datetime64[ns] 

感谢任何可以提供帮助的人!

标签: datetimegoogle-cloud-platformgoogle-bigquery

解决方案


您的代码似乎将此字段保存为纪元日期(unix 时间)。由于无法将INTEGER字段加载为TIMESTAMPin BigQuery,我可以建议您两件可能的事情:

  1. 您可以将该字段加载为 anINTEGER并使用 BigQuery 的USEC_TO_TIMESTAMP函数将纪元日期转换为 TIMESTAMP。您可以在此处找到其他一些时间功能
  2. 您可以尝试搜索是否有任何方法可以使用另一种日期格式保存您的字段Pandas

希望对你有帮助


推荐阅读