@@ -143,13 +143,13 @@ def _sparse_array_op(
143
143
name = name [1 :]
144
144
145
145
if name in ("and" , "or" ) and dtype == "bool" :
146
- opname = "sparse_{name}_uint8" . format ( name = name )
146
+ opname = f "sparse_{ name } _uint8"
147
147
# to make template simple, cast here
148
148
left_sp_values = left .sp_values .view (np .uint8 )
149
149
right_sp_values = right .sp_values .view (np .uint8 )
150
150
result_dtype = np .bool
151
151
else :
152
- opname = "sparse_{name}_{dtype}" . format ( name = name , dtype = dtype )
152
+ opname = f "sparse_{ name } _{ dtype } "
153
153
left_sp_values = left .sp_values
154
154
right_sp_values = right .sp_values
155
155
@@ -364,16 +364,16 @@ def __init__(
364
364
sparse_values = np .asarray (data , dtype = dtype )
365
365
if len (sparse_values ) != sparse_index .npoints :
366
366
raise AssertionError (
367
- "Non array-like type {type} must "
368
- "have the same length as the index" . format ( type = type ( sparse_values ))
367
+ f "Non array-like type { type ( sparse_values ) } must "
368
+ "have the same length as the index"
369
369
)
370
370
self ._sparse_index = sparse_index
371
371
self ._sparse_values = sparse_values
372
372
self ._dtype = SparseDtype (sparse_values .dtype , fill_value )
373
373
374
374
@classmethod
375
375
def _simple_new (
376
- cls , sparse_array : np .ndarray , sparse_index : SparseIndex , dtype : SparseDtype ,
376
+ cls , sparse_array : np .ndarray , sparse_index : SparseIndex , dtype : SparseDtype
377
377
) -> "SparseArray" :
378
378
new = cls ([])
379
379
new ._sparse_index = sparse_index
@@ -412,7 +412,7 @@ def from_spmatrix(cls, data):
412
412
length , ncol = data .shape
413
413
414
414
if ncol != 1 :
415
- raise ValueError ("'data' must have a single column, not '{}'" . format ( ncol ) )
415
+ raise ValueError (f "'data' must have a single column, not '{ ncol } '" )
416
416
417
417
# our sparse index classes require that the positions be strictly
418
418
# increasing. So we need to sort loc, and arr accordingly.
@@ -771,7 +771,7 @@ def __getitem__(self, key):
771
771
elif hasattr (key , "__len__" ):
772
772
return self .take (key )
773
773
else :
774
- raise ValueError ("Cannot slice with '{}'" . format ( key ) )
774
+ raise ValueError (f "Cannot slice with '{ key } '" )
775
775
776
776
return type (self )(data_slice , kind = self .kind )
777
777
@@ -791,9 +791,7 @@ def _get_val_at(self, loc):
791
791
792
792
def take (self , indices , allow_fill = False , fill_value = None ):
793
793
if is_scalar (indices ):
794
- raise ValueError (
795
- "'indices' must be an array, not a scalar '{}'." .format (indices )
796
- )
794
+ raise ValueError (f"'indices' must be an array, not a scalar '{ indices } '." )
797
795
indices = np .asarray (indices , dtype = np .int32 )
798
796
799
797
if indices .size == 0 :
@@ -932,8 +930,8 @@ def _concat_same_type(cls, to_concat):
932
930
if not (len (set (fill_values )) == 1 or isna (fill_values ).all ()):
933
931
warnings .warn (
934
932
"Concatenating sparse arrays with multiple fill "
935
- "values: '{}'. Picking the first and "
936
- "converting the rest." . format ( fill_values ) ,
933
+ f "values: '{ fill_values } '. Picking the first and "
934
+ "converting the rest." ,
937
935
PerformanceWarning ,
938
936
stacklevel = 6 ,
939
937
)
@@ -1153,11 +1151,7 @@ def _reduce(self, name, skipna=True, **kwargs):
1153
1151
method = getattr (self , name , None )
1154
1152
1155
1153
if method is None :
1156
- raise TypeError (
1157
- "cannot perform {name} with type {dtype}" .format (
1158
- name = name , dtype = self .dtype
1159
- )
1160
- )
1154
+ raise TypeError (f"cannot perform { name } with type { self .dtype } " )
1161
1155
1162
1156
if skipna :
1163
1157
arr = self
@@ -1253,7 +1247,7 @@ def cumsum(self, axis=0, *args, **kwargs):
1253
1247
nv .validate_cumsum (args , kwargs )
1254
1248
1255
1249
if axis is not None and axis >= self .ndim : # Mimic ndarray behaviour.
1256
- raise ValueError ("axis(={axis}) out of bounds" . format ( axis = axis ) )
1250
+ raise ValueError (f "axis(={ axis } ) out of bounds" )
1257
1251
1258
1252
if not self ._null_fill_value :
1259
1253
return SparseArray (self .to_dense ()).cumsum ()
@@ -1367,7 +1361,7 @@ def sparse_unary_method(self) -> "SparseArray":
1367
1361
dtype = SparseDtype (values .dtype , fill_value )
1368
1362
return cls ._simple_new (values , self .sp_index , dtype )
1369
1363
1370
- name = "__{name}__" . format ( name = op .__name__ )
1364
+ name = f "__{ op .__name__ } __"
1371
1365
return compat .set_function_name (sparse_unary_method , name , cls )
1372
1366
1373
1367
@classmethod
@@ -1401,11 +1395,7 @@ def sparse_arithmetic_method(self, other):
1401
1395
# TODO: look into _wrap_result
1402
1396
if len (self ) != len (other ):
1403
1397
raise AssertionError (
1404
- (
1405
- "length mismatch: {self} vs. {other}" .format (
1406
- self = len (self ), other = len (other )
1407
- )
1408
- )
1398
+ (f"length mismatch: { len (self )} vs. { len (other )} " )
1409
1399
)
1410
1400
if not isinstance (other , SparseArray ):
1411
1401
dtype = getattr (other , "dtype" , None )
@@ -1414,7 +1404,7 @@ def sparse_arithmetic_method(self, other):
1414
1404
)
1415
1405
return _sparse_array_op (self , other , op , op_name )
1416
1406
1417
- name = "__{name}__" . format ( name = op .__name__ )
1407
+ name = f "__{ op .__name__ } __"
1418
1408
return compat .set_function_name (sparse_arithmetic_method , name , cls )
1419
1409
1420
1410
@classmethod
@@ -1434,9 +1424,7 @@ def cmp_method(self, other):
1434
1424
# TODO: make this more flexible than just ndarray...
1435
1425
if len (self ) != len (other ):
1436
1426
raise AssertionError (
1437
- "length mismatch: {self} vs. {other}" .format (
1438
- self = len (self ), other = len (other )
1439
- )
1427
+ f"length mismatch: { len (self )} vs. { len (other )} "
1440
1428
)
1441
1429
other = SparseArray (other , fill_value = self .fill_value )
1442
1430
@@ -1454,7 +1442,7 @@ def cmp_method(self, other):
1454
1442
dtype = np .bool_ ,
1455
1443
)
1456
1444
1457
- name = "__{name}__" . format ( name = op .__name__ )
1445
+ name = f "__{ op .__name__ } __"
1458
1446
return compat .set_function_name (cmp_method , name , cls )
1459
1447
1460
1448
@classmethod
@@ -1473,11 +1461,10 @@ def _add_comparison_ops(cls):
1473
1461
# Formatting
1474
1462
# -----------
1475
1463
def __repr__ (self ) -> str :
1476
- return "{self}\n Fill: {fill}\n {index}" .format (
1477
- self = printing .pprint_thing (self ),
1478
- fill = printing .pprint_thing (self .fill_value ),
1479
- index = printing .pprint_thing (self .sp_index ),
1480
- )
1464
+ pp_str = printing .pprint_thing (self )
1465
+ pp_fill = printing .pprint_thing (self .fill_value )
1466
+ pp_index = printing .pprint_thing (self .sp_index )
1467
+ return f"{ pp_str } \n Fill: { pp_fill } \n { pp_index } "
1481
1468
1482
1469
def _formatter (self , boxed = False ):
1483
1470
# Defer to the formatter from the GenericArrayFormatter calling us.
0 commit comments