Skip to content

Commit 7abf188

Browse files
committed
feat: add 'columns' as an alias for 'col_order'
1 parent 4051266 commit 7abf188

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

docs/reading.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ destination DataFrame as well as a preferred column order as follows:
2828
'SELECT * FROM `test_dataset.test_table`',
2929
project_id=projectid,
3030
index_col='index_column_name',
31-
col_order=['col1', 'col2', 'col3'])
31+
col_order=['col1', 'col2', 'col3'],
32+
columns=['col1', 'col2'])
3233
3334
Querying with legacy SQL syntax
3435
-------------------------------

pandas_gbq/gbq.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ def read_gbq(
733733
project_id=None,
734734
index_col=None,
735735
col_order=None,
736+
columns=None,
736737
reauth=False,
737738
auth_local_webserver=True,
738739
dialect=None,
@@ -774,6 +775,8 @@ def read_gbq(
774775
col_order : list(str), optional
775776
List of BigQuery column names in the desired order for results
776777
DataFrame.
778+
columns : list(str), optional
779+
List of BigQuery column names to return, alias for col_order
777780
reauth : boolean, default False
778781
Force Google BigQuery to re-authenticate the user. This is useful
779782
if multiple accounts are used.
@@ -964,6 +967,9 @@ def read_gbq(
964967
'Index column "{0}" does not exist in DataFrame.'.format(index_col)
965968
)
966969

970+
# Creating an alias for col_order, which is columns
971+
col_order = col_order or columns
972+
967973
# Change the order of columns in the DataFrame based on provided list
968974
if col_order is not None:
969975
if sorted(col_order) == sorted(final_df.columns):

tests/system/test_gbq.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,26 @@ def test_tokyo(self, tokyo_dataset, tokyo_table, project_id):
600600
)
601601
assert df["max_year"][0] >= 2000
602602

603+
def test_columns_and_col_order(self, project_id):
604+
query = "SELECT 'a' AS string_1, 'b' AS string_2, 'c' AS string_3"
605+
columns = ["string_2", "string_1"]
606+
col_order = ["string_3", "string_1", "string_2"]
607+
result_frame = gbq.read_gbq(
608+
query,
609+
project_id=project_id,
610+
columns=columns,
611+
col_order=col_order,
612+
credentials=self.credentials,
613+
dialect="standard",
614+
)
615+
correct_frame = DataFrame(
616+
{"string_1": ["a"], "string_2": ["b"], "string_3": ["c"]}
617+
)[col_order]
618+
tm.assert_frame_equal(result_frame, correct_frame)
619+
620+
# Verify that col_order is prioritized over columns
621+
assert sorted(col_order) == sorted(result_frame.columns)
622+
603623

604624
class TestToGBQIntegration(object):
605625
@pytest.fixture(autouse=True, scope="function")

0 commit comments

Comments
 (0)