Skip to content

Commit d6ec1d6

Browse files
Brian TuBrian Tu
Brian Tu
authored and
Brian Tu
committed
Fix PEP8 issues, backward compatibility with 2.7 (no exception chaining)
1 parent 6da7805 commit d6ec1d6

File tree

12 files changed

+66
-54
lines changed

12 files changed

+66
-54
lines changed

pandas/core/indexes/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2596,7 +2596,7 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
25962596
if isinstance(tolerance, np.ndarray) and \
25972597
target.size != tolerance.size and tolerance.size > 1:
25982598
raise ValueError('ndarray tolerance size must match '
2599-
'target index size')
2599+
'target index size')
26002600

26012601
pself, ptarget = self._maybe_promote(target)
26022602
if pself is not self or ptarget is not target:

pandas/core/indexes/datetimelike.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from pandas._libs.period import Period
2929

3030
from pandas.core.indexes.base import (Index, _index_shared_docs,
31-
_list_to_ndarray)
31+
_list_to_ndarray)
3232
from pandas.util._decorators import Appender, cache_readonly
3333
import pandas.core.dtypes.concat as _concat
3434
import pandas.tseries.frequencies as frequencies
@@ -440,28 +440,29 @@ def _convert_tolerance(self, tolerance):
440440
else:
441441
try:
442442
tolerance = np.array([np.timedelta64(x)
443-
for x in tolerance])
443+
for x in tolerance])
444444
# in case user mixes something like seconds and Month
445445
if not np.issubdtype(tolerance.dtype, np.timedelta64):
446446
raise TypeError('All values in tolerance array must '
447-
'be convertible to [ns]')
448-
except ValueError as e:
447+
'be convertible to [ns]')
448+
except ValueError:
449449
raise TypeError(('tolerance argument for %s must contain '
450-
'objects convertible to np.timedelta64 '
451-
'if it is list type') %
452-
(type(self).__name__,)) from e
450+
'objects convertible to np.timedelta64 '
451+
'if it is list type') %
452+
(type(self).__name__,))
453453
else:
454454
warnings.warn('Converting tolerance array to '
455-
'np.timedelta64 objects, consider doing preconverting '
456-
'for speed')
455+
'np.timedelta64 objects, consider doing '
456+
'preconverting for speed')
457457
return tolerance
458458
else:
459459
try:
460460
return Timedelta(tolerance).to_timedelta64()
461461
except ValueError:
462462
raise ValueError(('tolerance argument for %s must be '
463-
'convertible to Timedelta if it is a scalar: %r')
464-
% (type(self).__name__, tolerance))
463+
'convertible to Timedelta if it is a '
464+
'scalar: %r') %
465+
(type(self).__name__, tolerance))
465466

466467
def _maybe_mask_results(self, result, fill_value=None, convert=None):
467468
"""

pandas/core/indexes/numeric.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,15 @@ def _convert_tolerance(self, tolerance):
7878
return tolerance
7979
else:
8080
raise ValueError(('tolerance argument for %s must contain '
81-
'numeric elements if it is list type') % (type(self).__name__,))
81+
'numeric elements if it is list type') %
82+
(type(self).__name__,))
8283
else:
8384
try:
8485
return float(tolerance)
8586
except ValueError:
8687
raise ValueError(('tolerance argument for %s must be numeric '
87-
'if it is a scalar: %r') %
88-
(type(self).__name__, tolerance))
88+
'if it is a scalar: %r') %
89+
(type(self).__name__, tolerance))
8990

9091
@classmethod
9192
def _assert_safe_casting(cls, data, subarr):

pandas/core/indexes/period.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,8 @@ def to_timestamp(self, freq=None, how='start'):
633633
return DatetimeIndex(new_data, freq='infer', name=self.name)
634634

635635
def _maybe_convert_timedelta(self, other):
636-
if isinstance(other,
637-
(timedelta, np.timedelta64, offsets.Tick, np.ndarray)):
636+
if isinstance(
637+
other, (timedelta, np.timedelta64, offsets.Tick, np.ndarray)):
638638
offset = frequencies.to_offset(self.freq.rule_code)
639639
if isinstance(offset, offsets.Tick):
640640
nanos = tslib._delta_to_nanoseconds(other)
@@ -783,7 +783,7 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
783783
if isinstance(tolerance, np.ndarray) and \
784784
target.size != tolerance.size and tolerance.size > 1:
785785
raise ValueError('ndarray tolerance size must match '
786-
'target index size')
786+
'target index size')
787787
return Index.get_indexer(self._int64index, target, method,
788788
limit, tolerance)
789789

@@ -914,7 +914,7 @@ def _convert_tolerance(self, tolerance):
914914
if isinstance(tolerance, np.ndarray) \
915915
and not np.issubdtype(tolerance.dtype, np.timedelta64):
916916
raise TypeError('All values in tolerance array must be '
917-
'convertible to [ns]')
917+
'convertible to [ns]')
918918
return self._maybe_convert_timedelta(tolerance)
919919

920920
def insert(self, loc, item):

pandas/tests/frame/test_indexing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,12 +1905,12 @@ def test_reindex_methods(self):
19051905

19061906
actual = df.reindex_like(df, method=method, tolerance=0)
19071907
assert_frame_equal(df, actual)
1908-
actual = df.reindex_like(df, method=method, tolerance=[0,0,0,0])
1908+
actual = df.reindex_like(df, method=method, tolerance=[0, 0, 0, 0])
19091909
assert_frame_equal(df, actual)
19101910

19111911
actual = df.reindex(target, method=method, tolerance=1)
19121912
assert_frame_equal(expected, actual)
1913-
actual = df.reindex(target, method=method, tolerance=[1,1,1,1])
1913+
actual = df.reindex(target, method=method, tolerance=[1, 1, 1, 1])
19141914
assert_frame_equal(expected, actual)
19151915

19161916
e2 = expected[::-1]
@@ -1934,7 +1934,7 @@ def test_reindex_methods(self):
19341934

19351935
expected = pd.DataFrame({'x': [0, np.nan, 1, np.nan]}, index=target)
19361936
actual = df.reindex(target, method='nearest',
1937-
tolerance=[0.5, 0.01, 0.4, 0.1])
1937+
tolerance=[0.5, 0.01, 0.4, 0.1])
19381938
assert_frame_equal(expected, actual)
19391939

19401940
def test_reindex_frame_add_nat(self):

pandas/tests/indexes/datetimes/test_datetime.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ def test_get_loc(self):
4545
idx.get_loc('2000-01-01T12', method='nearest', tolerance='foo')
4646
with pytest.raises(KeyError):
4747
idx.get_loc('2000-01-01T03', method='nearest', tolerance='2 hours')
48-
with pytest.raises(ValueError,
48+
with pytest.raises(
49+
ValueError,
4950
match='tolerance size must match target index size'):
5051
idx.get_loc('2000-01-01', method='nearest',
51-
tolerance=[pd.Timedelta('1day').to_timedelta64(),
52-
pd.Timedelta('1day').to_timedelta64()])
52+
tolerance=[pd.Timedelta('1day').to_timedelta64(),
53+
pd.Timedelta('1day').to_timedelta64()])
5354

5455
assert idx.get_loc('2000', method='nearest') == slice(0, 3)
5556
assert idx.get_loc('2000-01', method='nearest') == slice(0, 3)
@@ -113,14 +114,16 @@ def test_get_indexer(self):
113114
idx.get_indexer(target, 'nearest',
114115
tolerance=[np.timedelta64(x) for x in tol_raw]),
115116
np.array([0, -1, 1], dtype=np.intp))
116-
with pytest.raises(TypeError, match=('must contain objects '
117-
'convertible to np.timedelta64')):
118-
idx.get_indexer(target, 'nearest', tolerance=[1,2,3])
117+
with pytest.raises(
118+
TypeError,
119+
match='must contain objects convertible to np.timedelta64'):
120+
idx.get_indexer(target, 'nearest', tolerance=[1, 2, 3])
119121
tol_bad = [pd.Timedelta('2 hour').to_timedelta64(),
120122
pd.Timedelta('1 hour').to_timedelta64(),
121123
np.timedelta64(1, 'M'), ]
122-
with pytest.raises(TypeError, match=('All values.*'
123-
'convertible to \\[ns\\]')), pytest.warns(UserWarning):
124+
with (pytest.raises(
125+
TypeError, match='All values.* convertible to \\[ns\\]'),
126+
pytest.warns(UserWarning)):
124127
idx.get_indexer(target, 'nearest', tolerance=tol_bad)
125128
with pytest.raises(ValueError):
126129
idx.get_indexer(idx[[0]], method='nearest', tolerance='foo')

pandas/tests/indexes/period/test_period.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,12 @@ def test_get_loc(self):
8989
idx.get_loc('2000-01-10', method='nearest', tolerance='1 hour')
9090
with pytest.raises(KeyError):
9191
idx.get_loc('2000-01-10', method='nearest', tolerance='1 day')
92-
with pytest.raises(ValueError, match=('ndarray tolerance size must '
93-
'match target index size')):
92+
with pytest.raises(
93+
ValueError,
94+
match='ndarray tolerance size must match target index size'):
9495
idx.get_loc('2000-01-10', method='nearest',
95-
tolerance=[pd.Timedelta('1 day').to_timedelta64(),
96-
pd.Timedelta('1 day').to_timedelta64()])
96+
tolerance=[pd.Timedelta('1 day').to_timedelta64(),
97+
pd.Timedelta('1 day').to_timedelta64()])
9798

9899
def test_where(self):
99100
i = self.create_index()
@@ -179,12 +180,15 @@ def test_get_indexer(self):
179180
tol_bad = [pd.Timedelta('2 hour').to_timedelta64(),
180181
pd.Timedelta('1 hour').to_timedelta64(),
181182
np.timedelta64(1, 'M'), ]
182-
with pytest.raises(TypeError, match=('All values.*'
183-
'convertible to \\[ns\\]')), pytest.warns(UserWarning):
183+
with (pytest.raises(
184+
TypeError,
185+
match='All values.* convertible to \\[ns\\]'),
186+
pytest.warns(UserWarning)):
184187
idx.get_indexer(target, 'nearest', tolerance=tol_bad)
185-
with pytest.raises(TypeError, match=('must contain objects '
186-
'convertible to np.timedelta64')):
187-
idx.get_indexer(target, 'nearest', tolerance=[1,2,3])
188+
with pytest.raises(
189+
TypeError,
190+
match='must contain objects convertible to np.timedelta64'):
191+
idx.get_indexer(target, 'nearest', tolerance=[1, 2, 3])
188192

189193
def test_repeat(self):
190194
# GH10183

pandas/tests/indexes/test_base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ def test_get_indexer_invalid(self):
10671067

10681068
with pytest.raises(ValueError, match='tolerance size must match'):
10691069
idx.get_indexer([1, 0], method='nearest',
1070-
tolerance=[1, 2, 3])
1070+
tolerance=[1, 2, 3])
10711071

10721072
def test_get_indexer_nearest(self):
10731073
idx = Index(np.arange(10))
@@ -1083,7 +1083,7 @@ def test_get_indexer_nearest(self):
10831083
dtype=np.intp))
10841084

10851085
actual = idx.get_indexer([0, 5, 9], method=method,
1086-
tolerance=[0, 0, 0])
1086+
tolerance=[0, 0, 0])
10871087
tm.assert_numpy_array_equal(actual, np.array([0, 5, 9],
10881088
dtype=np.intp))
10891089

@@ -1177,7 +1177,7 @@ def test_get_indexer_strings(self):
11771177

11781178
with pytest.raises(TypeError):
11791179
idx.get_indexer(['a', 'b', 'c', 'd'], method='pad',
1180-
tolerance=[2,2,2,2])
1180+
tolerance=[2, 2, 2, 2])
11811181

11821182
def test_get_loc(self):
11831183
idx = pd.Index([0, 1, 2])
@@ -1204,7 +1204,7 @@ def test_get_loc(self):
12041204
with tm.assert_raises_regex(ValueError, 'tolerance .* valid if'):
12051205
idx.get_loc(1.1, tolerance=1)
12061206
with pytest.raises(ValueError, match='tolerance size must match'):
1207-
idx.get_loc(1.1, 'nearest', tolerance=[1,1])
1207+
idx.get_loc(1.1, 'nearest', tolerance=[1, 1])
12081208

12091209
idx = pd.Index(['a', 'c'])
12101210
with pytest.raises(TypeError):

pandas/tests/indexes/test_numeric.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@ def test_get_loc(self):
355355
with pytest.raises(ValueError, match='must contain numeric elements'):
356356
idx.get_loc(1.4, method='nearest', tolerance=np.array(['foo']))
357357

358-
with pytest.raises(ValueError, match='tolerance size must match target index size'):
358+
with pytest.raises(
359+
ValueError,
360+
match='tolerance size must match target index size'):
359361
idx.get_loc(1.4, method='nearest', tolerance=np.array([1, 2]))
360362

361363
def test_get_loc_na(self):

pandas/tests/indexes/timedeltas/test_timedelta.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@ def test_get_loc(self):
6363
with tm.assert_raises_regex(ValueError, 'must be convertible'):
6464
idx.get_loc(idx[1], method='nearest', tolerance='foo')
6565

66-
with pytest.raises(ValueError, match=('tolerance size must match '
67-
'target index size')):
66+
with pytest.raises(
67+
ValueError,
68+
match='tolerance size must match target index size'):
6869
idx.get_loc(idx[1], method='nearest',
69-
tolerance=[Timedelta(0).to_timedelta64(),
70-
Timedelta(0).to_timedelta64()])
70+
tolerance=[Timedelta(0).to_timedelta64(),
71+
Timedelta(0).to_timedelta64()])
7172

7273
for method, loc in [('pad', 1), ('backfill', 2), ('nearest', 1)]:
7374
assert idx.get_loc('1 day 1 hour', method) == loc
@@ -105,9 +106,9 @@ def test_get_indexer(self):
105106

106107
with pytest.warns(UserWarning):
107108
res = idx.get_indexer(target, 'nearest',
108-
tolerance=[pd.Timedelta('2 hour'),
109-
pd.Timedelta('5 hours'),
110-
pd.Timedelta('1 hour')])
109+
tolerance=[pd.Timedelta('2 hour'),
110+
pd.Timedelta('5 hours'),
111+
pd.Timedelta('1 hour')])
111112
tm.assert_numpy_array_equal(res, np.array([0, -1, 1], dtype=np.intp))
112113

113114
def test_numeric_compat(self):

pandas/tests/series/test_indexing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,15 +2072,15 @@ def test_reindex_nearest(self):
20722072
actual = s.reindex_like(actual, method='nearest', tolerance=1)
20732073
assert_series_equal(expected, actual)
20742074
actual = s.reindex_like(actual, method='nearest',
2075-
tolerance=[1, 2, 3, 4])
2075+
tolerance=[1, 2, 3, 4])
20762076
assert_series_equal(expected, actual)
20772077

20782078
actual = s.reindex(target, method='nearest', tolerance=0.2)
20792079
expected = Series([0, 1, np.nan, 2], target)
20802080
assert_series_equal(expected, actual)
20812081

20822082
actual = s.reindex(target, method='nearest',
2083-
tolerance=[0.3, 0.01, 0.4, 3])
2083+
tolerance=[0.3, 0.01, 0.4, 3])
20842084
expected = Series([0, np.nan, np.nan, 2], target)
20852085
assert_series_equal(expected, actual)
20862086

pandas/tests/sparse/test_indexing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ def test_reindex_nearest(self):
415415
tm.assert_sp_series_equal(expected, actual)
416416

417417
actual = s.reindex(target, method='nearest',
418-
tolerance=[0.3, 0.01, 0.4, 3])
418+
tolerance=[0.3, 0.01, 0.4, 3])
419419
expected = pd.Series([0, np.nan, np.nan, 2], target).to_sparse()
420420
tm.assert_sp_series_equal(expected, actual)
421421

0 commit comments

Comments
 (0)