-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
CLN: Refactor pandas/tests/base - part3 #30147
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
Changes from 12 commits
4d1750c
3d04ff2
89d84b4
199896f
e269b09
baab827
28291f1
589ae3b
2bbc3fd
b3d0252
53db63f
891b24c
8f0fdf6
69a0a0d
0fce4c5
b7892fa
471f217
baa4965
7562479
85b16cb
87e0a5b
3979b3d
452335a
8bf1142
c1e9f28
d9bea94
87247a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,18 +29,11 @@ | |
TimedeltaIndex, | ||
Timestamp, | ||
) | ||
from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin | ||
from pandas.tests.base.utils import allow_na_ops | ||
import pandas.util.testing as tm | ||
|
||
|
||
class Ops: | ||
def _allow_na_ops(self, obj): | ||
"""Whether to skip test cases including NaN""" | ||
if (isinstance(obj, Index) and obj.is_boolean()) or not obj._can_hold_na: | ||
# don't test boolean / integer dtypes | ||
return False | ||
return True | ||
|
||
def setup_method(self, method): | ||
self.bool_index = tm.makeBoolIndex(10, name="a") | ||
self.int_index = tm.makeIntIndex(10, name="a") | ||
|
@@ -83,74 +76,29 @@ def setup_method(self, method): | |
|
||
self.objs = self.indexes + self.series + self.narrow_series | ||
|
||
def check_ops_properties(self, props, filter=None, ignore_failures=False): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method was only used in pandas/tests/indexes. I could remove all tests where it was used though as they were redundant (already tested in |
||
for op in props: | ||
for o in self.is_valid_objs: | ||
SaturnFromTitan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# if a filter, skip if it doesn't match | ||
if filter is not None: | ||
filt = o.index if isinstance(o, Series) else o | ||
if not filter(filt): | ||
continue | ||
|
||
try: | ||
if isinstance(o, Series): | ||
expected = Series(getattr(o.index, op), index=o.index, name="a") | ||
else: | ||
expected = getattr(o, op) | ||
except (AttributeError): | ||
if ignore_failures: | ||
continue | ||
|
||
result = getattr(o, op) | ||
|
||
# these could be series, arrays or scalars | ||
if isinstance(result, Series) and isinstance(expected, Series): | ||
tm.assert_series_equal(result, expected) | ||
elif isinstance(result, Index) and isinstance(expected, Index): | ||
tm.assert_index_equal(result, expected) | ||
elif isinstance(result, np.ndarray) and isinstance( | ||
expected, np.ndarray | ||
): | ||
tm.assert_numpy_array_equal(result, expected) | ||
else: | ||
assert result == expected | ||
|
||
# freq raises AttributeError on an Int64Index because its not | ||
# defined we mostly care about Series here anyhow | ||
if not ignore_failures: | ||
for o in self.not_valid_objs: | ||
|
||
# an object that is datetimelike will raise a TypeError, | ||
# otherwise an AttributeError | ||
err = AttributeError | ||
if issubclass(type(o), DatetimeIndexOpsMixin): | ||
err = TypeError | ||
|
||
with pytest.raises(err): | ||
getattr(o, op) | ||
|
||
@pytest.mark.parametrize("klass", [Series, DataFrame]) | ||
def test_binary_ops_docs(self, klass): | ||
op_map = { | ||
"add": "+", | ||
"sub": "-", | ||
"mul": "*", | ||
"mod": "%", | ||
"pow": "**", | ||
"truediv": "/", | ||
"floordiv": "//", | ||
} | ||
for op_name in op_map: | ||
operand1 = klass.__name__.lower() | ||
operand2 = "other" | ||
op = op_map[op_name] | ||
expected_str = " ".join([operand1, op, operand2]) | ||
assert expected_str in getattr(klass, op_name).__doc__ | ||
|
||
# reverse version of the binary ops | ||
expected_str = " ".join([operand2, op, operand1]) | ||
assert expected_str in getattr(klass, "r" + op_name).__doc__ | ||
|
||
@pytest.mark.parametrize("klass", [Series, DataFrame]) | ||
@pytest.mark.parametrize( | ||
"op_name, op", | ||
[ | ||
("add", "+"), | ||
SaturnFromTitan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
("sub", "-"), | ||
("mul", "*"), | ||
("mod", "%"), | ||
("pow", "**"), | ||
("truediv", "/"), | ||
("floordiv", "//"), | ||
], | ||
) | ||
def test_binary_ops_docs(klass, op_name, op): | ||
operand1 = klass.__name__.lower() | ||
operand2 = "other" | ||
expected_str = " ".join([operand1, op, operand2]) | ||
assert expected_str in getattr(klass, op_name).__doc__ | ||
|
||
# reverse version of the binary ops | ||
expected_str = " ".join([operand2, op, operand1]) | ||
assert expected_str in getattr(klass, "r" + op_name).__doc__ | ||
|
||
|
||
class TestTranspose(Ops): | ||
|
@@ -313,7 +261,7 @@ def test_value_counts_unique_nunique_null(self, null_obj): | |
klass = type(o) | ||
values = o._ndarray_values | ||
|
||
if not self._allow_na_ops(o): | ||
if not allow_na_ops(o): | ||
continue | ||
|
||
# special assign to the numpy array | ||
|
@@ -795,7 +743,7 @@ def test_fillna(self): | |
o = orig.copy() | ||
klass = type(o) | ||
|
||
if not self._allow_na_ops(o): | ||
if not allow_na_ops(o): | ||
continue | ||
|
||
if needs_i8_conversion(o): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from typing import Any | ||
|
||
import numpy as np | ||
|
||
SaturnFromTitan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from pandas import Index, Series | ||
import pandas.util.testing as tm | ||
|
||
|
||
def allow_na_ops(obj: Any) -> bool: | ||
SaturnFromTitan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Whether to skip test cases including NaN""" | ||
is_bool_index = isinstance(obj, Index) and obj.is_boolean() | ||
return not is_bool_index and obj._can_hold_na | ||
|
||
|
||
def check_ops_properties_valid(obj: Any, op: str) -> None: | ||
""" Validates that certain properties are available """ | ||
if isinstance(obj, Series): | ||
expected = Series(getattr(obj.index, op), index=obj.index, name="a") | ||
else: | ||
expected = getattr(obj, op) | ||
|
||
result = getattr(obj, op) | ||
|
||
# these could be series, arrays or scalars | ||
if isinstance(result, Series) and isinstance(expected, Series): | ||
tm.assert_series_equal(result, expected) | ||
elif isinstance(result, Index) and isinstance(expected, Index): | ||
tm.assert_index_equal(result, expected) | ||
elif isinstance(result, np.ndarray) and isinstance(expected, np.ndarray): | ||
tm.assert_numpy_array_equal(result, expected) | ||
else: | ||
assert result == expected |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,39 +12,45 @@ | |
Index, | ||
PeriodIndex, | ||
Series, | ||
TimedeltaIndex, | ||
Timestamp, | ||
bdate_range, | ||
date_range, | ||
) | ||
from pandas.tests.base.test_ops import Ops | ||
from pandas.tests.base.utils import check_ops_properties_valid | ||
import pandas.util.testing as tm | ||
|
||
from pandas.tseries.offsets import BDay, BMonthEnd, CDay, Day, Hour | ||
|
||
START, END = datetime(2009, 1, 1), datetime(2010, 1, 1) | ||
|
||
|
||
class TestDatetimeIndexOps(Ops): | ||
def setup_method(self, method): | ||
super().setup_method(method) | ||
mask = lambda x: (isinstance(x, DatetimeIndex) or isinstance(x, PeriodIndex)) | ||
self.is_valid_objs = [o for o in self.objs if mask(o)] | ||
self.not_valid_objs = [o for o in self.objs if not mask(o)] | ||
class TestDatetimeIndexOps: | ||
@pytest.mark.parametrize("op", DatetimeIndex._datetimelike_ops) | ||
def test_valid_ops_properties(self, op, index_or_series_obj): | ||
obj = index_or_series_obj | ||
if isinstance(obj, DatetimeIndex): | ||
check_ops_properties_valid(obj, op) | ||
|
||
@pytest.mark.parametrize("op", DatetimeIndex._datetimelike_ops) | ||
def test_invalid_ops_properties(self, op, index_or_series_obj): | ||
obj = index_or_series_obj | ||
if isinstance(obj, (DatetimeIndex, PeriodIndex)): | ||
pytest.skip() | ||
if op == "freq" and isinstance(obj, TimedeltaIndex): | ||
pytest.skip() | ||
|
||
def test_ops_properties(self): | ||
f = lambda x: isinstance(x, DatetimeIndex) | ||
self.check_ops_properties(DatetimeIndex._field_ops, f) | ||
self.check_ops_properties(DatetimeIndex._object_ops, f) | ||
self.check_ops_properties(DatetimeIndex._bool_ops, f) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already tested in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. test_dt_namespace_accessor could use a good refactor if youre up to it (separate PR) |
||
with pytest.raises((AttributeError, TypeError)): | ||
getattr(obj, op) | ||
|
||
def test_ops_properties_basic(self): | ||
def test_ops_properties_basic(self, datetime_series): | ||
|
||
# sanity check that the behavior didn't change | ||
# GH#7206 | ||
msg = "'Series' object has no attribute '{}'" | ||
for op in ["year", "day", "second", "weekday"]: | ||
with pytest.raises(AttributeError, match=msg.format(op)): | ||
getattr(self.dt_series, op) | ||
getattr(datetime_series, op) | ||
|
||
# attribute access should still work! | ||
s = Series(dict(year=2000, month=1, day=10)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,24 +2,29 @@ | |
import pytest | ||
|
||
import pandas as pd | ||
from pandas import DatetimeIndex, Index, NaT, PeriodIndex, Series | ||
from pandas import DatetimeIndex, Index, NaT, PeriodIndex, Series, TimedeltaIndex | ||
from pandas.core.arrays import PeriodArray | ||
from pandas.tests.base.test_ops import Ops | ||
from pandas.tests.base.utils import check_ops_properties_valid | ||
import pandas.util.testing as tm | ||
|
||
|
||
class TestPeriodIndexOps(Ops): | ||
def setup_method(self, method): | ||
super().setup_method(method) | ||
mask = lambda x: (isinstance(x, DatetimeIndex) or isinstance(x, PeriodIndex)) | ||
self.is_valid_objs = [o for o in self.objs if mask(o)] | ||
self.not_valid_objs = [o for o in self.objs if not mask(o)] | ||
class TestPeriodIndexOps: | ||
@pytest.mark.parametrize("op", PeriodArray._datetimelike_ops) | ||
SaturnFromTitan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_valid_ops_properties(self, op, index_or_series_obj): | ||
obj = index_or_series_obj | ||
if isinstance(obj, PeriodIndex): | ||
check_ops_properties_valid(obj, op) | ||
|
||
def test_ops_properties(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already tested in |
||
f = lambda x: isinstance(x, PeriodIndex) | ||
self.check_ops_properties(PeriodArray._field_ops, f) | ||
self.check_ops_properties(PeriodArray._object_ops, f) | ||
self.check_ops_properties(PeriodArray._bool_ops, f) | ||
@pytest.mark.parametrize("op", PeriodArray._datetimelike_ops) | ||
def test_invalid_ops_properties(self, op, index_or_series_obj): | ||
obj = index_or_series_obj | ||
if isinstance(obj, (PeriodIndex, DatetimeIndex)): | ||
pytest.skip() | ||
if op == "freq" and isinstance(obj, TimedeltaIndex): | ||
pytest.skip() | ||
|
||
with pytest.raises((AttributeError, TypeError)): | ||
getattr(obj, op) | ||
|
||
def test_resolution(self): | ||
for freq, expected in zip( | ||
|
Uh oh!
There was an error while loading. Please reload this page.