Skip to content

Commit b024e93

Browse files
authored
Add max_results kwarg to read_gbq (#34639) (#34641)
Since max_results is a new kwarg (added in pandas-gbq 0.12.0), it is handled and tested in the same way as use_bqstorage_api, using the "new kwargs" mechanism to maintain backwards compatibility with older pandas-gbq versions.
1 parent 6010141 commit b024e93

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ Other enhancements
292292
- :meth:`groupby.transform` now allows ``func`` to be ``pad``, ``backfill`` and ``cumcount`` (:issue:`31269`).
293293
- :meth:`~pandas.io.json.read_json` now accepts `nrows` parameter. (:issue:`33916`).
294294
- :meth `~pandas.io.gbq.read_gbq` now allows to disable progress bar (:issue:`33360`).
295+
- :meth:`~pandas.io.gbq.read_gbq` now supports the ``max_results`` kwarg from ``pandas-gbq`` (:issue:`34639`).
295296

296297
.. ---------------------------------------------------------------------------
297298

pandas/io/gbq.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def read_gbq(
3030
configuration: Optional[Dict[str, Any]] = None,
3131
credentials=None,
3232
use_bqstorage_api: Optional[bool] = None,
33+
max_results: Optional[int] = None,
3334
private_key=None,
3435
verbose=None,
3536
progress_bar_type: Optional[str] = None,
@@ -125,6 +126,13 @@ def read_gbq(
125126
``fastavro`` packages.
126127
127128
.. versionadded:: 0.25.0
129+
max_results : int, optional
130+
If set, limit the maximum number of rows to fetch from the query
131+
results.
132+
133+
*New in version 0.12.0 of pandas-gbq*.
134+
135+
.. versionadded:: 1.1.0
128136
progress_bar_type : Optional, str
129137
If set, use the `tqdm <https://tqdm.github.io/>`__ library to
130138
display a progress bar while the data downloads. Install the
@@ -162,11 +170,13 @@ def read_gbq(
162170
"""
163171
pandas_gbq = _try_import()
164172

165-
kwargs: Dict[str, Union[str, bool, None]] = {}
173+
kwargs: Dict[str, Union[str, bool, int, None]] = {}
166174

167175
# START: new kwargs. Don't populate unless explicitly set.
168176
if use_bqstorage_api is not None:
169177
kwargs["use_bqstorage_api"] = use_bqstorage_api
178+
if max_results is not None:
179+
kwargs["max_results"] = max_results
170180

171181
kwargs["progress_bar_type"] = progress_bar_type
172182
# END: new kwargs

pandas/tests/io/test_gbq.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,10 @@ def mock_read_gbq(sql, **kwargs):
113113
return DataFrame([[1.0]])
114114

115115
monkeypatch.setattr("pandas_gbq.read_gbq", mock_read_gbq)
116-
pd.read_gbq("SELECT 1", use_bqstorage_api=True)
116+
pd.read_gbq("SELECT 1", use_bqstorage_api=True, max_results=1)
117117

118118
assert captured_kwargs["use_bqstorage_api"]
119+
assert captured_kwargs["max_results"]
119120

120121

121122
def test_read_gbq_without_new_kwargs(monkeypatch):
@@ -129,6 +130,7 @@ def mock_read_gbq(sql, **kwargs):
129130
pd.read_gbq("SELECT 1")
130131

131132
assert "use_bqstorage_api" not in captured_kwargs
133+
assert "max_results" not in captured_kwargs
132134

133135

134136
@pytest.mark.parametrize("progress_bar", [None, "foo"])

0 commit comments

Comments
 (0)