Skip to content

TST: clean up some tests issues & style #18232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def trans(x): # noqa
try:
if np.allclose(new_result, result, rtol=0):
return new_result
except:
except Exception:

# comparison of an object dtype with a number type could
# hit here
Expand All @@ -151,14 +151,14 @@ def trans(x): # noqa
elif dtype.kind in ['M', 'm'] and result.dtype.kind in ['i', 'f']:
try:
result = result.astype(dtype)
except:
except Exception:
if dtype.tz:
# convert to datetime and change timezone
from pandas import to_datetime
result = to_datetime(result).tz_localize('utc')
result = result.tz_convert(dtype.tz)

except:
except Exception:
pass

return result
Expand Down Expand Up @@ -210,7 +210,7 @@ def changeit():
new_result[mask] = om_at
result[:] = new_result
return result, False
except:
except Exception:
pass

# we are forced to change the dtype of the result as the input
Expand Down Expand Up @@ -243,7 +243,7 @@ def changeit():

try:
np.place(result, mask, other)
except:
except Exception:
return changeit()

return result, False
Expand Down Expand Up @@ -274,14 +274,14 @@ def maybe_promote(dtype, fill_value=np.nan):
if issubclass(dtype.type, np.datetime64):
try:
fill_value = tslib.Timestamp(fill_value).value
except:
except Exception:
# the proper thing to do here would probably be to upcast
# to object (but numpy 1.6.1 doesn't do this properly)
fill_value = iNaT
elif issubclass(dtype.type, np.timedelta64):
try:
fill_value = lib.Timedelta(fill_value).value
except:
except Exception:
# as for datetimes, cannot upcast to object
fill_value = iNaT
else:
Expand Down Expand Up @@ -592,12 +592,12 @@ def maybe_convert_scalar(values):

def coerce_indexer_dtype(indexer, categories):
""" coerce the indexer input array to the smallest dtype possible """
l = len(categories)
if l < _int8_max:
length = len(categories)
if length < _int8_max:
return _ensure_int8(indexer)
elif l < _int16_max:
elif length < _int16_max:
return _ensure_int16(indexer)
elif l < _int32_max:
elif length < _int32_max:
return _ensure_int32(indexer)
return _ensure_int64(indexer)

Expand Down Expand Up @@ -629,7 +629,7 @@ def conv(r, dtype):
r = float(r)
elif dtype.kind == 'i':
r = int(r)
except:
except Exception:
pass

return r
Expand Down Expand Up @@ -756,7 +756,7 @@ def maybe_convert_objects(values, convert_dates=True, convert_numeric=True,
if not isna(new_values).all():
values = new_values

except:
except Exception:
pass
else:
# soft-conversion
Expand Down Expand Up @@ -817,7 +817,7 @@ def soft_convert_objects(values, datetime=True, numeric=True, timedelta=True,
# If all NaNs, then do not-alter
values = converted if not isna(converted).all() else values
values = values.copy() if copy else values
except:
except Exception:
pass

return values
Expand Down Expand Up @@ -888,10 +888,10 @@ def try_datetime(v):
try:
from pandas import to_datetime
return to_datetime(v)
except:
except Exception:
pass

except:
except Exception:
pass

return v.reshape(shape)
Expand All @@ -903,7 +903,7 @@ def try_timedelta(v):
from pandas import to_timedelta
try:
return to_timedelta(v)._values.reshape(shape)
except:
except Exception:
return v.reshape(shape)

inferred_type = lib.infer_datetimelike_array(_ensure_object(v))
Expand Down
10 changes: 5 additions & 5 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2032,7 +2032,7 @@ def equals(self, other):
try:
return array_equivalent(_values_from_object(self),
_values_from_object(other))
except:
except Exception:
return False

def identical(self, other):
Expand Down Expand Up @@ -2315,7 +2315,7 @@ def intersection(self, other):
try:
indexer = Index(other._values).get_indexer(self._values)
indexer = indexer.take((indexer != -1).nonzero()[0])
except:
except Exception:
# duplicates
indexer = algos.unique1d(
Index(other._values).get_indexer_non_unique(self._values)[0])
Expand Down Expand Up @@ -3022,13 +3022,13 @@ def _reindex_non_unique(self, target):
new_indexer = None

if len(missing):
l = np.arange(len(indexer))
length = np.arange(len(indexer))

missing = _ensure_platform_int(missing)
missing_labels = target.take(missing)
missing_indexer = _ensure_int64(l[~check])
missing_indexer = _ensure_int64(length[~check])
cur_labels = self.take(indexer[check]).values
cur_indexer = _ensure_int64(l[check])
cur_indexer = _ensure_int64(length[check])

new_labels = np.empty(tuple([len(indexer)]), dtype=object)
new_labels[cur_indexer] = cur_labels
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def _generate(cls, start, end, periods, name, offset,

try:
inferred_tz = timezones.infer_tzinfo(start, end)
except:
except Exception:
raise TypeError('Start and end cannot both be tz-aware with '
'different timezones')

Expand Down Expand Up @@ -1176,12 +1176,12 @@ def __iter__(self):

# convert in chunks of 10k for efficiency
data = self.asi8
l = len(self)
length = len(self)
chunksize = 10000
chunks = int(l / chunksize) + 1
chunks = int(length / chunksize) + 1
for i in range(chunks):
start_i = i * chunksize
end_i = min((i + 1) * chunksize, l)
end_i = min((i + 1) * chunksize, length)
converted = libts.ints_to_pydatetime(data[start_i:end_i],
tz=self.tz, freq=self.freq,
box=True)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ def insert(self, loc, item):
if _is_convertible_to_td(item):
try:
item = Timedelta(item)
except:
except Exception:
pass

freq = None
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ def _ixs(self, i, axis=0):
return values[i]
except IndexError:
raise
except:
except Exception:
if isinstance(i, slice):
indexer = self.index._convert_slice_indexer(i, kind='iloc')
return self._get_values(indexer)
Expand Down Expand Up @@ -675,7 +675,7 @@ def _get_with(self, key):
if isinstance(key, tuple):
try:
return self._get_values_tuple(key)
except:
except Exception:
if len(key) == 1:
key = key[0]
if isinstance(key, slice):
Expand Down Expand Up @@ -818,7 +818,7 @@ def _set_with(self, key, value):
if not isinstance(key, (list, Series, np.ndarray, Series)):
try:
key = list(key)
except:
except Exception:
key = [key]

if isinstance(key, Index):
Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/io/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,7 @@ def test_importcheck_thread_safety():
def test_parse_failure_unseekable():
# Issue #17975
_skip_if_no('lxml')
_skip_if_no('bs4')

class UnseekableStringIO(StringIO):
def seekable(self):
Expand All @@ -996,6 +997,7 @@ def seekable(self):
def test_parse_failure_rewinds():
# Issue #17975
_skip_if_no('lxml')
_skip_if_no('bs4')

class MockFile(object):
def __init__(self, data):
Expand Down
11 changes: 8 additions & 3 deletions pandas/tests/scalar/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,13 +1101,18 @@ def test_timestamp(self):

tsc = Timestamp('2014-10-11 11:00:01.12345678', tz='US/Central')
utsc = tsc.tz_convert('UTC')

# utsc is a different representation of the same time
assert tsc.timestamp() == utsc.timestamp()

if PY3:
# should agree with datetime.timestamp method
dt = ts.to_pydatetime()
assert dt.timestamp() == ts.timestamp()

# datetime.timestamp() converts in the local timezone
with tm.set_timezone('UTC'):

# should agree with datetime.timestamp method
dt = ts.to_pydatetime()
assert dt.timestamp() == ts.timestamp()


class TestTimestampNsOperations(object):
Expand Down
16 changes: 11 additions & 5 deletions pandas/tests/tseries/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import pandas.util.testing as tm
import pandas.tseries.offsets as offsets
from pandas.compat import lrange, zip
from pandas.compat import lrange, zip, PY3
from pandas.core.indexes.datetimes import bdate_range, date_range
from pandas.core.dtypes.dtypes import DatetimeTZDtype
from pandas._libs import tslib
Expand Down Expand Up @@ -1278,16 +1278,22 @@ def test_replace_tzinfo(self):
result_dt = dt.replace(tzinfo=tzinfo)
result_pd = Timestamp(dt).replace(tzinfo=tzinfo)

if hasattr(result_dt, 'timestamp'): # New method in Py 3.3
assert result_dt.timestamp() == result_pd.timestamp()
if PY3:
# datetime.timestamp() converts in the local timezone
with tm.set_timezone('UTC'):
assert result_dt.timestamp() == result_pd.timestamp()

assert result_dt == result_pd
assert result_dt == result_pd.to_pydatetime()

result_dt = dt.replace(tzinfo=tzinfo).replace(tzinfo=None)
result_pd = Timestamp(dt).replace(tzinfo=tzinfo).replace(tzinfo=None)

if hasattr(result_dt, 'timestamp'): # New method in Py 3.3
assert result_dt.timestamp() == result_pd.timestamp()
if PY3:
# datetime.timestamp() converts in the local timezone
with tm.set_timezone('UTC'):
assert result_dt.timestamp() == result_pd.timestamp()

assert result_dt == result_pd
assert result_dt == result_pd.to_pydatetime()

Expand Down