首页 > 解决方案 > 使用 pandas 将时间戳插入 bigquery 表

问题描述

我在 Google bigquery 中有一个表,其中一列设置为数据类型时间戳。

我必须使用熊猫的 to_gbq 函数插入数据。

如果我将数据类型设置为字符串而不是时间戳,则数据将加载到表中。

但我希望该列是时间戳数据类型。

如何将数据框的列类型转换为与 Google bigquery 兼容的时间戳。

错误

在此处输入图像描述

表架构

在此处输入图像描述

标签: pandasgoogle-bigquerypython-bigquery

解决方案


Timestamp is stored as int64 datatype, or int, so if you would like to format it in such a way, you can store cast your column to this datatype in pandas.

import numpy as np
import pandas as pd
df.COLUMN_OBJECT = df.COLUMN_OBJECT.astype(np.int64)
df.COLUMN_DATETIME = df.COLUMN_DATETIME.apply(lambda x: x.timestamp).astype(np.int64)

You can have the timestamp in seconds, ms, or ns.


推荐阅读