-
-
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 all 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 |
---|---|---|
|
@@ -265,7 +265,7 @@ def _get_opstr(op): | |
rtruediv: "/", | ||
operator.floordiv: "//", | ||
rfloordiv: "//", | ||
operator.mod: None, # TODO: Why None for mod but '%' for rmod? | ||
operator.mod: "%", | ||
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. do we have tests where this makes a difference? maybe this lets us use numexpr and performance is affected? 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. Yeah, I wasn't sure about this one. Thought I give it a try and see if any of the CI hooks is failing. The 2 failing ones 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. @jbrockmendel CI is green now, so all tests seem to be fine with this change. 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. looks ok, original issue is that this looks like a format string to python |
||
rmod: "%", | ||
operator.pow: "**", | ||
rpow: "**", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from datetime import datetime, timedelta | ||
from io import StringIO | ||
import sys | ||
from typing import Any | ||
|
||
import numpy as np | ||
import pytest | ||
|
@@ -30,17 +31,15 @@ | |
Timestamp, | ||
) | ||
import pandas._testing as tm | ||
from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin | ||
|
||
|
||
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 allow_na_ops(obj: Any) -> bool: | ||
"""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 | ||
|
||
|
||
class Ops: | ||
def setup_method(self, method): | ||
self.bool_index = tm.makeBoolIndex(10, name="a") | ||
self.int_index = tm.makeIntIndex(10, name="a") | ||
|
@@ -83,74 +82,31 @@ 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 | ||
msg = "no attribute" | ||
err = AttributeError | ||
if issubclass(type(o), DatetimeIndexOpsMixin): | ||
err = TypeError | ||
with pytest.raises(err, match=msg): | ||
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( | ||
"op_name, op", | ||
[ | ||
("add", "+"), | ||
SaturnFromTitan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
("sub", "-"), | ||
("mul", "*"), | ||
("mod", "%"), | ||
("pow", "**"), | ||
("truediv", "/"), | ||
("floordiv", "//"), | ||
], | ||
) | ||
@pytest.mark.parametrize("klass", [Series, DataFrame]) | ||
def test_binary_ops(klass, op_name, op): | ||
# not using the all_arithmetic_functions fixture with _get_opstr | ||
# as _get_opstr is used internally in the dynamic implementation of the docstring | ||
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 +269,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 | ||
|
@@ -794,7 +750,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 |
---|---|---|
|
@@ -7,44 +7,23 @@ | |
from pandas.core.dtypes.generic import ABCDateOffset | ||
|
||
import pandas as pd | ||
from pandas import ( | ||
DatetimeIndex, | ||
Index, | ||
PeriodIndex, | ||
Series, | ||
Timestamp, | ||
bdate_range, | ||
date_range, | ||
) | ||
from pandas import DatetimeIndex, Index, Series, Timestamp, bdate_range, date_range | ||
import pandas._testing as tm | ||
from pandas.tests.base.test_ops import Ops | ||
|
||
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)] | ||
|
||
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) |
||
|
||
def test_ops_properties_basic(self): | ||
class TestDatetimeIndexOps: | ||
def test_ops_properties_basic(self, datetime_series): | ||
|
||
# sanity check that the behavior didn't change | ||
# GH#7206 | ||
for op in ["year", "day", "second", "weekday"]: | ||
msg = f"'Series' object has no attribute '{op}'" | ||
with pytest.raises(AttributeError, match=msg): | ||
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,25 +2,11 @@ | |
import pytest | ||
|
||
import pandas as pd | ||
from pandas import DatetimeIndex, Index, NaT, PeriodIndex, Series | ||
from pandas import Index, NaT, PeriodIndex, Series | ||
import pandas._testing as tm | ||
from pandas.core.arrays import PeriodArray | ||
from pandas.tests.base.test_ops import Ops | ||
|
||
|
||
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)] | ||
|
||
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) | ||
|
||
class TestPeriodIndexOps: | ||
def test_resolution(self): | ||
for freq, expected in zip( | ||
["A", "Q", "M", "D", "H", "T", "S", "L", "U"], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,26 +8,13 @@ | |
import pandas as pd | ||
from pandas import Series, TimedeltaIndex, timedelta_range | ||
import pandas._testing as tm | ||
from pandas.tests.base.test_ops import Ops | ||
|
||
from pandas.tseries.offsets import Day, Hour | ||
|
||
|
||
class TestTimedeltaIndexOps(Ops): | ||
def setup_method(self, method): | ||
super().setup_method(method) | ||
mask = lambda x: isinstance(x, TimedeltaIndex) | ||
self.is_valid_objs = [o for o in self.objs if mask(o)] | ||
self.not_valid_objs = [] | ||
|
||
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, TimedeltaIndex) | ||
self.check_ops_properties(TimedeltaIndex._field_ops, f) | ||
self.check_ops_properties(TimedeltaIndex._object_ops, f) | ||
|
||
class TestTimedeltaIndexOps: | ||
def test_value_counts_unique(self): | ||
# GH 7735 | ||
|
||
idx = timedelta_range("1 days 09:00:00", freq="H", periods=10) | ||
# create repeated values, 'n'th element is repeated by n+1 times | ||
idx = TimedeltaIndex(np.repeat(idx.values, range(1, len(idx) + 1))) | ||
|
Uh oh!
There was an error while loading. Please reload this page.