@@ -579,71 +579,57 @@ def __setattr__(self, key: str, value):
579
579
object .__setattr__ (self , key , value )
580
580
581
581
def __repr__ (self ) -> str :
582
- """Converts a DataFrame to a string. Calls to_pandas .
582
+ """Converts a DataFrame to a string using pandas dataframe __repr__ .
583
583
584
- Only represents the first `bigframes.options.display.max_rows`.
584
+ Only represents the first `bigframes.options.display.max_rows`
585
+ and `bigframes.options.display.max_columns`.
585
586
"""
586
- opts = bigframes .options .display
587
- max_results = opts .max_rows
588
- if opts .repr_mode == "deferred" :
587
+ if bigframes .options .display .repr_mode == "deferred" :
589
588
return formatter .repr_query_job (self .query_job )
590
589
591
- self ._cached ()
592
- # TODO(swast): pass max_columns and get the true column count back. Maybe
593
- # get 1 more column than we have requested so that pandas can add the
594
- # ... for us?
595
- pandas_df , row_count , query_job = self ._block .retrieve_repr_request_results (
596
- max_results
597
- )
598
-
599
- self ._set_internal_query_job (query_job )
600
-
601
- column_count = len (pandas_df .columns )
602
-
603
- with display_options .pandas_repr (opts ):
590
+ pandas_df , shape = self ._perform_repr_request ()
591
+ with display_options .pandas_repr (bigframes .options .display ):
604
592
repr_string = repr (pandas_df )
605
593
606
594
# Modify the end of the string to reflect count.
607
595
lines = repr_string .split ("\n " )
608
596
pattern = re .compile ("\\ [[0-9]+ rows x [0-9]+ columns\\ ]" )
609
597
if pattern .match (lines [- 1 ]):
610
598
lines = lines [:- 2 ]
611
-
612
- if row_count > len (lines ) - 1 :
599
+ if shape [0 ] > len (lines ) - 1 :
613
600
lines .append ("..." )
614
-
615
601
lines .append ("" )
616
- lines .append (f"[{ row_count } rows x { column_count } columns]" )
602
+ lines .append (f"[{ shape [ 0 ] } rows x { shape [ 1 ] } columns]" )
617
603
return "\n " .join (lines )
618
604
605
+ def _perform_repr_request (self ) -> Tuple [pandas .DataFrame , Tuple [int , int ]]:
606
+ max_results = bigframes .options .display .max_rows
607
+ max_columns = bigframes .options .display .max_columns
608
+ self ._cached ()
609
+ pandas_df , shape , query_job = self ._block .retrieve_repr_request_results (
610
+ max_results , max_columns
611
+ )
612
+ self ._set_internal_query_job (query_job )
613
+ return pandas_df , shape
614
+
619
615
def _repr_html_ (self ) -> str :
620
616
"""
621
617
Returns an html string primarily for use by notebooks for displaying
622
- a representation of the DataFrame. Displays 20 rows by default since
623
- many notebooks are not configured for large tables.
618
+ a representation of the DataFrame. Displays at most the number of rows
619
+ and columns given by `bigframes.options.display.max_rows` and
620
+ `bigframes.options.display.max_columns`.
624
621
"""
625
- opts = bigframes .options .display
626
- max_results = bigframes .options .display .max_rows
627
- if opts .repr_mode == "deferred" :
628
- return formatter .repr_query_job_html (self .query_job )
629
622
630
- self ._cached ()
631
- # TODO(swast): pass max_columns and get the true column count back. Maybe
632
- # get 1 more column than we have requested so that pandas can add the
633
- # ... for us?
634
- pandas_df , row_count , query_job = self ._block .retrieve_repr_request_results (
635
- max_results
636
- )
637
-
638
- self ._set_internal_query_job (query_job )
623
+ if bigframes .options .display .repr_mode == "deferred" :
624
+ return formatter .repr_query_job_html (self .query_job )
639
625
640
- column_count = len ( pandas_df . columns )
626
+ pandas_df , shape = self . _perform_repr_request ( )
641
627
642
- with display_options .pandas_repr (opts ):
628
+ with display_options .pandas_repr (bigframes . options . display ):
643
629
# _repr_html_ stub is missing so mypy thinks it's a Series. Ignore mypy.
644
630
html_string = pandas_df ._repr_html_ () # type:ignore
645
631
646
- html_string += f"[{ row_count } rows x { column_count } columns in total]"
632
+ html_string += f"[{ shape [ 0 ] } rows x { shape [ 1 ] } columns in total]"
647
633
return html_string
648
634
649
635
def __setitem__ (self , key : str , value : SingleItemValue ):
0 commit comments