Skip to content

Commit a511e09

Browse files
authored
docs: add code snippet for storing dataframes to a CSV file (#1943)
* docs: add code snippet for storing dataframes to a CSV file * fix lint * fix more lint
1 parent 03ac660 commit a511e09

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

samples/snippets/conftest.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Iterator
15+
from typing import Generator, Iterator
1616

17-
from google.cloud import bigquery
17+
from google.cloud import bigquery, storage
1818
import pytest
1919
import test_utils.prefixer
2020

@@ -42,11 +42,27 @@ def bigquery_client() -> bigquery.Client:
4242
return bigquery_client
4343

4444

45+
@pytest.fixture(scope="session")
46+
def storage_client(project_id: str) -> storage.Client:
47+
return storage.Client(project=project_id)
48+
49+
4550
@pytest.fixture(scope="session")
4651
def project_id(bigquery_client: bigquery.Client) -> str:
4752
return bigquery_client.project
4853

4954

55+
@pytest.fixture(scope="session")
56+
def gcs_bucket(storage_client: storage.Client) -> Generator[str, None, None]:
57+
bucket_name = "bigframes_blob_test"
58+
59+
yield bucket_name
60+
61+
bucket = storage_client.get_bucket(bucket_name)
62+
for blob in bucket.list_blobs():
63+
blob.delete()
64+
65+
5066
@pytest.fixture(autouse=True)
5167
def reset_session() -> None:
5268
"""An autouse fixture ensuring each sample runs in a fresh session.
@@ -78,11 +94,6 @@ def dataset_id_eu(bigquery_client: bigquery.Client, project_id: str) -> Iterator
7894
bigquery_client.delete_dataset(dataset, delete_contents=True, not_found_ok=True)
7995

8096

81-
@pytest.fixture(scope="session")
82-
def gcs_dst_bucket() -> str:
83-
return "gs://bigframes_blob_test"
84-
85-
8697
@pytest.fixture
8798
def random_model_id(
8899
bigquery_client: bigquery.Client, project_id: str, dataset_id: str

samples/snippets/multimodal_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
# limitations under the License.
1414

1515

16-
def test_multimodal_dataframe(gcs_dst_bucket: str) -> None:
16+
def test_multimodal_dataframe(gcs_bucket: str) -> None:
1717
# destination folder must be in a GCS bucket that the BQ connection service account (default or user provided) has write access to.
18-
dst_bucket = gcs_dst_bucket
18+
dst_bucket = f"gs://{gcs_bucket}"
1919
# [START bigquery_dataframes_multimodal_dataframe_create]
2020
import bigframes
2121

samples/snippets/sessions_and_io_test.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
# limitations under the License.
1414

1515

16-
def test_sessions_and_io(project_id: str, dataset_id: str) -> None:
16+
def test_sessions_and_io(project_id: str, dataset_id: str, gcs_bucket: str) -> None:
1717
YOUR_PROJECT_ID = project_id
1818
YOUR_LOCATION = "us"
19+
YOUR_BUCKET = gcs_bucket
1920

2021
# [START bigquery_dataframes_create_and_use_session_instance]
2122
import bigframes
@@ -138,6 +139,15 @@ def test_sessions_and_io(project_id: str, dataset_id: str) -> None:
138139
# [END bigquery_dataframes_read_data_from_csv]
139140
assert df is not None
140141

142+
# [START bigquery_dataframes_write_data_to_csv]
143+
import bigframes.pandas as bpd
144+
145+
df = bpd.DataFrame({"my_col": [1, 2, 3]})
146+
# Write a dataframe to a CSV file in GCS
147+
df.to_csv(f"gs://{YOUR_BUCKET}/myfile*.csv")
148+
# [END bigquery_dataframes_write_data_to_csv]
149+
assert df is not None
150+
141151
# [START bigquery_dataframes_read_data_from_bigquery_table]
142152
import bigframes.pandas as bpd
143153

0 commit comments

Comments
 (0)