Skip to content

Commit 6bebf30

Browse files
feat: add PendingDeprecationWarning for to_dataframe and to_arrow conversion methods (#18021)
As part of the `pandas-gbq` migration and API unification strategy, this PR introduces `PendingDeprecationWarning` to the core conversion wrapper methods in `google-cloud-bigquery`. This alerts developers to adopt direct, optimized `pandas-gbq` entry points (`pandas_gbq.read_gbq()` and `pandas_gbq.arrow.*`) ahead of future deprecation phases. Fixes #<526614511> 🦕 --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent cddf35b commit 6bebf30

2 files changed

Lines changed: 201 additions & 82 deletions

File tree

packages/google-cloud-bigquery/google/cloud/bigquery/table.py

Lines changed: 79 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121
import functools
2222
import operator
2323
import typing
24-
from typing import Any, Dict, Iterable, Iterator, List, Optional, Tuple, Union, Sequence
25-
2624
import warnings
25+
from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence, Tuple, Union
2726

2827
try:
2928
import pandas # type: ignore
@@ -56,30 +55,33 @@
5655
_read_wkt = wkt.loads
5756

5857
import google.api_core.exceptions
59-
from google.api_core.page_iterator import HTTPIterator
60-
6158
import google.cloud._helpers # type: ignore
62-
from google.cloud.bigquery import _helpers
63-
from google.cloud.bigquery import _pandas_helpers
64-
from google.cloud.bigquery import _versions_helpers
59+
from google.api_core.page_iterator import HTTPIterator
60+
from google.cloud.bigquery import (
61+
_helpers,
62+
_pandas_helpers,
63+
_string_references,
64+
_versions_helpers,
65+
external_config,
66+
)
6567
from google.cloud.bigquery import exceptions as bq_exceptions
68+
from google.cloud.bigquery import schema as _schema
6669
from google.cloud.bigquery._tqdm_helpers import get_progress_bar
6770
from google.cloud.bigquery.encryption_configuration import EncryptionConfiguration
6871
from google.cloud.bigquery.enums import DefaultPandasDTypes
6972
from google.cloud.bigquery.external_config import ExternalConfig
70-
from google.cloud.bigquery import schema as _schema
71-
from google.cloud.bigquery.schema import _build_schema_resource
72-
from google.cloud.bigquery.schema import _parse_schema_resource
73-
from google.cloud.bigquery.schema import _to_schema_fields
74-
from google.cloud.bigquery import external_config
75-
from google.cloud.bigquery import _string_references
73+
from google.cloud.bigquery.schema import (
74+
_build_schema_resource,
75+
_parse_schema_resource,
76+
_to_schema_fields,
77+
)
7678

7779
if typing.TYPE_CHECKING: # pragma: NO COVER
7880
# Unconditionally import optional dependencies again to tell pytype that
7981
# they are not None, avoiding false "no attribute" errors.
82+
import geopandas # type: ignore
8083
import pandas
8184
import pyarrow
82-
import geopandas # type: ignore
8385
from google.cloud import bigquery_storage # type: ignore
8486
from google.cloud.bigquery.dataset import DatasetReference
8587

@@ -110,6 +112,17 @@
110112
"pyarrow >= 10.0.1."
111113
)
112114

115+
_TO_DATAFRAME_DEPRECATED = (
116+
"Retrieving DataFrames via google-cloud-bigquery is deprecated. "
117+
"For direct, optimized access, please call 'pandas_gbq.read_gbq()' directly."
118+
)
119+
120+
_TO_ARROW_DEPRECATED = (
121+
"Retrieving PyArrow Tables via google-cloud-bigquery is deprecated. "
122+
"For direct, optimized access, please call 'pandas_gbq.arrow.read_bigquery_table()' "
123+
"or 'pandas_gbq.arrow.read_bigquery_query()' directly."
124+
)
125+
113126
# How many of the total rows need to be downloaded already for us to skip
114127
# calling the BQ Storage API?
115128
#
@@ -2316,6 +2329,12 @@ def to_arrow(
23162329
23172330
.. versionadded:: 1.17.0
23182331
"""
2332+
warnings.warn(
2333+
_TO_ARROW_DEPRECATED,
2334+
PendingDeprecationWarning,
2335+
stacklevel=2,
2336+
)
2337+
23192338
if pyarrow is None:
23202339
raise ValueError(_NO_PYARROW_ERROR)
23212340

@@ -2709,6 +2728,12 @@ def to_dataframe(
27092728
is not supported dtype.
27102729
27112730
"""
2731+
warnings.warn(
2732+
_TO_DATAFRAME_DEPRECATED,
2733+
PendingDeprecationWarning,
2734+
stacklevel=2,
2735+
)
2736+
27122737
_pandas_helpers.verify_pandas_imports()
27132738

27142739
if geography_as_object and shapely is None:
@@ -2801,12 +2826,18 @@ def to_dataframe(
28012826
create_bqstorage_client = False
28022827
bqstorage_client = None
28032828

2804-
record_batch = self.to_arrow(
2805-
progress_bar_type=progress_bar_type,
2806-
bqstorage_client=bqstorage_client,
2807-
create_bqstorage_client=create_bqstorage_client,
2808-
timeout=timeout,
2809-
)
2829+
with warnings.catch_warnings():
2830+
warnings.filterwarnings(
2831+
"ignore",
2832+
category=PendingDeprecationWarning,
2833+
message="Retrieving PyArrow Tables.*",
2834+
)
2835+
record_batch = self.to_arrow(
2836+
progress_bar_type=progress_bar_type,
2837+
bqstorage_client=bqstorage_client,
2838+
create_bqstorage_client=create_bqstorage_client,
2839+
timeout=timeout,
2840+
)
28102841

28112842
# Default date dtype is `db_dtypes.DateDtype()` that could cause out of bounds error,
28122843
# when pyarrow converts date values to nanosecond precision. To avoid the error, we
@@ -3009,18 +3040,24 @@ def to_geodataframe(
30093040
"one to use to create a GeoDataFrame"
30103041
)
30113042

3012-
df = self.to_dataframe(
3013-
bqstorage_client,
3014-
dtypes,
3015-
progress_bar_type,
3016-
create_bqstorage_client,
3017-
geography_as_object=True,
3018-
bool_dtype=bool_dtype,
3019-
int_dtype=int_dtype,
3020-
float_dtype=float_dtype,
3021-
string_dtype=string_dtype,
3022-
timeout=timeout,
3023-
)
3043+
with warnings.catch_warnings():
3044+
warnings.filterwarnings(
3045+
"ignore",
3046+
category=PendingDeprecationWarning,
3047+
message="Retrieving DataFrames via google-cloud-bigquery is deprecated.*",
3048+
)
3049+
df = self.to_dataframe(
3050+
bqstorage_client,
3051+
dtypes,
3052+
progress_bar_type,
3053+
create_bqstorage_client,
3054+
geography_as_object=True,
3055+
bool_dtype=bool_dtype,
3056+
int_dtype=int_dtype,
3057+
float_dtype=float_dtype,
3058+
string_dtype=string_dtype,
3059+
timeout=timeout,
3060+
)
30243061

30253062
return geopandas.GeoDataFrame(
30263063
df, crs=_COORDINATE_REFERENCE_SYSTEM, geometry=geography_column
@@ -3068,6 +3105,11 @@ def to_arrow(
30683105
"""
30693106
if pyarrow is None:
30703107
raise ValueError(_NO_PYARROW_ERROR)
3108+
warnings.warn(
3109+
_TO_ARROW_DEPRECATED,
3110+
PendingDeprecationWarning,
3111+
stacklevel=2,
3112+
)
30713113
return pyarrow.Table.from_arrays(())
30723114

30733115
def to_dataframe(
@@ -3114,6 +3156,11 @@ def to_dataframe(
31143156
Returns:
31153157
pandas.DataFrame: An empty :class:`~pandas.DataFrame`.
31163158
"""
3159+
warnings.warn(
3160+
_TO_DATAFRAME_DEPRECATED,
3161+
PendingDeprecationWarning,
3162+
stacklevel=2,
3163+
)
31173164
_pandas_helpers.verify_pandas_imports()
31183165
return pandas.DataFrame()
31193166

0 commit comments

Comments
 (0)