|
21 | 21 | import functools |
22 | 22 | import operator |
23 | 23 | import typing |
24 | | -from typing import Any, Dict, Iterable, Iterator, List, Optional, Tuple, Union, Sequence |
25 | | - |
26 | 24 | import warnings |
| 25 | +from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence, Tuple, Union |
27 | 26 |
|
28 | 27 | try: |
29 | 28 | import pandas # type: ignore |
|
56 | 55 | _read_wkt = wkt.loads |
57 | 56 |
|
58 | 57 | import google.api_core.exceptions |
59 | | -from google.api_core.page_iterator import HTTPIterator |
60 | | - |
61 | 58 | 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 | +) |
65 | 67 | from google.cloud.bigquery import exceptions as bq_exceptions |
| 68 | +from google.cloud.bigquery import schema as _schema |
66 | 69 | from google.cloud.bigquery._tqdm_helpers import get_progress_bar |
67 | 70 | from google.cloud.bigquery.encryption_configuration import EncryptionConfiguration |
68 | 71 | from google.cloud.bigquery.enums import DefaultPandasDTypes |
69 | 72 | 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 | +) |
76 | 78 |
|
77 | 79 | if typing.TYPE_CHECKING: # pragma: NO COVER |
78 | 80 | # Unconditionally import optional dependencies again to tell pytype that |
79 | 81 | # they are not None, avoiding false "no attribute" errors. |
| 82 | + import geopandas # type: ignore |
80 | 83 | import pandas |
81 | 84 | import pyarrow |
82 | | - import geopandas # type: ignore |
83 | 85 | from google.cloud import bigquery_storage # type: ignore |
84 | 86 | from google.cloud.bigquery.dataset import DatasetReference |
85 | 87 |
|
@@ -2709,6 +2711,14 @@ def to_dataframe( |
2709 | 2711 | is not supported dtype. |
2710 | 2712 |
|
2711 | 2713 | """ |
| 2714 | + if not _versions_helpers.PANDAS_GBQ_VERSIONS.is_delegation_supported: |
| 2715 | + warnings.warn( |
| 2716 | + "Retrieving dataframes via the core client is deprecated. " |
| 2717 | + "Please install 'pandas-gbq' for the new high-performance backend.", |
| 2718 | + PendingDeprecationWarning, |
| 2719 | + stacklevel=2, |
| 2720 | + ) |
| 2721 | + |
2712 | 2722 | _pandas_helpers.verify_pandas_imports() |
2713 | 2723 |
|
2714 | 2724 | if geography_as_object and shapely is None: |
@@ -2801,6 +2811,37 @@ def to_dataframe( |
2801 | 2811 | create_bqstorage_client = False |
2802 | 2812 | bqstorage_client = None |
2803 | 2813 |
|
| 2814 | + if _versions_helpers.PANDAS_GBQ_VERSIONS.is_delegation_supported: |
| 2815 | + import pandas_gbq # type: ignore |
| 2816 | + |
| 2817 | + if self.client and hasattr(self.client, "_connection") and hasattr(self.client._connection, "_client_info"): |
| 2818 | + client_info = self.client._connection._client_info |
| 2819 | + if client_info: |
| 2820 | + ua = client_info.user_agent or "" |
| 2821 | + if "pandas-gbq" not in ua: |
| 2822 | + client_info.user_agent = f"{ua} pandas-gbq/{pandas_gbq.__version__}".strip() |
| 2823 | + |
| 2824 | + return pandas_gbq.pandas.from_row_iterator( |
| 2825 | + self, |
| 2826 | + bqstorage_client=bqstorage_client, |
| 2827 | + dtypes=dtypes, |
| 2828 | + progress_bar_type=progress_bar_type, |
| 2829 | + create_bqstorage_client=create_bqstorage_client, |
| 2830 | + geography_as_object=geography_as_object, |
| 2831 | + bool_dtype=bool_dtype, |
| 2832 | + int_dtype=int_dtype, |
| 2833 | + float_dtype=float_dtype, |
| 2834 | + string_dtype=string_dtype, |
| 2835 | + date_dtype=date_dtype, |
| 2836 | + datetime_dtype=datetime_dtype, |
| 2837 | + time_dtype=time_dtype, |
| 2838 | + timestamp_dtype=timestamp_dtype, |
| 2839 | + range_date_dtype=range_date_dtype, |
| 2840 | + range_datetime_dtype=range_datetime_dtype, |
| 2841 | + range_timestamp_dtype=range_timestamp_dtype, |
| 2842 | + timeout=timeout, |
| 2843 | + ) |
| 2844 | + |
2804 | 2845 | record_batch = self.to_arrow( |
2805 | 2846 | progress_bar_type=progress_bar_type, |
2806 | 2847 | bqstorage_client=bqstorage_client, |
|
0 commit comments