Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
BigQuery将日期插入到';日期';使用Python Google云库的类型字段_Python_Google Bigquery - Fatal编程技术网

BigQuery将日期插入到';日期';使用Python Google云库的类型字段

BigQuery将日期插入到';日期';使用Python Google云库的类型字段,python,google-bigquery,Python,Google Bigquery,我正在使用Python 2.7和Google Cloud Client Library for Python(v0.27.0)将数据插入BigQuery表(使用table.insert_data()) 我的表中有一个字段的类型为“DATE” 在我的Python脚本中,我将日期数据格式化为'YYYY-MM-DD',但不幸的是,Google云库为该字段返回了一个'Invalid date:'错误 我尝试过用多种方式格式化日期字段(如“YYYYMMDD”、时间戳等),但到目前为止运气不佳 不幸的是,A

我正在使用Python 2.7和Google Cloud Client Library for Python(v0.27.0)将数据插入BigQuery表(使用table.insert_data())

我的表中有一个字段的类型为“DATE”

在我的Python脚本中,我将日期数据格式化为'YYYY-MM-DD',但不幸的是,Google云库为该字段返回了一个'Invalid date:'错误

我尝试过用多种方式格式化日期字段(如“YYYYMMDD”、时间戳等),但到目前为止运气不佳

不幸的是,API文档()没有提到Python中所需的日期格式/类型/对象

这是我的代码:

from google.cloud import bigquery
import pandas as pd
import json
from pprint import pprint
from collections import OrderedDict

# Using a pandas dataframe 'df' as input

# Converting date field to YYYY-MM-DD format

df['DATE_VALUE_LOCAL'] = df['DATE_VALUE_LOCAL'].apply(lambda x: x.strftime('%Y-%m-%d'))

# Converting pandas dataframe to json

json_data = df.to_json(orient='records',date_format='iso')

# Instantiates a client
bigquery_client = bigquery.Client(project="xxx")

# The name for the new dataset
dataset_name = 'dataset_name'
table_name = 'table_name'

def stream_data(dataset_name, table_name, json_data):
    dataset = bigquery_client.dataset(dataset_name)
    table = dataset.table(table_name)

    data = json.loads(json_data, object_pairs_hook=OrderedDict)

    # Reload the table to get the schema.
    table.reload()

    errors = table.insert_data(data)

    if not errors:
        print('Loaded 1 row into {}:{}'.format(dataset_name, table_name))
    else:
        print('Errors:')
        pprint(errors)

stream_data(dataset_name, table_name, json_data)

在BigQuery日期字段中插入日期所需的Python日期格式/类型/对象是什么?

我只是在这里模拟了您的代码,一切正常。以下是我模拟的:

import pandas as pd
import json
import os
from collections import OrderedDict
from google.cloud.bigquery import Client

d = {'ed': ['3', '5'],
     'date': ['2017-10-11', '2017-11-12']}
json_data = df.to_json(orient='records', date_formate='iso')
json_data = json.loads(json_data, object_pairs_hook=OrderedDict)

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/key.json'
bc = Client()
ds = bc.dataset('dataset name')
table = ds.table('table I just created')
table = bc.get_table(table)
bc.create_rows(table, json_data)
它使用的是版本
0.28.0
,但仍然是与以前版本相同的方法


您可能在某些步骤中发生了一些错误,可能是将日期转换为BQ的其他无法识别的格式。尝试使用此脚本作为参考,查看代码中可能发生的错误。

这很奇怪。实际上,它应该已经工作了……您使用的是什么版本的python客户端?您也可以分享您的脚本吗?我正在使用Python客户端的v0.28.0。我已经用一个代码示例更新了我的问题。Thnx!您可能正在使用v0.27.0,如0.28中的方法
insert_data
no-Ah,客户机在21小时前已更新。。你完全正确,我用的是0.27.0。我将重写代码,使其在0.28.0上运行,并检查问题是否得到解决