简体   繁体   English

使用熊猫将时间戳插入bigquery表中

[英]Insert timestamp into bigquery table using pandas

I have a table in Google bigquery, where a column is set to datatype timestamp. 我在Google bigquery中有一个表,其中一列设置为数据类型timestamp。

I have to insert data using to_gbq function of pandas. 我必须使用pandas的to_gbq函数插入数据。

If I set the datatype to string instead of timestamp the data is loaded into the table. 如果我将数据类型设置为字符串而不是时间戳,则数据将加载到表中。

But I want the column to be timestamp datatype. 但我希望该列为时间戳数据类型。

How can the column type of dataframe converted to timestamp compatible with Google bigquery. 数据框的列类型如何转换为与Google bigquery兼容的时间戳。

Error 错误

在此处输入图片说明

Table schema 表架构

在此处输入图片说明

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. 时间戳记存储为int64数据类型或int ,因此,如果您希望以这种方式设置其格式,则可以将列转换为熊猫格式存储到此数据类型。

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. 您可以使用秒,ms或ns为单位的时间戳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM