1717from __future__ import annotations
1818
1919import typing
20- from typing import Union
2120
2221import pandas
2322import pandas .io .formats
2726if typing .TYPE_CHECKING :
2827 import pandas as pd
2928
30- import bigframes .dataframe
31- import bigframes .series
32-
3329
3430def create_text_representation (
35- obj : Union [bigframes .dataframe .DataFrame , bigframes .series .Series ],
3631 pandas_df : pd .DataFrame ,
3732 total_rows : typing .Optional [int ],
33+ is_series : bool ,
34+ has_index : bool = True ,
35+ column_count : int = 0 ,
3836) -> str :
39- """Create a text representation of the DataFrame or Series."""
40- from bigframes .series import Series
41-
37+ """Create a text representation of the DataFrame or Series.
38+
39+ Args:
40+ pandas_df:
41+ The pandas DataFrame containing the data to represent.
42+ total_rows:
43+ The total number of rows in the original BigFrames object.
44+ is_series:
45+ Whether the object being represented is a Series.
46+ has_index:
47+ Whether the object has an index to display.
48+ column_count:
49+ The total number of columns in the original BigFrames object.
50+ Only used for DataFrames.
51+
52+ Returns:
53+ A plaintext string representation.
54+ """
4255 opts = options .display
4356
44- if isinstance ( obj , Series ) :
57+ if is_series :
4558 with display_options .pandas_repr (opts ):
4659 pd_series = pandas_df .iloc [:, 0 ]
47- if len ( obj . _block . index_columns ) == 0 :
60+ if not has_index :
4861 repr_string = pd_series .to_string (
4962 length = False , index = False , name = True , dtype = True
5063 )
@@ -68,7 +81,7 @@ def create_text_representation(
6881 to_string_kwargs = (
6982 pandas .io .formats .format .get_dataframe_repr_params () # type: ignore
7083 )
71- if not obj . _has_index :
84+ if not has_index :
7285 to_string_kwargs .update ({"index" : False })
7386
7487 # We add our own dimensions string, so don't want pandas to.
@@ -81,11 +94,9 @@ def create_text_representation(
8194 if is_truncated :
8295 lines .append ("..." )
8396 lines .append ("" ) # Add empty line for spacing only if truncated
84- column_count = len (obj .columns )
8597 lines .append (f"[{ total_rows or '?' } rows x { column_count } columns]" )
8698 else :
8799 # For non-truncated DataFrames, we still need to add dimensions if show_dimensions was False
88- column_count = len (obj .columns )
89100 lines .append ("" )
90101 lines .append (f"[{ total_rows or '?' } rows x { column_count } columns]" )
91102 return "\n " .join (lines )
0 commit comments