Skip to content

Commit

Permalink
No public description
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 655276399
  • Loading branch information
colaboratory-team committed Jul 23, 2024
1 parent 4dbb466 commit 643eb21
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions google/colab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,5 @@ def load_ipython_extension(ipython):
_reprs.enable_string_repr()
# TODO(b/203704905): remove workaround when pandas fixes this issue.
_reprs.enable_df_style_formatter()
_reprs.enable_pandas_series_repr()
_interactive_table_hint_button._enable_df_interactive_hint_formatter() # pylint:disable=protected-access
29 changes: 29 additions & 0 deletions google/colab/_reprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,35 @@ def _summarize_dataframe(df, variable_name):
)


def _pandas_series_html_repr(series):
"""Renders a pandas Series as a DataFrame HTML table with a dtype label."""
series_as_table_html = series.to_frame().to_html()
series_as_table_html += f'<br><label><b>dtype:</b> {series.dtype}</label>'
return series_as_table_html


def enable_pandas_series_repr():
"""Enables pandas Series representation in HTML display formatting."""
shell = IPython.get_ipython()
if not shell:
return

html_formatter = shell.display_formatter.formatters['text/html']
html_formatter.for_type_by_name(
'pandas.core.series', 'Series', _pandas_series_html_repr
)


def disable_pandas_series_repr():
"""Disables pandas Series representation in HTML display formatting."""
shell = IPython.get_ipython()
if not shell:
return

html_formatter = shell.display_formatter.formatters['text/html']
html_formatter.pop('pandas.core.series.Series', default=None)


def _fullname(obj):
module = obj.__module__
if module == 'builtins' or module == '__main__':
Expand Down

0 comments on commit 643eb21

Please sign in to comment.