Skip to content

Commit

Permalink
No public description
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 566653529
  • Loading branch information
blois authored and colaboratory-team committed Sep 19, 2023
1 parent 50cb41a commit 11b74fe
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 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 re
import warnings
# pytype: disable=import-error
import IPython
Expand Down Expand Up @@ -145,10 +146,24 @@ def _dataframe_intrinsic_repr(dataframe):
}
if ip := IPython.get_ipython():
namespace = ip.user_ns
found = False
for varname, var in namespace.items():
if dataframe is var and not varname.startswith('_'):
result['variable_name'] = varname
found = True
break
if not found:
last_line = (
ip.parent_header.get('content', {}).get('code', '').split('\n')[-1]
)
if match := re.match(r'^([a-z_A-Z0-9]+).head(\s?[0-9]*\s?)', last_line):
import pandas as pd

varname = match.groups()[0]
possible_df = ip.user_ns.get(varname)
if isinstance(possible_df, pd.DataFrame):
result['variable_name'] = varname
dataframe = possible_df

if summary := _summarize_dataframe(dataframe):
result['summary'] = summary
Expand Down

0 comments on commit 11b74fe

Please sign in to comment.