Skip to content
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
18 changes: 12 additions & 6 deletions bigframes/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,15 +521,21 @@ def info(
if self._block.has_index:
index_type = "MultiIndex" if self.index.nlevels > 1 else "Index"

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

if n_columns == 0:
# We don't display any more information if the dataframe has no columns
obuf.write("Empty DataFrame\n")
return

dtype_strings = self.dtypes.astype("string")
if show_all_columns:
obuf.write(f"Data columns (total {n_columns} columns):\n")
Expand Down
50 changes: 48 additions & 2 deletions tests/system/small/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,15 +671,61 @@ def test_df_info(scalars_dfs):
"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"
"memory usage: 1341 bytes\n"
)

scalars_df, _ = scalars_dfs
bf_result = io.StringIO()

bf_result = io.StringIO()
scalars_df.info(buf=bf_result)

assert expected == bf_result.getvalue()


def test_df_info_no_rows(session):
expected = (
"<class 'bigframes.dataframe.DataFrame'>\n"
"Index: 0 entries\n"
"Data columns (total 1 columns):\n"
" # Column Non-Null Count Dtype\n"
"--- -------- ---------------- -------\n"
" 0 col 0 non-null Float64\n"
"dtypes: Float64(1)\n"
"memory usage: 0 bytes\n"
)
df = session.DataFrame({"col": []})

bf_result = io.StringIO()
df.info(buf=bf_result)

assert expected == bf_result.getvalue()


def test_df_info_no_cols(session):
expected = (
"<class 'bigframes.dataframe.DataFrame'>\n"
"Index: 3 entries, 1 to 3\n"
"Empty DataFrame\n"
)
df = session.DataFrame({}, index=[1, 2, 3])

bf_result = io.StringIO()
df.info(buf=bf_result)

assert expected == bf_result.getvalue()


def test_df_info_no_cols_no_rows(session):
expected = (
"<class 'bigframes.dataframe.DataFrame'>\n"
"Index: 0 entries\n"
"Empty DataFrame\n"
)
df = session.DataFrame({})

bf_result = io.StringIO()
df.info(buf=bf_result)

assert expected == bf_result.getvalue()


@pytest.mark.parametrize(
("include", "exclude"),
[
Expand Down