Skip to content

Commit a298a02

Browse files
authored
docs: add code snippet for storing dataframes to a CSV file (#1953)
* Revert "Revert "docs: add code snippet for storing dataframes to a CSV file (…" This reverts commit 0468a4d. * update the bucket name
1 parent b6aeca3 commit a298a02

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_with_data_wipeout"
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,10 +13,11 @@
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_DATASET_ID = dataset_id
1919
YOUR_LOCATION = "us"
20+
YOUR_BUCKET = gcs_bucket
2021

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

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

0 commit comments

Comments
 (0)