Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 'columns' as an alias for 'col_order' #701

Merged
merged 18 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reading.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ destination DataFrame as well as a preferred column order as follows:
'SELECT * FROM `test_dataset.test_table`',
project_id=projectid,
index_col='index_column_name',
col_order=['col1', 'col2', 'col3'])
columns=['col1', 'col2'])

Querying with legacy SQL syntax
-------------------------------
Expand Down
15 changes: 12 additions & 3 deletions pandas_gbq/gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ def read_gbq(
project_id=None,
index_col=None,
col_order=None,
columns=None,
kiraksi marked this conversation as resolved.
Show resolved Hide resolved
reauth=False,
auth_local_webserver=True,
dialect=None,
Expand Down Expand Up @@ -776,6 +777,8 @@ def read_gbq(
col_order : list(str), optional
List of BigQuery column names in the desired order for results
DataFrame.
columns : list(str), optional
Alias for col_order
kiraksi marked this conversation as resolved.
Show resolved Hide resolved
reauth : boolean, default False
Force Google BigQuery to re-authenticate the user. This is useful
if multiple accounts are used.
Expand Down Expand Up @@ -966,10 +969,16 @@ def read_gbq(
'Index column "{0}" does not exist in DataFrame.'.format(index_col)
)

# Using columns as an alias for col_order, raising an error if both provided
if col_order and not columns:
columns = col_order
elif col_order and columns:
raise ValueError("Must specify either columns or col_order, not both")
kiraksi marked this conversation as resolved.
Show resolved Hide resolved

# Change the order of columns in the DataFrame based on provided list
if col_order is not None:
if sorted(col_order) == sorted(final_df.columns):
final_df = final_df[col_order]
if columns is not None:
if sorted(columns) == sorted(final_df.columns):
kiraksi marked this conversation as resolved.
Show resolved Hide resolved
final_df = final_df[columns]
else:
raise InvalidColumnOrder("Column order does not match this DataFrame.")

Expand Down
34 changes: 34 additions & 0 deletions tests/system/test_gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,40 @@ def test_tokyo(self, tokyo_dataset, tokyo_table, project_id):
)
assert df["max_year"][0] >= 2000

def test_columns_as_alias(self, project_id):
query = "SELECT 'a' AS string_1, 'b' AS string_2, 'c' AS string_3"
columns = ["string_2", "string_1", "string_3"]

df = gbq.read_gbq(
query,
project_id=project_id,
columns=columns,
credentials=self.credentials,
dialect="standard",
)

expected = DataFrame({"string_1": ["a"], "string_2": ["b"], "string_3": ["c"]})[
columns
]

# Verify that the result_frame matches the expected DataFrame
tm.assert_frame_equal(df, expected)

def test_columns_and_col_order_raises_error(self, project_id):
query = "SELECT 'a' AS string_1, 'b' AS string_2, 'c' AS string_3"
columns = ["string_2", "string_1"]
col_order = ["string_3", "string_1", "string_2"]

with pytest.raises(ValueError):
gbq.read_gbq(
query,
project_id=project_id,
columns=columns,
col_order=col_order,
credentials=self.credentials,
dialect="standard",
)


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