FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-bigquery/samples/load_table_dataframe.py at main · googleapis/python-bigquery · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Mar 6, 2026. It is now read-only.
googleapis
/
python-bigquery
Public archive
Notifications
You must be signed in to change notification settings
Fork
324
Star
798
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-bigquery
/
samples
/
load_table_dataframe.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
120 lines (110 loc) · 4.43 KB
Breadcrumbs
python-bigquery
/
samples
/
load_table_dataframe.py
Copy path
File metadata and controls
120 lines (110 loc) · 4.43 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
typing
if
typing
.
TYPE_CHECKING
:
from
google
.
cloud
import
bigquery
def
load_table_dataframe
(
table_id
:
str
)
->
"bigquery.Table"
:
# [START bigquery_load_table_dataframe]
import
datetime
from
google
.
cloud
import
bigquery
import
pandas
import
pytz
# Construct a BigQuery client object.
client
=
bigquery
.
Client
()
# TODO(developer): Set table_id to the ID of the table to create.
# table_id = "your-project.your_dataset.your_table_name"
records
=
[
{
"title"
:
"The Meaning of Life"
,
"release_year"
:
1983
,
"length_minutes"
:
112.5
,
"release_date"
:
pytz
.
timezone
(
"Europe/Paris"
)
.
localize
(
datetime
.
datetime
(
1983
,
5
,
9
,
13
,
0
,
0
))
.
astimezone
(
pytz
.
utc
),
# Assume UTC timezone when a datetime object contains no timezone.
"dvd_release"
:
datetime
.
datetime
(
2002
,
1
,
22
,
7
,
0
,
0
),
},
{
"title"
:
"Monty Python and the Holy Grail"
,
"release_year"
:
1975
,
"length_minutes"
:
91.5
,
"release_date"
:
pytz
.
timezone
(
"Europe/London"
)
.
localize
(
datetime
.
datetime
(
1975
,
4
,
9
,
23
,
59
,
2
))
.
astimezone
(
pytz
.
utc
),
"dvd_release"
:
datetime
.
datetime
(
2002
,
7
,
16
,
9
,
0
,
0
),
},
{
"title"
:
"Life of Brian"
,
"release_year"
:
1979
,
"length_minutes"
:
94.25
,
"release_date"
:
pytz
.
timezone
(
"America/New_York"
)
.
localize
(
datetime
.
datetime
(
1979
,
8
,
17
,
23
,
59
,
5
))
.
astimezone
(
pytz
.
utc
),
"dvd_release"
:
datetime
.
datetime
(
2008
,
1
,
14
,
8
,
0
,
0
),
},
{
"title"
:
"And Now for Something Completely Different"
,
"release_year"
:
1971
,
"length_minutes"
:
88.0
,
"release_date"
:
pytz
.
timezone
(
"Europe/London"
)
.
localize
(
datetime
.
datetime
(
1971
,
9
,
28
,
23
,
59
,
7
))
.
astimezone
(
pytz
.
utc
),
"dvd_release"
:
datetime
.
datetime
(
2003
,
10
,
22
,
10
,
0
,
0
),
},
]
dataframe
=
pandas
.
DataFrame
(
records
,
# In the loaded table, the column order reflects the order of the
# columns in the DataFrame.
columns
=
[
"title"
,
"release_year"
,
"length_minutes"
,
"release_date"
,
"dvd_release"
,
],
# Optionally, set a named index, which can also be written to the
# BigQuery table.
index
=
pandas
.
Index
(
[
"Q24980"
,
"Q25043"
,
"Q24953"
,
"Q16403"
],
name
=
"wikidata_id"
),
)
job_config
=
bigquery
.
LoadJobConfig
(
# Specify a (partial) schema. All columns are always written to the
# table. The schema is used to assist in data type definitions.
schema
=
[
# Specify the type of columns whose type cannot be auto-detected. For
# example the "title" column uses pandas dtype "object", so its
# data type is ambiguous.
bigquery
.
SchemaField
(
"title"
,
bigquery
.
enums
.
SqlTypeNames
.
STRING
),
# Indexes are written if included in the schema by name.
bigquery
.
SchemaField
(
"wikidata_id"
,
bigquery
.
enums
.
SqlTypeNames
.
STRING
),
],
# Optionally, set the write disposition. BigQuery appends loaded rows
# to an existing table by default, but with WRITE_TRUNCATE write
# disposition it replaces the table with the loaded data.
write_disposition
=
"WRITE_TRUNCATE"
,
)
job
=
client
.
load_table_from_dataframe
(
dataframe
,
table_id
,
job_config
=
job_config
)
# Make an API request.
job
.
result
()
# Wait for the job to complete.
table
=
client
.
get_table
(
table_id
)
# Make an API request.
print
(
"Loaded {} rows and {} columns to {}"
.
format
(
table
.
num_rows
,
len
(
table
.
schema
),
table_id
)
)
# [END bigquery_load_table_dataframe]
return
table
Back
|
FazBrowse Home
|
New Git URL