73
73
74
74
75
75
def _cat_compare_op (op ):
76
- opname = "__{op}__" . format ( op = op . __name__ )
76
+ opname = f "__{ op . __name__ } __"
77
77
78
78
@unpack_zerodim_and_defer (opname )
79
- def f (self , other ):
79
+ def func (self , other ):
80
80
# On python2, you can usually compare any type to any type, and
81
81
# Categoricals can be seen as a custom type, but having different
82
82
# results depending whether categories are the same or not is kind of
@@ -137,28 +137,26 @@ def f(self, other):
137
137
elif opname == "__ne__" :
138
138
return np .repeat (True , len (self ))
139
139
else :
140
- msg = (
141
- "Cannot compare a Categorical for op {op } with a "
140
+ raise TypeError (
141
+ f "Cannot compare a Categorical for op { opname } with a "
142
142
"scalar, which is not a category."
143
143
)
144
- raise TypeError (msg .format (op = opname ))
145
144
else :
146
145
147
146
# allow categorical vs object dtype array comparisons for equality
148
147
# these are only positional comparisons
149
148
if opname in ["__eq__" , "__ne__" ]:
150
149
return getattr (np .array (self ), opname )(np .array (other ))
151
150
152
- msg = (
153
- "Cannot compare a Categorical for op {op } with type {typ}. "
154
- " \n If you want to compare values, use 'np.asarray(cat) "
155
- "<op> other'."
151
+ raise TypeError (
152
+ f "Cannot compare a Categorical for op { opname } with "
153
+ f"type { type ( other ) } . \n If you want to compare values, "
154
+ "use 'np.asarray(cat) <op> other'."
156
155
)
157
- raise TypeError (msg .format (op = opname , typ = type (other )))
158
156
159
- f .__name__ = opname
157
+ func .__name__ = opname
160
158
161
- return f
159
+ return func
162
160
163
161
164
162
def contains (cat , key , container ):
@@ -1060,11 +1058,9 @@ def add_categories(self, new_categories, inplace=False):
1060
1058
new_categories = [new_categories ]
1061
1059
already_included = set (new_categories ) & set (self .dtype .categories )
1062
1060
if len (already_included ) != 0 :
1063
- msg = (
1064
- "new categories must not include old categories: "
1065
- "{already_included!s}"
1061
+ raise ValueError (
1062
+ f"new categories must not include old categories: { already_included } "
1066
1063
)
1067
- raise ValueError (msg .format (already_included = already_included ))
1068
1064
new_categories = list (self .dtype .categories ) + list (new_categories )
1069
1065
new_dtype = CategoricalDtype (new_categories , self .ordered )
1070
1066
@@ -1120,8 +1116,7 @@ def remove_categories(self, removals, inplace=False):
1120
1116
new_categories = [x for x in new_categories if notna (x )]
1121
1117
1122
1118
if len (not_included ) != 0 :
1123
- msg = "removals must all be in old categories: {not_included!s}"
1124
- raise ValueError (msg .format (not_included = not_included ))
1119
+ raise ValueError (f"removals must all be in old categories: { not_included } " )
1125
1120
1126
1121
return self .set_categories (
1127
1122
new_categories , ordered = self .ordered , rename = False , inplace = inplace
@@ -1299,9 +1294,8 @@ def shift(self, periods, fill_value=None):
1299
1294
fill_value = self .categories .get_loc (fill_value )
1300
1295
else :
1301
1296
raise ValueError (
1302
- "'fill_value={}' is not present "
1303
- "in this Categorical's "
1304
- "categories" .format (fill_value )
1297
+ f"'fill_value={ fill_value } ' is not present "
1298
+ "in this Categorical's categories"
1305
1299
)
1306
1300
if periods > 0 :
1307
1301
codes [:periods ] = fill_value
@@ -1342,8 +1336,8 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
1342
1336
# for all other cases, raise for now (similarly as what happens in
1343
1337
# Series.__array_prepare__)
1344
1338
raise TypeError (
1345
- "Object with dtype {dtype} cannot perform "
1346
- "the numpy op {op}" . format ( dtype = self . dtype , op = ufunc .__name__ )
1339
+ f "Object with dtype { self . dtype } cannot perform "
1340
+ f "the numpy op { ufunc .__name__ } "
1347
1341
)
1348
1342
1349
1343
def __setstate__ (self , state ):
@@ -1542,9 +1536,9 @@ def check_for_ordered(self, op):
1542
1536
""" assert that we are ordered """
1543
1537
if not self .ordered :
1544
1538
raise TypeError (
1545
- "Categorical is not ordered for operation {op}\n "
1539
+ f "Categorical is not ordered for operation { op } \n "
1546
1540
"you can use .as_ordered() to change the "
1547
- "Categorical to an ordered one\n " . format ( op = op )
1541
+ "Categorical to an ordered one\n "
1548
1542
)
1549
1543
1550
1544
def _values_for_argsort (self ):
@@ -1679,8 +1673,7 @@ def sort_values(self, inplace=False, ascending=True, na_position="last"):
1679
1673
"""
1680
1674
inplace = validate_bool_kwarg (inplace , "inplace" )
1681
1675
if na_position not in ["last" , "first" ]:
1682
- msg = "invalid na_position: {na_position!r}"
1683
- raise ValueError (msg .format (na_position = na_position ))
1676
+ raise ValueError (f"invalid na_position: { na_position !r} " )
1684
1677
1685
1678
sorted_idx = nargsort (self , ascending = ascending , na_position = na_position )
1686
1679
@@ -1836,8 +1829,7 @@ def fillna(self, value=None, method=None, limit=None):
1836
1829
else :
1837
1830
raise TypeError (
1838
1831
'"value" parameter must be a scalar, dict '
1839
- "or Series, but you passed a "
1840
- '"{0}"' .format (type (value ).__name__ )
1832
+ f'or Series, but you passed a { type (value ).__name__ !r} "'
1841
1833
)
1842
1834
1843
1835
return self ._constructor (codes , dtype = self .dtype , fastpath = True )
@@ -1930,8 +1922,11 @@ def take_nd(self, indexer, allow_fill=None, fill_value=None):
1930
1922
if fill_value in self .categories :
1931
1923
fill_value = self .categories .get_loc (fill_value )
1932
1924
else :
1933
- msg = "'fill_value' ('{}') is not in this Categorical's categories."
1934
- raise TypeError (msg .format (fill_value ))
1925
+ msg = (
1926
+ f"'fill_value' ('{ fill_value } ') is not in this "
1927
+ "Categorical's categories."
1928
+ )
1929
+ raise TypeError (msg )
1935
1930
1936
1931
codes = take (self ._codes , indexer , allow_fill = allow_fill , fill_value = fill_value )
1937
1932
result = type (self ).from_codes (codes , dtype = dtype )
@@ -1969,11 +1964,9 @@ def _tidy_repr(self, max_vals=10, footer=True):
1969
1964
head = self [:num ]._get_repr (length = False , footer = False )
1970
1965
tail = self [- (max_vals - num ) :]._get_repr (length = False , footer = False )
1971
1966
1972
- result = "{head}, ..., {tail}" . format ( head = head [: - 1 ], tail = tail [ 1 :])
1967
+ result = f "{ head [: - 1 ] } , ..., { tail [ 1 :] } "
1973
1968
if footer :
1974
- result = "{result}\n {footer}" .format (
1975
- result = result , footer = self ._repr_footer ()
1976
- )
1969
+ result = f"{ result } \n { self ._repr_footer ()} "
1977
1970
1978
1971
return str (result )
1979
1972
@@ -2007,9 +2000,7 @@ def _repr_categories_info(self):
2007
2000
2008
2001
category_strs = self ._repr_categories ()
2009
2002
dtype = str (self .categories .dtype )
2010
- levheader = "Categories ({length}, {dtype}): " .format (
2011
- length = len (self .categories ), dtype = dtype
2012
- )
2003
+ levheader = f"Categories ({ len (self .categories )} , { dtype } ): "
2013
2004
width , height = get_terminal_size ()
2014
2005
max_width = get_option ("display.width" ) or width
2015
2006
if console .in_ipython_frontend ():
@@ -2033,10 +2024,8 @@ def _repr_categories_info(self):
2033
2024
return levheader + "[" + levstring .replace (" < ... < " , " ... " ) + "]"
2034
2025
2035
2026
def _repr_footer (self ):
2036
-
2037
- return "Length: {length}\n {info}" .format (
2038
- length = len (self ), info = self ._repr_categories_info ()
2039
- )
2027
+ info = self ._repr_categories_info ()
2028
+ return f"Length: { len (self )} \n { info } "
2040
2029
2041
2030
def _get_repr (self , length = True , na_rep = "NaN" , footer = True ):
2042
2031
from pandas .io .formats import format as fmt
@@ -2058,7 +2047,7 @@ def __repr__(self) -> str:
2058
2047
result = self ._get_repr (length = len (self ) > _maxlen )
2059
2048
else :
2060
2049
msg = self ._get_repr (length = False , footer = True ).replace ("\n " , ", " )
2061
- result = "[], {repr_msg}" . format ( repr_msg = msg )
2050
+ result = f "[], { msg } "
2062
2051
2063
2052
return result
2064
2053
@@ -2189,8 +2178,7 @@ def _reverse_indexer(self):
2189
2178
def _reduce (self , name , axis = 0 , ** kwargs ):
2190
2179
func = getattr (self , name , None )
2191
2180
if func is None :
2192
- msg = "Categorical cannot perform the operation {op}"
2193
- raise TypeError (msg .format (op = name ))
2181
+ raise TypeError (f"Categorical cannot perform the operation { name } " )
2194
2182
return func (** kwargs )
2195
2183
2196
2184
def min (self , numeric_only = None , ** kwargs ):
@@ -2458,11 +2446,10 @@ def isin(self, values):
2458
2446
array([ True, False, True, False, True, False])
2459
2447
"""
2460
2448
if not is_list_like (values ):
2449
+ values_type = type (values ).__name__
2461
2450
raise TypeError (
2462
2451
"only list-like objects are allowed to be passed"
2463
- " to isin(), you passed a [{values_type}]" .format (
2464
- values_type = type (values ).__name__
2465
- )
2452
+ f" to isin(), you passed a [{ values_type } ]"
2466
2453
)
2467
2454
values = sanitize_array (values , None , None )
2468
2455
null_mask = np .asarray (isna (values ))
0 commit comments