@@ -2151,21 +2151,6 @@ def _formatter(self, boxed: bool = False):
21512151 # Defer to CategoricalFormatter's formatter.
21522152 return None
21532153
2154- def _tidy_repr (self , max_vals : int = 10 , footer : bool = True ) -> str :
2155- """
2156- a short repr displaying only max_vals and an optional (but default
2157- footer)
2158- """
2159- num = max_vals // 2
2160- head = self [:num ]._get_repr (length = False , footer = False )
2161- tail = self [- (max_vals - num ) :]._get_repr (length = False , footer = False )
2162-
2163- result = f"{ head [:- 1 ]} , ..., { tail [1 :]} "
2164- if footer :
2165- result = f"{ result } \n { self ._repr_footer ()} "
2166-
2167- return str (result )
2168-
21692154 def _repr_categories (self ) -> list [str ]:
21702155 """
21712156 return the base repr for the categories
@@ -2221,33 +2206,49 @@ def _repr_categories_info(self) -> str:
22212206 # replace to simple save space by
22222207 return f"{ levheader } [{ levstring .replace (' < ... < ' , ' ... ' )} ]"
22232208
2224- def _repr_footer (self ) -> str :
2225- info = self ._repr_categories_info ()
2226- return f"Length: { len (self )} \n { info } "
2227-
2228- def _get_repr (
2229- self , length : bool = True , na_rep : str = "NaN" , footer : bool = True
2230- ) -> str :
2209+ def _get_values_repr (self ) -> str :
22312210 from pandas .io .formats import format as fmt
22322211
2233- formatter = fmt .CategoricalFormatter (
2234- self , length = length , na_rep = na_rep , footer = footer
2212+ assert len (self ) > 0
2213+
2214+ vals = self ._internal_get_values ()
2215+ fmt_values = fmt .format_array (
2216+ vals ,
2217+ None ,
2218+ float_format = None ,
2219+ na_rep = "NaN" ,
2220+ quoting = QUOTE_NONNUMERIC ,
22352221 )
2236- result = formatter .to_string ()
2237- return str (result )
2222+
2223+ fmt_values = [i .strip () for i in fmt_values ]
2224+ joined = ", " .join (fmt_values )
2225+ result = "[" + joined + "]"
2226+ return result
22382227
22392228 def __repr__ (self ) -> str :
22402229 """
22412230 String representation.
22422231 """
2243- _maxlen = 10
2244- if len (self ._codes ) > _maxlen :
2245- result = self ._tidy_repr (_maxlen )
2246- elif len (self ._codes ) > 0 :
2247- result = self ._get_repr (length = len (self ) > _maxlen )
2232+ footer = self ._repr_categories_info ()
2233+ length = len (self )
2234+ max_len = 10
2235+ if length > max_len :
2236+ # In long cases we do not display all entries, so we add Length
2237+ # information to the __repr__.
2238+ num = max_len // 2
2239+ head = self [:num ]._get_values_repr ()
2240+ tail = self [- (max_len - num ) :]._get_values_repr ()
2241+ body = f"{ head [:- 1 ]} , ..., { tail [1 :]} "
2242+ length_info = f"Length: { len (self )} "
2243+ result = f"{ body } \n { length_info } \n { footer } "
2244+ elif length > 0 :
2245+ body = self ._get_values_repr ()
2246+ result = f"{ body } \n { footer } "
22482247 else :
2249- msg = self ._get_repr (length = False , footer = True ).replace ("\n " , ", " )
2250- result = f"[], { msg } "
2248+ # In the empty case we use a comma instead of newline to get
2249+ # a more compact __repr__
2250+ body = "[]"
2251+ result = f"{ body } , { footer } "
22512252
22522253 return result
22532254
0 commit comments