Skip to content

Commit

Permalink
MAINT: Remove tm.assertNotIsInstance (#16061)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyoung authored and jreback committed Apr 20, 2017
1 parent 97215b3 commit b9cb3fe
Show file tree
Hide file tree
Showing 16 changed files with 151 additions and 153 deletions.
22 changes: 12 additions & 10 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-

import pytest

from pandas import compat
from pandas.compat import PY3

Expand Down Expand Up @@ -793,41 +795,41 @@ def test_numpy_ufuncs(self):
if isinstance(idx, DatetimeIndexOpsMixin):
# raise TypeError or ValueError (PeriodIndex)
# PeriodIndex behavior should be changed in future version
with tm.assertRaises(Exception):
with pytest.raises(Exception):
with np.errstate(all='ignore'):
func(idx)
elif isinstance(idx, (Float64Index, Int64Index, UInt64Index)):
# coerces to float (e.g. np.sin)
with np.errstate(all='ignore'):
result = func(idx)
exp = Index(func(idx.values), name=idx.name)
self.assert_index_equal(result, exp)
self.assertIsInstance(result, pd.Float64Index)

tm.assert_index_equal(result, exp)
assert isinstance(result, pd.Float64Index)
else:
# raise AttributeError or TypeError
if len(idx) == 0:
continue
else:
with tm.assertRaises(Exception):
with pytest.raises(Exception):
with np.errstate(all='ignore'):
func(idx)

for func in [np.isfinite, np.isinf, np.isnan, np.signbit]:
if isinstance(idx, DatetimeIndexOpsMixin):
# raise TypeError or ValueError (PeriodIndex)
with tm.assertRaises(Exception):
with pytest.raises(Exception):
func(idx)
elif isinstance(idx, (Float64Index, Int64Index, UInt64Index)):
# results in bool array
# Results in bool array
result = func(idx)
exp = func(idx.values)
self.assertIsInstance(result, np.ndarray)
tm.assertNotIsInstance(result, Index)
assert isinstance(result, np.ndarray)
assert not isinstance(result, Index)
else:
if len(idx) == 0:
continue
else:
with tm.assertRaises(Exception):
with pytest.raises(Exception):
func(idx)

def test_hasnans_isnans(self):
Expand Down
46 changes: 23 additions & 23 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import pytest

import numpy as np
import pandas as pd
import pandas.util.testing as tm
import pandas.compat as compat
Expand Down Expand Up @@ -54,9 +55,9 @@ def test_insert(self):
expected = Index([datetime(2000, 1, 4), 'inserted',
datetime(2000, 1, 1),
datetime(2000, 1, 2)], name='idx')
self.assertNotIsInstance(result, DatetimeIndex)
assert not isinstance(result, DatetimeIndex)
tm.assert_index_equal(result, expected)
self.assertEqual(result.name, expected.name)
assert result.name == expected.name

idx = date_range('1/1/2000', periods=3, freq='M', name='idx')

Expand Down Expand Up @@ -85,33 +86,32 @@ def test_insert(self):
for n, d, expected in cases:
result = idx.insert(n, d)
tm.assert_index_equal(result, expected)
self.assertEqual(result.name, expected.name)
self.assertEqual(result.freq, expected.freq)
assert result.name == expected.name
assert result.freq == expected.freq

# reset freq to None
result = idx.insert(3, datetime(2000, 1, 2))
expected = DatetimeIndex(['2000-01-31', '2000-02-29', '2000-03-31',
'2000-01-02'], name='idx', freq=None)
tm.assert_index_equal(result, expected)
self.assertEqual(result.name, expected.name)
self.assertTrue(result.freq is None)
assert result.name == expected.name
assert result.freq is None

# GH 7299
tm._skip_if_no_pytz()
import pytz

idx = date_range('1/1/2000', periods=3, freq='D', tz='Asia/Tokyo',
name='idx')
with tm.assertRaises(ValueError):
result = idx.insert(3, pd.Timestamp('2000-01-04'))
with tm.assertRaises(ValueError):
result = idx.insert(3, datetime(2000, 1, 4))
with tm.assertRaises(ValueError):
result = idx.insert(3, pd.Timestamp('2000-01-04', tz='US/Eastern'))
with tm.assertRaises(ValueError):
result = idx.insert(3,
datetime(2000, 1, 4,
tzinfo=pytz.timezone('US/Eastern')))
with pytest.raises(ValueError):
idx.insert(3, pd.Timestamp('2000-01-04'))
with pytest.raises(ValueError):
idx.insert(3, datetime(2000, 1, 4))
with pytest.raises(ValueError):
idx.insert(3, pd.Timestamp('2000-01-04', tz='US/Eastern'))
with pytest.raises(ValueError):
idx.insert(3, datetime(2000, 1, 4,
tzinfo=pytz.timezone('US/Eastern')))

for tz in ['US/Pacific', 'Asia/Singapore']:
idx = date_range('1/1/2000 09:00', periods=6, freq='H', tz=tz,
Expand All @@ -124,9 +124,9 @@ def test_insert(self):

result = idx.insert(6, d)
tm.assert_index_equal(result, expected)
self.assertEqual(result.name, expected.name)
self.assertEqual(result.freq, expected.freq)
self.assertEqual(result.tz, expected.tz)
assert result.name == expected.name
assert result.freq == expected.freq
assert result.tz == expected.tz

expected = DatetimeIndex(['2000-01-01 09:00', '2000-01-01 10:00',
'2000-01-01 11:00',
Expand All @@ -139,9 +139,9 @@ def test_insert(self):
pytz.timezone(tz).localize(datetime(2000, 1, 1, 10))]:
result = idx.insert(6, d)
tm.assert_index_equal(result, expected)
self.assertEqual(result.name, expected.name)
self.assertTrue(result.freq is None)
self.assertEqual(result.tz, expected.tz)
assert result.name == expected.name
assert result.tz == expected.tz
assert result.freq is None

def test_delete(self):
idx = date_range(start='2000-01-01', periods=5, freq='M', name='idx')
Expand Down
30 changes: 15 additions & 15 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,27 +283,27 @@ def test_constructor_dtypes(self):
for idx in [Index(np.array([1, 2, 3], dtype=int)),
Index(np.array([1, 2, 3], dtype=int), dtype=int),
Index([1, 2, 3], dtype=int)]:
self.assertIsInstance(idx, Int64Index)
assert isinstance(idx, Int64Index)

# these should coerce
# These should coerce
for idx in [Index(np.array([1., 2., 3.], dtype=float), dtype=int),
Index([1., 2., 3.], dtype=int)]:
self.assertIsInstance(idx, Int64Index)
assert isinstance(idx, Int64Index)

for idx in [Index(np.array([1., 2., 3.], dtype=float)),
Index(np.array([1, 2, 3], dtype=int), dtype=float),
Index(np.array([1., 2., 3.], dtype=float), dtype=float),
Index([1, 2, 3], dtype=float),
Index([1., 2., 3.], dtype=float)]:
self.assertIsInstance(idx, Float64Index)
assert isinstance(idx, Float64Index)

for idx in [Index(np.array([True, False, True], dtype=bool)),
Index([True, False, True]),
Index(np.array([True, False, True], dtype=bool),
dtype=bool),
Index([True, False, True], dtype=bool)]:
self.assertIsInstance(idx, Index)
self.assertEqual(idx.dtype, object)
assert isinstance(idx, Index)
assert idx.dtype == object

for idx in [Index(np.array([1, 2, 3], dtype=int), dtype='category'),
Index([1, 2, 3], dtype='category'),
Expand All @@ -312,32 +312,32 @@ def test_constructor_dtypes(self):
dtype='category'),
Index([datetime(2011, 1, 1), datetime(2011, 1, 2)],
dtype='category')]:
self.assertIsInstance(idx, CategoricalIndex)
assert isinstance(idx, CategoricalIndex)

for idx in [Index(np.array([np_datetime64_compat('2011-01-01'),
np_datetime64_compat('2011-01-02')])),
Index([datetime(2011, 1, 1), datetime(2011, 1, 2)])]:
self.assertIsInstance(idx, DatetimeIndex)
assert isinstance(idx, DatetimeIndex)

for idx in [Index(np.array([np_datetime64_compat('2011-01-01'),
np_datetime64_compat('2011-01-02')]),
dtype=object),
Index([datetime(2011, 1, 1),
datetime(2011, 1, 2)], dtype=object)]:
self.assertNotIsInstance(idx, DatetimeIndex)
self.assertIsInstance(idx, Index)
self.assertEqual(idx.dtype, object)
assert not isinstance(idx, DatetimeIndex)
assert isinstance(idx, Index)
assert idx.dtype == object

for idx in [Index(np.array([np.timedelta64(1, 'D'), np.timedelta64(
1, 'D')])), Index([timedelta(1), timedelta(1)])]:
self.assertIsInstance(idx, TimedeltaIndex)
assert isinstance(idx, TimedeltaIndex)

for idx in [Index(np.array([np.timedelta64(1, 'D'),
np.timedelta64(1, 'D')]), dtype=object),
Index([timedelta(1), timedelta(1)], dtype=object)]:
self.assertNotIsInstance(idx, TimedeltaIndex)
self.assertIsInstance(idx, Index)
self.assertEqual(idx.dtype, object)
assert not isinstance(idx, TimedeltaIndex)
assert isinstance(idx, Index)
assert idx.dtype == object

def test_constructor_dtypes_datetime(self):

Expand Down
70 changes: 38 additions & 32 deletions pandas/tests/indexes/test_category.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-

import pytest

import pandas.util.testing as tm
from pandas.core.indexes.api import Index, CategoricalIndex
from .common import Base
Expand Down Expand Up @@ -37,62 +39,66 @@ def test_construction(self):

result = Index(ci)
tm.assert_index_equal(result, ci, exact=True)
self.assertFalse(result.ordered)
assert not result.ordered

result = Index(ci.values)
tm.assert_index_equal(result, ci, exact=True)
self.assertFalse(result.ordered)
assert not result.ordered

# empty
result = CategoricalIndex(categories=categories)
self.assert_index_equal(result.categories, Index(categories))
tm.assert_index_equal(result.categories, Index(categories))
tm.assert_numpy_array_equal(result.codes, np.array([], dtype='int8'))
self.assertFalse(result.ordered)
assert not result.ordered

# passing categories
result = CategoricalIndex(list('aabbca'), categories=categories)
self.assert_index_equal(result.categories, Index(categories))
tm.assert_index_equal(result.categories, Index(categories))
tm.assert_numpy_array_equal(result.codes,
np.array([0, 0, 1, 1, 2, 0], dtype='int8'))
np.array([0, 0, 1,
1, 2, 0], dtype='int8'))

c = pd.Categorical(list('aabbca'))
result = CategoricalIndex(c)
self.assert_index_equal(result.categories, Index(list('abc')))
tm.assert_index_equal(result.categories, Index(list('abc')))
tm.assert_numpy_array_equal(result.codes,
np.array([0, 0, 1, 1, 2, 0], dtype='int8'))
self.assertFalse(result.ordered)
np.array([0, 0, 1,
1, 2, 0], dtype='int8'))
assert not result.ordered

result = CategoricalIndex(c, categories=categories)
self.assert_index_equal(result.categories, Index(categories))
tm.assert_index_equal(result.categories, Index(categories))
tm.assert_numpy_array_equal(result.codes,
np.array([0, 0, 1, 1, 2, 0], dtype='int8'))
self.assertFalse(result.ordered)
np.array([0, 0, 1,
1, 2, 0], dtype='int8'))
assert not result.ordered

ci = CategoricalIndex(c, categories=list('abcd'))
result = CategoricalIndex(ci)
self.assert_index_equal(result.categories, Index(categories))
tm.assert_index_equal(result.categories, Index(categories))
tm.assert_numpy_array_equal(result.codes,
np.array([0, 0, 1, 1, 2, 0], dtype='int8'))
self.assertFalse(result.ordered)
np.array([0, 0, 1,
1, 2, 0], dtype='int8'))
assert not result.ordered

result = CategoricalIndex(ci, categories=list('ab'))
self.assert_index_equal(result.categories, Index(list('ab')))
tm.assert_index_equal(result.categories, Index(list('ab')))
tm.assert_numpy_array_equal(result.codes,
np.array([0, 0, 1, 1, -1, 0],
dtype='int8'))
self.assertFalse(result.ordered)
np.array([0, 0, 1,
1, -1, 0], dtype='int8'))
assert not result.ordered

result = CategoricalIndex(ci, categories=list('ab'), ordered=True)
self.assert_index_equal(result.categories, Index(list('ab')))
tm.assert_index_equal(result.categories, Index(list('ab')))
tm.assert_numpy_array_equal(result.codes,
np.array([0, 0, 1, 1, -1, 0],
dtype='int8'))
self.assertTrue(result.ordered)
np.array([0, 0, 1,
1, -1, 0], dtype='int8'))
assert result.ordered

# turn me to an Index
result = Index(np.array(ci))
self.assertIsInstance(result, Index)
self.assertNotIsInstance(result, CategoricalIndex)
assert isinstance(result, Index)
assert not isinstance(result, CategoricalIndex)

def test_construction_with_dtype(self):

Expand Down Expand Up @@ -325,9 +331,9 @@ def test_delete(self):
expected = CategoricalIndex(list('aabbc'), categories=categories)
tm.assert_index_equal(result, expected, exact=True)

with tm.assertRaises((IndexError, ValueError)):
# either depeidnig on numpy version
result = ci.delete(10)
with pytest.raises((IndexError, ValueError)):
# Either depending on NumPy version
ci.delete(10)

def test_astype(self):

Expand All @@ -336,12 +342,12 @@ def test_astype(self):
tm.assert_index_equal(result, ci, exact=True)

result = ci.astype(object)
self.assert_index_equal(result, Index(np.array(ci)))
tm.assert_index_equal(result, Index(np.array(ci)))

# this IS equal, but not the same class
self.assertTrue(result.equals(ci))
self.assertIsInstance(result, Index)
self.assertNotIsInstance(result, CategoricalIndex)
assert result.equals(ci)
assert isinstance(result, Index)
assert not isinstance(result, CategoricalIndex)

# interval
ii = IntervalIndex.from_arrays(left=[-0.001, 2.0],
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/indexes/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,17 +539,17 @@ def test_astype(self):
def test_constructor_single_level(self):
single_level = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux']],
labels=[[0, 1, 2, 3]], names=['first'])
tm.assertIsInstance(single_level, Index)
self.assertNotIsInstance(single_level, MultiIndex)
self.assertEqual(single_level.name, 'first')
assert isinstance(single_level, Index)
assert not isinstance(single_level, MultiIndex)
assert single_level.name == 'first'

single_level = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux']],
labels=[[0, 1, 2, 3]])
self.assertIsNone(single_level.name)
assert single_level.name is None

def test_constructor_no_levels(self):
assertRaisesRegexp(ValueError, "non-zero number of levels/labels",
MultiIndex, levels=[], labels=[])
tm.assertRaisesRegexp(ValueError, "non-zero number of levels/labels",
MultiIndex, levels=[], labels=[])
both_re = re.compile('Must pass both levels and labels')
with tm.assertRaisesRegexp(TypeError, both_re):
MultiIndex(levels=[])
Expand Down
Loading

0 comments on commit b9cb3fe

Please sign in to comment.