@@ -2151,21 +2151,6 @@ def _formatter(self, boxed: bool = False):
2151
2151
# Defer to CategoricalFormatter's formatter.
2152
2152
return None
2153
2153
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
-
2169
2154
def _repr_categories (self ) -> list [str ]:
2170
2155
"""
2171
2156
return the base repr for the categories
@@ -2221,33 +2206,49 @@ def _repr_categories_info(self) -> str:
2221
2206
# replace to simple save space by
2222
2207
return f"{ levheader } [{ levstring .replace (' < ... < ' , ' ... ' )} ]"
2223
2208
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 :
2231
2210
from pandas .io .formats import format as fmt
2232
2211
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 ,
2235
2221
)
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
2238
2227
2239
2228
def __repr__ (self ) -> str :
2240
2229
"""
2241
2230
String representation.
2242
2231
"""
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 } "
2248
2247
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 } "
2251
2252
2252
2253
return result
2253
2254
0 commit comments