@@ -159,10 +159,10 @@ def __new__(cls, data, closed=None, dtype=None, copy=False, verify_integrity=Tru
159
159
# don't allow scalars
160
160
if is_scalar (data ):
161
161
msg = (
162
- "{ }(...) must be called with a collection of some kind, "
163
- " { } was passed"
162
+ f" { cls . __name__ } (...) must be called with a collection "
163
+ f"of some kind, { data } was passed"
164
164
)
165
- raise TypeError (msg . format ( cls . __name__ , data ) )
165
+ raise TypeError (msg )
166
166
167
167
# might need to convert empty or purely na data
168
168
data = maybe_convert_platform_interval (data )
@@ -194,8 +194,8 @@ def _simple_new(
194
194
# GH 19262: dtype must be an IntervalDtype to override inferred
195
195
dtype = pandas_dtype (dtype )
196
196
if not is_interval_dtype (dtype ):
197
- msg = "dtype must be an IntervalDtype, got {dtype}"
198
- raise TypeError (msg . format ( dtype = dtype ) )
197
+ msg = f "dtype must be an IntervalDtype, got { dtype } "
198
+ raise TypeError (msg )
199
199
elif dtype .subtype is not None :
200
200
left = left .astype (dtype .subtype )
201
201
right = right .astype (dtype .subtype )
@@ -207,10 +207,11 @@ def _simple_new(
207
207
left = left .astype (right .dtype )
208
208
209
209
if type (left ) != type (right ):
210
- msg = "must not have differing left [{ltype}] and right [{rtype}] types"
211
- raise ValueError (
212
- msg . format ( ltype = type ( left ). __name__ , rtype = type (right ).__name__ )
210
+ msg = (
211
+ f"must not have differing left [ { type ( left ). __name__ } ] and "
212
+ f"right [ { type (right ).__name__ } ] types"
213
213
)
214
+ raise ValueError (msg )
214
215
elif is_categorical_dtype (left .dtype ) or is_string_dtype (left .dtype ):
215
216
# GH 19016
216
217
msg = (
@@ -224,9 +225,9 @@ def _simple_new(
224
225
elif isinstance (left , ABCDatetimeIndex ) and str (left .tz ) != str (right .tz ):
225
226
msg = (
226
227
"left and right must have the same time zone, got "
227
- "'{left_tz }' and '{right_tz }'"
228
+ f "'{ left . tz } ' and '{ right . tz } '"
228
229
)
229
- raise ValueError (msg . format ( left_tz = left . tz , right_tz = right . tz ) )
230
+ raise ValueError (msg )
230
231
231
232
result ._left = left
232
233
result ._right = right
@@ -443,14 +444,10 @@ def from_tuples(cls, data, closed="right", copy=False, dtype=None):
443
444
# need list of length 2 tuples, e.g. [(0, 1), (1, 2), ...]
444
445
lhs , rhs = d
445
446
except ValueError :
446
- msg = (
447
- "{name}.from_tuples requires tuples of length 2, got {tpl}"
448
- ).format (name = name , tpl = d )
447
+ msg = f"{ name } .from_tuples requires tuples of length 2, got { d } "
449
448
raise ValueError (msg )
450
449
except TypeError :
451
- msg = ("{name}.from_tuples received an invalid item, {tpl}" ).format (
452
- name = name , tpl = d
453
- )
450
+ msg = f"{ name } .from_tuples received an invalid item, { d } "
454
451
raise TypeError (msg )
455
452
left .append (lhs )
456
453
right .append (rhs )
@@ -468,20 +465,22 @@ def _validate(self):
468
465
* left is always below right
469
466
"""
470
467
if self .closed not in _VALID_CLOSED :
471
- raise ValueError (
472
- "invalid option for 'closed': {closed}" .format (closed = self .closed )
473
- )
468
+ msg = f"invalid option for 'closed': { self .closed } "
469
+ raise ValueError (msg )
474
470
if len (self .left ) != len (self .right ):
475
- raise ValueError ("left and right must have the same length" )
471
+ msg = "left and right must have the same length"
472
+ raise ValueError (msg )
476
473
left_mask = notna (self .left )
477
474
right_mask = notna (self .right )
478
475
if not (left_mask == right_mask ).all ():
479
- raise ValueError (
476
+ msg = (
480
477
"missing values must be missing in the same "
481
478
"location both left and right sides"
482
479
)
480
+ raise ValueError (msg )
483
481
if not (self .left [left_mask ] <= self .right [left_mask ]).all ():
484
- raise ValueError ("left side of interval must be <= right side" )
482
+ msg = "left side of interval must be <= right side"
483
+ raise ValueError (msg )
485
484
486
485
# ---------
487
486
# Interface
@@ -531,8 +530,8 @@ def __setitem__(self, key, value):
531
530
value_left , value_right = array .left , array .right
532
531
except TypeError :
533
532
# wrong type: not interval or NA
534
- msg = "'value' should be an interval type, got {} instead."
535
- raise TypeError (msg . format ( type ( value )) )
533
+ msg = f "'value' should be an interval type, got { type ( value ) } instead."
534
+ raise TypeError (msg )
536
535
537
536
# Need to ensure that left and right are updated atomically, so we're
538
537
# forced to copy, update the copy, and swap in the new values.
@@ -583,9 +582,7 @@ def fillna(self, value=None, method=None, limit=None):
583
582
if not isinstance (value , ABCInterval ):
584
583
msg = (
585
584
"'IntervalArray.fillna' only supports filling with a "
586
- "scalar 'pandas.Interval'. Got a '{}' instead." .format (
587
- type (value ).__name__
588
- )
585
+ f"scalar 'pandas.Interval'. Got a '{ type (value ).__name__ } ' instead."
589
586
)
590
587
raise TypeError (msg )
591
588
@@ -630,19 +627,18 @@ def astype(self, dtype, copy=True):
630
627
new_right = self .right .astype (dtype .subtype )
631
628
except TypeError :
632
629
msg = (
633
- "Cannot convert {dtype} to {new_dtype}; subtypes are "
634
- "incompatible"
630
+ f"Cannot convert { self .dtype } to { dtype } ; subtypes are incompatible"
635
631
)
636
- raise TypeError (msg . format ( dtype = self . dtype , new_dtype = dtype ) )
632
+ raise TypeError (msg )
637
633
return self ._shallow_copy (new_left , new_right )
638
634
elif is_categorical_dtype (dtype ):
639
635
return Categorical (np .asarray (self ))
640
636
# TODO: This try/except will be repeated.
641
637
try :
642
638
return np .asarray (self ).astype (dtype , copy = copy )
643
639
except (TypeError , ValueError ):
644
- msg = "Cannot cast {name } to dtype {dtype}"
645
- raise TypeError (msg . format ( name = type ( self ). __name__ , dtype = dtype ) )
640
+ msg = f "Cannot cast { type ( self ). __name__ } to dtype { dtype } "
641
+ raise TypeError (msg )
646
642
647
643
@classmethod
648
644
def _concat_same_type (cls , to_concat ):
@@ -790,9 +786,8 @@ def take(self, indices, allow_fill=False, fill_value=None, axis=None, **kwargs):
790
786
elif not is_scalar (fill_value ) and notna (fill_value ):
791
787
msg = (
792
788
"'IntervalArray.fillna' only supports filling with a "
793
- "'scalar pandas.Interval or NA'. Got a '{}' instead." .format (
794
- type (fill_value ).__name__
795
- )
789
+ "'scalar pandas.Interval or NA'. "
790
+ f"Got a '{ type (fill_value ).__name__ } ' instead."
796
791
)
797
792
raise ValueError (msg )
798
793
@@ -840,48 +835,44 @@ def _format_data(self):
840
835
summary = "[]"
841
836
elif n == 1 :
842
837
first = formatter (self [0 ])
843
- summary = "[{first}]" . format ( first = first )
838
+ summary = f "[{ first } ]"
844
839
elif n == 2 :
845
840
first = formatter (self [0 ])
846
841
last = formatter (self [- 1 ])
847
- summary = "[{first}, {last}]" . format ( first = first , last = last )
842
+ summary = f "[{ first } , { last } ]"
848
843
else :
849
844
850
845
if n > max_seq_items :
851
846
n = min (max_seq_items // 2 , 10 )
852
847
head = [formatter (x ) for x in self [:n ]]
853
848
tail = [formatter (x ) for x in self [- n :]]
854
- summary = "[{head} ... {tail}]" . format (
855
- head = ", " . join ( head ), tail = ", " .join (tail )
856
- )
849
+ head_str = ", " . join ( head )
850
+ tail_str = ", " .join (tail )
851
+ summary = f"[ { head_str } ... { tail_str } ]"
857
852
else :
858
853
tail = [formatter (x ) for x in self ]
859
- summary = "[{tail}]" .format (tail = ", " .join (tail ))
854
+ tail_str = ", " .join (tail )
855
+ summary = f"[{ tail_str } ]"
860
856
861
857
return summary
862
858
863
859
def __repr__ (self ) -> str :
864
- template = (
865
- "{class_name}"
866
- "{data}\n "
867
- "Length: {length}, closed: {closed}, dtype: {dtype}"
868
- )
869
860
# the short repr has no trailing newline, while the truncated
870
861
# repr does. So we include a newline in our template, and strip
871
862
# any trailing newlines from format_object_summary
872
863
data = self ._format_data ()
873
- class_name = "<{}>\n " .format (type (self ).__name__ )
874
- return template .format (
875
- class_name = class_name ,
876
- data = data ,
877
- length = len (self ),
878
- closed = self .closed ,
879
- dtype = self .dtype ,
864
+ class_name = f"<{ type (self ).__name__ } >\n "
865
+
866
+ template = (
867
+ f"{ class_name } "
868
+ f"{ data } \n "
869
+ f"Length: { len (self )} , closed: { self .closed } , dtype: { self .dtype } "
880
870
)
871
+ return template
881
872
882
873
def _format_space (self ):
883
874
space = " " * (len (type (self ).__name__ ) + 1 )
884
- return "\n {space}" . format ( space = space )
875
+ return f "\n { space } "
885
876
886
877
@property
887
878
def left (self ):
@@ -951,8 +942,8 @@ def closed(self):
951
942
)
952
943
def set_closed (self , closed ):
953
944
if closed not in _VALID_CLOSED :
954
- msg = "invalid option for 'closed': {closed}"
955
- raise ValueError (msg . format ( closed = closed ) )
945
+ msg = f "invalid option for 'closed': { closed } "
946
+ raise ValueError (msg )
956
947
957
948
return self ._shallow_copy (closed = closed )
958
949
@@ -1188,8 +1179,8 @@ def overlaps(self, other):
1188
1179
if isinstance (other , (IntervalArray , ABCIntervalIndex )):
1189
1180
raise NotImplementedError
1190
1181
elif not isinstance (other , Interval ):
1191
- msg = "`other` must be Interval-like, got {other}"
1192
- raise TypeError (msg . format ( other = type ( other ). __name__ ) )
1182
+ msg = f "`other` must be Interval-like, got { type ( other ). __name__ } "
1183
+ raise TypeError (msg )
1193
1184
1194
1185
# equality is okay if both endpoints are closed (overlap at a point)
1195
1186
op1 = le if (self .closed_left and other .closed_right ) else lt
0 commit comments