datetime - 在 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]
感谢任何可以提供帮助的人!
解决方案
您的代码似乎将此字段保存为纪元日期(unix 时间)。由于无法将INTEGER
字段加载为TIMESTAMP
in BigQuery
,我可以建议您两件可能的事情:
- 您可以将该字段加载为 an
INTEGER
并使用 BigQuery 的USEC_TO_TIMESTAMP
函数将纪元日期转换为 TIMESTAMP。您可以在此处找到其他一些时间功能 - 您可以尝试搜索是否有任何方法可以使用另一种日期格式保存您的字段
Pandas
希望对你有帮助
推荐阅读
- python - 打开 csv 文件而不自动分离内容
- arrays - Postgres将多维数组嵌套到列?
- python - 如何格式化 2018 年 1 月,即如何在 python 中获取 mon-year 格式
- android - 设备屏幕关闭时如何打开手电筒?
- python - ModuleNotFoundError:没有名为“tkinter”/ PyCharm 的模块
- docker - 撰写时需要Dockerfile?
- c - 警告:函数“timerisset”的隐式声明
- python - Jupyter notebook 不运行任何命令
- c++14 - 特殊最小生成树
- python - 在获得不同上下文的同时使用 pandas.DataFrame.values?