Skip to content

Commit ae5a043

Browse files
authored
TST: parameterize indexes base test to introspect ufuncs fails on numpy_dev (#26161)
1 parent c79b7bb commit ae5a043

File tree

2 files changed

+86
-53
lines changed

2 files changed

+86
-53
lines changed

pandas/tests/indexes/common.py

+4-53
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
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import (
5+
DatetimeIndex, Float64Index, Index, Int64Index, TimedeltaIndex,
6+
UInt64Index, _np_version_under1p17)
7+
from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin
8+
from pandas.util import testing as tm
9+
10+
11+
@pytest.mark.parametrize(
12+
'func', [np.exp, np.exp2, np.expm1, np.log, np.log2, np.log10,
13+
np.log1p, np.sqrt, np.sin, np.cos, np.tan, np.arcsin,
14+
np.arccos, np.arctan, np.sinh, np.cosh, np.tanh,
15+
np.arcsinh, np.arccosh, np.arctanh, np.deg2rad,
16+
np.rad2deg],
17+
ids=lambda x: x.__name__)
18+
def test_numpy_ufuncs_basic(indices, func):
19+
# test ufuncs of numpy, see:
20+
# http://docs.scipy.org/doc/numpy/reference/ufuncs.html
21+
22+
idx = indices
23+
if isinstance(idx, DatetimeIndexOpsMixin):
24+
# raise TypeError or ValueError (PeriodIndex)
25+
with pytest.raises(Exception):
26+
with np.errstate(all='ignore'):
27+
func(idx)
28+
elif isinstance(idx, (Float64Index, Int64Index, UInt64Index)):
29+
# coerces to float (e.g. np.sin)
30+
with np.errstate(all='ignore'):
31+
result = func(idx)
32+
exp = Index(func(idx.values), name=idx.name)
33+
34+
tm.assert_index_equal(result, exp)
35+
assert isinstance(result, Float64Index)
36+
else:
37+
# raise AttributeError or TypeError
38+
if len(idx) == 0:
39+
pass
40+
else:
41+
with pytest.raises(Exception):
42+
with np.errstate(all='ignore'):
43+
func(idx)
44+
45+
46+
@pytest.mark.parametrize(
47+
'func', [np.isfinite, np.isinf, np.isnan, np.signbit],
48+
ids=lambda x: x.__name__)
49+
def test_numpy_ufuncs_other(indices, func):
50+
# test ufuncs of numpy, see:
51+
# http://docs.scipy.org/doc/numpy/reference/ufuncs.html
52+
53+
idx = indices
54+
if isinstance(idx, (DatetimeIndex, TimedeltaIndex)):
55+
56+
# ok under numpy >= 1.17
57+
if not _np_version_under1p17 and func in [np.isfinite]:
58+
# Results in bool array
59+
result = func(idx)
60+
assert isinstance(result, np.ndarray)
61+
assert not isinstance(result, Index)
62+
else:
63+
# raise TypeError or ValueError (PeriodIndex)
64+
with pytest.raises(Exception):
65+
func(idx)
66+
67+
elif isinstance(idx, DatetimeIndexOpsMixin):
68+
# raise TypeError or ValueError (PeriodIndex)
69+
with pytest.raises(Exception):
70+
func(idx)
71+
72+
elif isinstance(idx, (Float64Index, Int64Index, UInt64Index)):
73+
# Results in bool array
74+
result = func(idx)
75+
assert isinstance(result, np.ndarray)
76+
assert not isinstance(result, Index)
77+
else:
78+
if len(idx) == 0:
79+
pass
80+
else:
81+
with pytest.raises(Exception):
82+
func(idx)

0 commit comments

Comments
 (0)