Skip to content

Commit 0e27d55

Browse files
committed
TST: parameterize indexes base test to introspect ufuncs fails on numpy_dev
1 parent c79b7bb commit 0e27d55

File tree

2 files changed

+71
-53
lines changed

2 files changed

+71
-53
lines changed

pandas/tests/indexes/common.py

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
import pandas as pd
1111
from pandas import (
12-
CategoricalIndex, DatetimeIndex, Float64Index, Index, Int64Index,
13-
IntervalIndex, MultiIndex, PeriodIndex, RangeIndex, Series, TimedeltaIndex,
14-
UInt64Index, isna)
12+
CategoricalIndex, DatetimeIndex, Index, Int64Index, IntervalIndex,
13+
MultiIndex, PeriodIndex, RangeIndex, Series, TimedeltaIndex, UInt64Index,
14+
isna)
1515
from pandas.core.indexes.base import InvalidIndexError
1616
from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin
1717
import pandas.util.testing as tm
@@ -677,58 +677,9 @@ def test_equals_op(self):
677677
tm.assert_numpy_array_equal(index_a == item, expected3)
678678
tm.assert_series_equal(series_a == item, Series(expected3))
679679

680-
def test_numpy_ufuncs(self):
681-
# test ufuncs of numpy, see:
682-
# http://docs.scipy.org/doc/numpy/reference/ufuncs.html
683-
684-
for name, idx in self.indices.items():
685-
for func in [np.exp, np.exp2, np.expm1, np.log, np.log2, np.log10,
686-
np.log1p, np.sqrt, np.sin, np.cos, np.tan, np.arcsin,
687-
np.arccos, np.arctan, np.sinh, np.cosh, np.tanh,
688-
np.arcsinh, np.arccosh, np.arctanh, np.deg2rad,
689-
np.rad2deg]:
690-
if isinstance(idx, DatetimeIndexOpsMixin):
691-
# raise TypeError or ValueError (PeriodIndex)
692-
# PeriodIndex behavior should be changed in future version
693-
with pytest.raises(Exception):
694-
with np.errstate(all='ignore'):
695-
func(idx)
696-
elif isinstance(idx, (Float64Index, Int64Index, UInt64Index)):
697-
# coerces to float (e.g. np.sin)
698-
with np.errstate(all='ignore'):
699-
result = func(idx)
700-
exp = Index(func(idx.values), name=idx.name)
701-
702-
tm.assert_index_equal(result, exp)
703-
assert isinstance(result, pd.Float64Index)
704-
else:
705-
# raise AttributeError or TypeError
706-
if len(idx) == 0:
707-
continue
708-
else:
709-
with pytest.raises(Exception):
710-
with np.errstate(all='ignore'):
711-
func(idx)
712-
713-
for func in [np.isfinite, np.isinf, np.isnan, np.signbit]:
714-
if isinstance(idx, DatetimeIndexOpsMixin):
715-
# raise TypeError or ValueError (PeriodIndex)
716-
with pytest.raises(Exception):
717-
func(idx)
718-
elif isinstance(idx, (Float64Index, Int64Index, UInt64Index)):
719-
# Results in bool array
720-
result = func(idx)
721-
assert isinstance(result, np.ndarray)
722-
assert not isinstance(result, Index)
723-
else:
724-
if len(idx) == 0:
725-
continue
726-
else:
727-
with pytest.raises(Exception):
728-
func(idx)
729-
730680
def test_hasnans_isnans(self):
731681
# GH 11343, added tests for hasnans / isnans
682+
732683
for name, index in self.indices.items():
733684
if isinstance(index, MultiIndex):
734685
pass
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import Float64Index, Index, Int64Index, UInt64Index
5+
from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin
6+
from pandas.util import testing as tm
7+
8+
9+
@pytest.mark.parametrize(
10+
'func', [np.exp, np.exp2, np.expm1, np.log, np.log2, np.log10,
11+
np.log1p, np.sqrt, np.sin, np.cos, np.tan, np.arcsin,
12+
np.arccos, np.arctan, np.sinh, np.cosh, np.tanh,
13+
np.arcsinh, np.arccosh, np.arctanh, np.deg2rad,
14+
np.rad2deg],
15+
ids=lambda x: x.__name__)
16+
def test_numpy_ufuncs_basic(indices, func):
17+
# test ufuncs of numpy, see:
18+
# http://docs.scipy.org/doc/numpy/reference/ufuncs.html
19+
20+
idx = indices
21+
if isinstance(idx, DatetimeIndexOpsMixin):
22+
# raise TypeError or ValueError (PeriodIndex)
23+
# PeriodIndex behavior should be changed in future version
24+
with pytest.raises(Exception):
25+
with np.errstate(all='ignore'):
26+
func(idx)
27+
elif isinstance(idx, (Float64Index, Int64Index, UInt64Index)):
28+
# coerces to float (e.g. np.sin)
29+
with np.errstate(all='ignore'):
30+
result = func(idx)
31+
exp = Index(func(idx.values), name=idx.name)
32+
33+
tm.assert_index_equal(result, exp)
34+
assert isinstance(result, Float64Index)
35+
else:
36+
# raise AttributeError or TypeError
37+
if len(idx) == 0:
38+
pass
39+
else:
40+
with pytest.raises(Exception):
41+
with np.errstate(all='ignore'):
42+
func(idx)
43+
44+
45+
@pytest.mark.parametrize(
46+
'func', [np.isfinite, np.isinf, np.isnan, np.signbit],
47+
ids=lambda x: x.__name__)
48+
def test_numpy_ufuncs_other(indices, func):
49+
# test ufuncs of numpy, see:
50+
# http://docs.scipy.org/doc/numpy/reference/ufuncs.html
51+
52+
idx = indices
53+
if isinstance(idx, DatetimeIndexOpsMixin):
54+
# raise TypeError or ValueError (PeriodIndex)
55+
with pytest.raises(Exception):
56+
func(idx)
57+
elif isinstance(idx, (Float64Index, Int64Index, UInt64Index)):
58+
# Results in bool array
59+
result = func(idx)
60+
assert isinstance(result, np.ndarray)
61+
assert not isinstance(result, Index)
62+
else:
63+
if len(idx) == 0:
64+
pass
65+
else:
66+
with pytest.raises(Exception):
67+
func(idx)

0 commit comments

Comments
 (0)