Skip to content

Commit

Permalink
No public description
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 574297502
  • Loading branch information
blois authored and colaboratory-team committed Oct 18, 2023
1 parent 028ecb7 commit 4c69495
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions google/colab/_reprs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Rich representations of built-in types."""

import json
import warnings
# pytype: disable=import-error
import IPython
Expand Down Expand Up @@ -143,6 +144,7 @@ def _dataframe_intrinsic_repr(dataframe):
result = {
'type': 'dataframe',
}
varname = ''
if ip := IPython.get_ipython():
namespace = ip.user_ns
found = False
Expand All @@ -162,7 +164,7 @@ def _dataframe_intrinsic_repr(dataframe):
result['variable_name'] = varname
dataframe = possible_df

if summary := _summarize_dataframe(dataframe):
if summary := _summarize_dataframe(dataframe, varname):
result['summary'] = summary

return result
Expand All @@ -172,24 +174,21 @@ def _dataframe_intrinsic_repr(dataframe):
_MAX_DATAFRAME_COLS = 20


def _summarize_dataframe(df):
def _summarize_dataframe(df, variable_name):
"""Summarizes a dataframe."""
try:
import pandas as pd
from lida.components import summarizer

if len(df) > _MAX_DATAFRAME_ROWS or len(df.columns) > _MAX_DATAFRAME_COLS:
return None

return (
pd.DataFrame()
.assign(
name=df.columns.to_series(),
variance=df.var(numeric_only=True),
nunique=df.nunique(),
dtype=df.dtypes,
example_value=df.iloc[:1].T,
)
.to_string()
columns = summarizer.Summarizer().get_column_properties(df)
return json.dumps(
{
'name': variable_name,
'fields': columns,
},
indent=2,
)
except Exception: # pylint: disable=broad-except
return None

0 comments on commit 4c69495

Please sign in to comment.