@@ -411,7 +411,7 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
411
411
arr = np .array (data , dtype = dtype , copy = copy )
412
412
except (ValueError , TypeError ) as e :
413
413
exc = TypeError ('DataFrame constructor called with '
414
- 'incompatible data and dtype: %s' % e )
414
+ 'incompatible data and dtype: {e}' . format ( e = e ) )
415
415
raise_with_traceback (exc )
416
416
417
417
if arr .ndim == 0 and index is not None and columns is not None :
@@ -520,8 +520,9 @@ def _get_axes(N, K, index=index, columns=columns):
520
520
try :
521
521
values = values .astype (dtype )
522
522
except Exception as orig :
523
- e = ValueError ("failed to cast to '%s' (Exception was: %s)"
524
- % (dtype , orig ))
523
+ e = ValueError ("failed to cast to '{dtype}' (Exception "
524
+ "was: {orig})" .format (dtype = dtype ,
525
+ orig = orig ))
525
526
raise_with_traceback (e )
526
527
527
528
index , columns = _get_axes (* values .shape )
@@ -873,8 +874,9 @@ def dot(self, other):
873
874
lvals = self .values
874
875
rvals = np .asarray (other )
875
876
if lvals .shape [1 ] != rvals .shape [0 ]:
876
- raise ValueError ('Dot product shape mismatch, %s vs %s' %
877
- (lvals .shape , rvals .shape ))
877
+ raise ValueError ('Dot product shape mismatch, '
878
+ '{l} vs {r}' .format (l = lvals .shape ,
879
+ r = rvals .shape ))
878
880
879
881
if isinstance (other , DataFrame ):
880
882
return self ._constructor (np .dot (lvals , rvals ), index = left .index ,
@@ -888,7 +890,7 @@ def dot(self, other):
888
890
else :
889
891
return Series (result , index = left .index )
890
892
else : # pragma: no cover
891
- raise TypeError ('unsupported type: %s' % type (other ))
893
+ raise TypeError ('unsupported type: {oth}' . format ( oth = type (other ) ))
892
894
893
895
def __matmul__ (self , other ):
894
896
""" Matrix multiplication using binary `@` operator in Python>=3.5 """
@@ -1098,7 +1100,7 @@ def to_dict(self, orient='dict', into=dict):
1098
1100
return into_c ((t [0 ], dict (zip (self .columns , t [1 :])))
1099
1101
for t in self .itertuples ())
1100
1102
else :
1101
- raise ValueError ("orient '%s ' not understood" % orient )
1103
+ raise ValueError ("orient '{o} ' not understood" . format ( o = orient ) )
1102
1104
1103
1105
def to_gbq (self , destination_table , project_id , chunksize = None ,
1104
1106
verbose = None , reauth = False , if_exists = 'fail' , private_key = None ,
@@ -2140,7 +2142,7 @@ def info(self, verbose=None, buf=None, max_cols=None, memory_usage=None,
2140
2142
lines .append (self .index ._summary ())
2141
2143
2142
2144
if len (self .columns ) == 0 :
2143
- lines .append ('Empty %s' % type (self ).__name__ )
2145
+ lines .append ('Empty {name}' . format ( name = type (self ).__name__ ) )
2144
2146
fmt .buffer_put_lines (buf , lines )
2145
2147
return
2146
2148
@@ -2166,13 +2168,15 @@ def _verbose_repr():
2166
2168
space = max (len (pprint_thing (k )) for k in self .columns ) + 4
2167
2169
counts = None
2168
2170
2169
- tmpl = "%s%s "
2171
+ tmpl = "{count}{dtype} "
2170
2172
if show_counts :
2171
2173
counts = self .count ()
2172
2174
if len (cols ) != len (counts ): # pragma: no cover
2173
- raise AssertionError ('Columns must equal counts (%d != %d)'
2174
- % (len (cols ), len (counts )))
2175
- tmpl = "%s non-null %s"
2175
+ raise AssertionError (
2176
+ 'Columns must equal counts '
2177
+ '({cols:d} != {counts:d})' .format (
2178
+ cols = len (cols ), counts = len (counts )))
2179
+ tmpl = "{count} non-null {dtype}"
2176
2180
2177
2181
dtypes = self .dtypes
2178
2182
for i , col in enumerate (self .columns ):
@@ -2183,7 +2187,8 @@ def _verbose_repr():
2183
2187
if show_counts :
2184
2188
count = counts .iloc [i ]
2185
2189
2186
- lines .append (_put_str (col , space ) + tmpl % (count , dtype ))
2190
+ lines .append (_put_str (col , space ) + tmpl .format (count = count ,
2191
+ dtype = dtype ))
2187
2192
2188
2193
def _non_verbose_repr ():
2189
2194
lines .append (self .columns ._summary (name = 'Columns' ))
@@ -2192,9 +2197,12 @@ def _sizeof_fmt(num, size_qualifier):
2192
2197
# returns size in human readable format
2193
2198
for x in ['bytes' , 'KB' , 'MB' , 'GB' , 'TB' ]:
2194
2199
if num < 1024.0 :
2195
- return "%3.1f%s %s" % (num , size_qualifier , x )
2200
+ return ("{num:3.1f}{size_q}"
2201
+ "{x}" .format (num = num , size_q = size_qualifier , x = x ))
2196
2202
num /= 1024.0
2197
- return "%3.1f%s %s" % (num , size_qualifier , 'PB' )
2203
+ return "{num:3.1f}{size_q} {pb}" .format (num = num ,
2204
+ size_q = size_qualifier ,
2205
+ pb = 'PB' )
2198
2206
2199
2207
if verbose :
2200
2208
_verbose_repr ()
@@ -2207,8 +2215,9 @@ def _sizeof_fmt(num, size_qualifier):
2207
2215
_verbose_repr ()
2208
2216
2209
2217
counts = self .get_dtype_counts ()
2210
- dtypes = ['%s(%d)' % k for k in sorted (compat .iteritems (counts ))]
2211
- lines .append ('dtypes: %s' % ', ' .join (dtypes ))
2218
+ dtypes = ['{k}({kk:d})' .format (k = k [0 ], kk = k [1 ]) for k
2219
+ in sorted (compat .iteritems (counts ))]
2220
+ lines .append ('dtypes: {types}' .format (types = ', ' .join (dtypes )))
2212
2221
2213
2222
if memory_usage is None :
2214
2223
memory_usage = get_option ('display.memory_usage' )
@@ -2226,8 +2235,9 @@ def _sizeof_fmt(num, size_qualifier):
2226
2235
self .index ._is_memory_usage_qualified ()):
2227
2236
size_qualifier = '+'
2228
2237
mem_usage = self .memory_usage (index = True , deep = deep ).sum ()
2229
- lines .append ("memory usage: %s\n " %
2230
- _sizeof_fmt (mem_usage , size_qualifier ))
2238
+ lines .append ("memory usage: {mem}\n " .format (
2239
+ mem = _sizeof_fmt (mem_usage , size_qualifier )))
2240
+
2231
2241
fmt .buffer_put_lines (buf , lines )
2232
2242
2233
2243
def memory_usage (self , index = True , deep = False ):
@@ -3013,8 +3023,8 @@ def select_dtypes(self, include=None, exclude=None):
3013
3023
3014
3024
# can't both include AND exclude!
3015
3025
if not include .isdisjoint (exclude ):
3016
- raise ValueError ('include and exclude overlap on %s' %
3017
- (include & exclude ))
3026
+ raise ValueError ('include and exclude overlap on {inc_ex}' . format (
3027
+ inc_ex = (include & exclude ) ))
3018
3028
3019
3029
# empty include/exclude -> defaults to True
3020
3030
# three cases (we've already raised if both are empty)
@@ -3869,7 +3879,8 @@ def set_index(self, keys, drop=True, append=False, inplace=False,
3869
3879
3870
3880
if verify_integrity and not index .is_unique :
3871
3881
duplicates = index .get_duplicates ()
3872
- raise ValueError ('Index has duplicate keys: %s' % duplicates )
3882
+ raise ValueError ('Index has duplicate keys: {dup}' .format (
3883
+ dup = duplicates ))
3873
3884
3874
3885
for c in to_remove :
3875
3886
del frame [c ]
@@ -4241,7 +4252,7 @@ def dropna(self, axis=0, how='any', thresh=None, subset=None,
4241
4252
mask = count > 0
4242
4253
else :
4243
4254
if how is not None :
4244
- raise ValueError ('invalid how option: %s' % how )
4255
+ raise ValueError ('invalid how option: {h}' . format ( h = how ) )
4245
4256
else :
4246
4257
raise TypeError ('must specify how or thresh' )
4247
4258
@@ -6750,8 +6761,8 @@ def _count_level(self, level, axis=0, numeric_only=False):
6750
6761
agg_axis = frame ._get_agg_axis (axis )
6751
6762
6752
6763
if not isinstance (count_axis , MultiIndex ):
6753
- raise TypeError ("Can only count levels on hierarchical %s." %
6754
- self ._get_axis_name (axis ))
6764
+ raise TypeError ("Can only count levels on hierarchical "
6765
+ "{ax}." . format ( ax = self ._get_axis_name (axis ) ))
6755
6766
6756
6767
if frame ._is_mixed_type :
6757
6768
# Since we have mixed types, calling notna(frame.values) might
@@ -6829,9 +6840,9 @@ def f(x):
6829
6840
elif filter_type == 'bool' :
6830
6841
data = self ._get_bool_data ()
6831
6842
else : # pragma: no cover
6832
- e = NotImplementedError ("Handling exception with filter_"
6833
- "type %s not implemented." %
6834
- filter_type )
6843
+ e = NotImplementedError (
6844
+ "Handling exception with filter_type {f} not"
6845
+ "implemented." . format ( f = filter_type ) )
6835
6846
raise_with_traceback (e )
6836
6847
with np .errstate (all = 'ignore' ):
6837
6848
result = f (data .values )
@@ -6843,8 +6854,8 @@ def f(x):
6843
6854
elif filter_type == 'bool' :
6844
6855
data = self ._get_bool_data ()
6845
6856
else : # pragma: no cover
6846
- msg = ("Generating numeric_only data with filter_type %s "
6847
- "not supported." % filter_type )
6857
+ msg = ("Generating numeric_only data with filter_type {f} "
6858
+ "not supported." . format ( f = filter_type ) )
6848
6859
raise NotImplementedError (msg )
6849
6860
values = data .values
6850
6861
labels = data ._get_agg_axis (axis )
@@ -7119,7 +7130,8 @@ def to_timestamp(self, freq=None, how='start', axis=0, copy=True):
7119
7130
elif axis == 1 :
7120
7131
new_data .set_axis (0 , self .columns .to_timestamp (freq = freq , how = how ))
7121
7132
else : # pragma: no cover
7122
- raise AssertionError ('Axis must be 0 or 1. Got %s' % str (axis ))
7133
+ raise AssertionError ('Axis must be 0 or 1. Got {ax!s}' .format (
7134
+ ax = axis ))
7123
7135
7124
7136
return self ._constructor (new_data )
7125
7137
@@ -7150,7 +7162,8 @@ def to_period(self, freq=None, axis=0, copy=True):
7150
7162
elif axis == 1 :
7151
7163
new_data .set_axis (0 , self .columns .to_period (freq = freq ))
7152
7164
else : # pragma: no cover
7153
- raise AssertionError ('Axis must be 0 or 1. Got %s' % str (axis ))
7165
+ raise AssertionError ('Axis must be 0 or 1. Got {ax!s}' .format (
7166
+ ax = axis ))
7154
7167
7155
7168
return self ._constructor (new_data )
7156
7169
@@ -7509,8 +7522,9 @@ def _convert_object_array(content, columns, coerce_float=False, dtype=None):
7509
7522
else :
7510
7523
if len (columns ) != len (content ): # pragma: no cover
7511
7524
# caller's responsibility to check for this...
7512
- raise AssertionError ('%d columns passed, passed data had %s '
7513
- 'columns' % (len (columns ), len (content )))
7525
+ raise AssertionError ('{col:d} columns passed, passed data had '
7526
+ '{con} columns' .format (col = len (columns ),
7527
+ con = len (content )))
7514
7528
7515
7529
# provide soft conversion of object dtypes
7516
7530
def convert (arr ):
@@ -7585,4 +7599,4 @@ def _from_nested_dict(data):
7585
7599
7586
7600
7587
7601
def _put_str (s , space ):
7588
- return ( '%s' % s )[:space ].ljust (space )
7602
+ return u'{s}' . format ( s = s )[:space ].ljust (space )
0 commit comments