Skip to content

Commit a5832db

Browse files
committed
fix: calling info() on empty dataframes no longer leads to errors
1 parent f73fb98 commit a5832db

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

bigframes/dataframe.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,20 @@ def info(
522522
index_type = "MultiIndex" if self.index.nlevels > 1 else "Index"
523523

524524
# These accessses are kind of expensive, maybe should try to skip?
525-
first_indice = self.index[0]
526-
last_indice = self.index[-1]
527-
obuf.write(
528-
f"{index_type}: {n_rows} entries, {first_indice} to {last_indice}\n"
529-
)
525+
index_stats = f"{n_rows} entries"
526+
if n_rows > 0:
527+
first_indice = self.index[0]
528+
last_indice = self.index[-1]
529+
index_stats += f", {first_indice} to {last_indice}"
530+
obuf.write(f"{index_type}: {index_stats}\n")
530531
else:
531532
obuf.write("NullIndex\n")
532533

534+
if n_columns == 0:
535+
# We don't display any more information if the dataframe has no columns
536+
obuf.write("Empty DataFrame\n")
537+
return
538+
533539
dtype_strings = self.dtypes.astype("string")
534540
if show_all_columns:
535541
obuf.write(f"Data columns (total {n_columns} columns):\n")

tests/system/small/test_dataframe.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,15 +671,61 @@ def test_df_info(scalars_dfs):
671671
"dtypes: Float64(1), Int64(3), binary[pyarrow](1), boolean(1), date32[day][pyarrow](1), decimal128(38, 9)[pyarrow](1), duration[us][pyarrow](1), geometry(1), string(1), time64[us][pyarrow](1), timestamp[us, tz=UTC][pyarrow](1), timestamp[us][pyarrow](1)\n"
672672
"memory usage: 1341 bytes\n"
673673
)
674-
675674
scalars_df, _ = scalars_dfs
676-
bf_result = io.StringIO()
677675

676+
bf_result = io.StringIO()
678677
scalars_df.info(buf=bf_result)
679678

680679
assert expected == bf_result.getvalue()
681680

682681

682+
def test_df_info_no_rows(session):
683+
expected = (
684+
"<class 'bigframes.dataframe.DataFrame'>\n"
685+
"Index: 0 entries\n"
686+
"Data columns (total 1 columns):\n"
687+
" # Column Non-Null Count Dtype\n"
688+
"--- -------- ---------------- -------\n"
689+
" 0 col 0 non-null Float64\n"
690+
"dtypes: Float64(1)\n"
691+
"memory usage: 0 bytes\n"
692+
)
693+
df = session.DataFrame({"col": []})
694+
695+
bf_result = io.StringIO()
696+
df.info(buf=bf_result)
697+
698+
assert expected == bf_result.getvalue()
699+
700+
701+
def test_df_info_no_cols(session):
702+
expected = (
703+
"<class 'bigframes.dataframe.DataFrame'>\n"
704+
"Index: 3 entries, 1 to 3\n"
705+
"Empty DataFrame\n"
706+
)
707+
df = session.DataFrame({}, index=[1, 2, 3])
708+
709+
bf_result = io.StringIO()
710+
df.info(buf=bf_result)
711+
712+
assert expected == bf_result.getvalue()
713+
714+
715+
def test_df_info_no_cols_no_rows(session):
716+
expected = (
717+
"<class 'bigframes.dataframe.DataFrame'>\n"
718+
"Index: 0 entries\n"
719+
"Empty DataFrame\n"
720+
)
721+
df = session.DataFrame({})
722+
723+
bf_result = io.StringIO()
724+
df.info(buf=bf_result)
725+
726+
assert expected == bf_result.getvalue()
727+
728+
683729
@pytest.mark.parametrize(
684730
("include", "exclude"),
685731
[

0 commit comments

Comments
 (0)