Skip to content

fix: Fix header skipping logic in read_csv #49

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

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 3 additions & 4 deletions bigframes/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,11 +1050,10 @@ def read_csv(
# We want to match pandas behavior. If header is 0, no rows should be skipped, so we
# do not need to set `skip_leading_rows`. If header is None, then there is no header.
# Setting skip_leading_rows to 0 does that. If header=N and N>0, we want to skip N rows.
# `skip_leading_rows` skips N-1 rows, so we set it to header+1.
if header is not None and header > 0:
job_config.skip_leading_rows = header + 1
elif header is None:
if header is None:
job_config.skip_leading_rows = 0
elif header > 0:
job_config.skip_leading_rows = header

return self._read_bigquery_load_job(
filepath_or_buffer,
Expand Down
18 changes: 12 additions & 6 deletions tests/system/small/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,12 @@ def test_read_csv_gcs_bq_engine_w_header(session, scalars_df_index, gcs_folder):
path = gcs_folder + "test_read_csv_gcs_bq_engine_w_header*.csv"
scalars_df_index.to_csv(path, index=False)

# Skip the header and the first 2 data rows. Without provided schema, the column names
# would be like `bool_field_0`, `string_field_1` and etc.
df = session.read_csv(path, header=2, engine="bigquery")
# Skip the header and the first 2 data rows. Note that one line of header
# also got added while writing the csv through `to_csv`, so we would have to
# pass headers=3 in the `read_csv` to skip reading the header and two rows.
# Without provided schema, the column names would be like `bool_field_0`,
# `string_field_1` and etc.
df = session.read_csv(path, header=3, engine="bigquery")
assert df.shape[0] == scalars_df_index.shape[0] - 2
assert len(df.columns) == len(scalars_df_index.columns)

Expand Down Expand Up @@ -609,9 +612,12 @@ def test_read_csv_local_bq_engine_w_header(session, scalars_pandas_df_index):
# Using the pandas to_csv method because the BQ one does not support local write.
scalars_pandas_df_index.to_csv(path, index=False)

# Skip the header and the first 2 data rows. Without provided schema, the column names
# would be like `bool_field_0`, `string_field_1` and etc.
df = session.read_csv(path, header=2, engine="bigquery")
# Skip the header and the first 2 data rows. Note that one line of
# header also got added while writing the csv through `to_csv`, so we
# would have to pass headers=3 in the `read_csv` to skip reading the
# header and two rows. Without provided schema, the column names would
# be like `bool_field_0`, `string_field_1` and etc.
df = session.read_csv(path, header=3, engine="bigquery")
assert df.shape[0] == scalars_pandas_df_index.shape[0] - 2
assert len(df.columns) == len(scalars_pandas_df_index.columns)

Expand Down