Skip to content

CLN/TST: parametrize #44888

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

Merged
merged 6 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion asv_bench/benchmarks/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def setup(self, op, shape):
# should already be the case, but just to be sure
df._consolidate_inplace()

# TODO: GH#33198 the setting here shoudlnt need two steps
# TODO: GH#33198 the setting here shouldn't need two steps
arr1 = np.random.randn(n_rows, max(n_cols // 4, 3)).astype("f8")
arr2 = np.random.randn(n_rows, n_cols // 2).astype("i8")
arr3 = np.random.randn(n_rows, n_cols // 4).astype("f8")
Expand Down
3 changes: 0 additions & 3 deletions pandas/compat/pickle_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ def load_reduce(self):
args = stack.pop()
func = stack[-1]

if len(args) and type(args[0]) is type:
n = args[0].__name__ # noqa

try:
stack[-1] = func(*args)
return
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# flake8: noqa
# flake8: noqa:F401

from pandas._libs import (
NaT,
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def asi8(self) -> npt.NDArray[np.int64]:
# ----------------------------------------------------------------
# Rendering Methods

def _format_native_types(self, na_rep="NaT", date_format=None):
def _format_native_types(self, *, na_rep="NaT", date_format=None):
"""
Helper method for astype when converting to strings.

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ def astype(self, dtype, copy: bool = True):

@dtl.ravel_compat
def _format_native_types(
self, na_rep="NaT", date_format=None, **kwargs
self, *, na_rep="NaT", date_format=None, **kwargs
) -> npt.NDArray[np.object_]:
from pandas.io.formats.format import get_format_datetime64_from_values

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ def _formatter(self, boxed: bool = False):

@dtl.ravel_compat
def _format_native_types(
self, na_rep="NaT", date_format=None, **kwargs
self, *, na_rep="NaT", date_format=None, **kwargs
) -> np.ndarray:
"""
actually format my specific types
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def _formatter(self, boxed: bool = False):

@dtl.ravel_compat
def _format_native_types(
self, na_rep="NaT", date_format=None, **kwargs
self, *, na_rep="NaT", date_format=None, **kwargs
) -> np.ndarray:
from pandas.io.formats.format import get_format_timedelta64

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/computation/api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# flake8: noqa

__all__ = ["eval"]
from pandas.core.computation.eval import eval
2 changes: 1 addition & 1 deletion pandas/core/dtypes/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# flake8: noqa
# flake8: noqa:F401

from pandas.core.dtypes.common import (
is_array_like,
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def maybe_downcast_to_dtype(result: ArrayLike, dtype: str | np.dtype) -> ArrayLi

if isinstance(dtype, str):
if dtype == "infer":
inferred_type = lib.infer_dtype(ensure_object(result), skipna=False)
inferred_type = lib.infer_dtype(result, skipna=False)
if inferred_type == "boolean":
dtype = "bool"
elif inferred_type == "integer":
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ def is_bool_dtype(arr_or_dtype) -> bool:
except (TypeError, ValueError):
return False

if isinstance(arr_or_dtype, CategoricalDtype):
if isinstance(dtype, CategoricalDtype):
arr_or_dtype = arr_or_dtype.categories
# now we use the special definition for Index

Expand All @@ -1329,7 +1329,7 @@ def is_bool_dtype(arr_or_dtype) -> bool:
# so its object, we need to infer to
# guess this
return arr_or_dtype.is_object() and arr_or_dtype.inferred_type == "boolean"
elif is_extension_array_dtype(arr_or_dtype):
elif isinstance(dtype, ExtensionDtype):
return getattr(dtype, "_is_boolean", False)

return issubclass(dtype.type, np.bool_)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1387,7 +1387,7 @@ def to_native_types(self, slicer=None, **kwargs) -> np.ndarray:
values = values[slicer]
return values._format_native_types(**kwargs)

def _format_native_types(self, na_rep="", quoting=None, **kwargs):
def _format_native_types(self, *, na_rep="", quoting=None, **kwargs):
"""
Actually format specific types of the index.
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ def _format_with_header(self, header: list[str], na_rep: str) -> list[str]:
# matches base class except for whitespace padding
return header + list(self._format_native_types(na_rep=na_rep))

def _format_native_types(self, na_rep="NaN", quoting=None, **kwargs):
def _format_native_types(self, *, na_rep="NaN", quoting=None, **kwargs):
# GH 28210: use base method but with different default na_rep
return super()._format_native_types(na_rep=na_rep, quoting=quoting, **kwargs)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ def _formatter_func(self, tup):
formatter_funcs = [level._formatter_func for level in self.levels]
return tuple(func(val) for func, val in zip(formatter_funcs, tup))

def _format_native_types(self, na_rep="nan", **kwargs):
def _format_native_types(self, *, na_rep="nan", **kwargs):
new_levels = []
new_codes = []

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def _is_all_dates(self) -> bool:
return False

def _format_native_types(
self, na_rep="", float_format=None, decimal=".", quoting=None, **kwargs
self, *, na_rep="", float_format=None, decimal=".", quoting=None, **kwargs
):
from pandas.io.formats.format import FloatArrayFormatter

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/reshape/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# flake8: noqa
# flake8: noqa:F401

from pandas.core.reshape.concat import concat
from pandas.core.reshape.melt import (
Expand Down
6 changes: 2 additions & 4 deletions pandas/errors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# flake8: noqa

"""
Expose public exceptions & warnings
"""

from pandas._config.config import OptionError
from pandas._config.config import OptionError # noqa:F401

from pandas._libs.tslibs import (
from pandas._libs.tslibs import ( # noqa:F401
OutOfBoundsDatetime,
OutOfBoundsTimedelta,
)
Expand Down
8 changes: 0 additions & 8 deletions pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,6 @@ def test_nat_comparisons(
@pytest.mark.parametrize("dtype", [None, object])
def test_nat_comparisons_scalar(self, dtype, data, box_with_array):
box = box_with_array
if box_with_array is tm.to_array and dtype is object:
# dont bother testing ndarray comparison methods as this fails
# on older numpys (since they check object identity)
return

left = Series(data, dtype=dtype)
left = tm.box_expected(left, box)
Expand Down Expand Up @@ -434,10 +430,6 @@ def test_dti_cmp_datetimelike(self, other, tz_naive_fixture):

@pytest.mark.parametrize("dtype", [None, object])
def test_dti_cmp_nat(self, dtype, box_with_array):
if box_with_array is tm.to_array and dtype is object:
# dont bother testing ndarray comparison methods as this fails
# on older numpys (since they check object identity)
return

left = DatetimeIndex([Timestamp("2011-01-01"), NaT, Timestamp("2011-01-03")])
right = DatetimeIndex([NaT, NaT, Timestamp("2011-01-03")])
Expand Down
6 changes: 2 additions & 4 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ def test_numeric_cmp_string_numexpr_path(self, box_with_array):


class TestNumericArraylikeArithmeticWithDatetimeLike:

# TODO: also check name retentention
@pytest.mark.parametrize("box_cls", [np.array, Index, Series])
@pytest.mark.parametrize(
"left", lefts, ids=lambda x: type(x).__name__ + str(x.dtype)
Expand All @@ -149,7 +147,6 @@ def test_mul_td64arr(self, left, box_cls):
result = right * left
tm.assert_equal(result, expected)

# TODO: also check name retentention
@pytest.mark.parametrize("box_cls", [np.array, Index, Series])
@pytest.mark.parametrize(
"left", lefts, ids=lambda x: type(x).__name__ + str(x.dtype)
Expand Down Expand Up @@ -1241,7 +1238,7 @@ def test_binops_pow(self):
idxs = [RangeIndex(0, 10, 1), RangeIndex(0, 20, 2)]
self.check_binop(ops, scalars, idxs)

# TODO: mod, divmod?
# TODO: divmod?
@pytest.mark.parametrize(
"op",
[
Expand All @@ -1251,6 +1248,7 @@ def test_binops_pow(self):
operator.floordiv,
operator.truediv,
operator.pow,
operator.mod,
],
)
def test_arithmetic_with_frame_or_series(self, op):
Expand Down
54 changes: 35 additions & 19 deletions pandas/tests/arithmetic/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,12 @@ def test_pi_sub_pi_with_nat(self):
expected = pd.Index([pd.NaT, 0 * off, 0 * off, 0 * off, 0 * off])
tm.assert_index_equal(result, expected)

def test_parr_sub_pi_mismatched_freq(self, box_with_array):
def test_parr_sub_pi_mismatched_freq(self, box_with_array, box_with_array2):
rng = period_range("1/1/2000", freq="D", periods=5)
other = period_range("1/6/2000", freq="H", periods=5)
# TODO: parametrize over boxes for other?

rng = tm.box_expected(rng, box_with_array)
other = tm.box_expected(other, box_with_array2)
msg = r"Input has different freq=[HD] from PeriodArray\(freq=[DH]\)"
with pytest.raises(IncompatibleFrequency, match=msg):
rng - other
Expand Down Expand Up @@ -998,58 +998,67 @@ def test_pi_sub_intarray(self, int_holder):
# Timedelta-like (timedelta, timedelta64, Timedelta, Tick)
# TODO: Some of these are misnomers because of non-Tick DateOffsets

def test_pi_add_timedeltalike_minute_gt1(self, three_days):
def test_parr_add_timedeltalike_minute_gt1(self, three_days, box_with_array):
# GH#23031 adding a time-delta-like offset to a PeriodArray that has
# minute frequency with n != 1. A more general case is tested below
# in test_pi_add_timedeltalike_tick_gt1, but here we write out the
# expected result more explicitly.
other = three_days
rng = period_range("2014-05-01", periods=3, freq="2D")
rng = tm.box_expected(rng, box_with_array)

expected = PeriodIndex(["2014-05-04", "2014-05-06", "2014-05-08"], freq="2D")
expected = tm.box_expected(expected, box_with_array)

result = rng + other
tm.assert_index_equal(result, expected)
tm.assert_equal(result, expected)

result = other + rng
tm.assert_index_equal(result, expected)
tm.assert_equal(result, expected)

# subtraction
expected = PeriodIndex(["2014-04-28", "2014-04-30", "2014-05-02"], freq="2D")
expected = tm.box_expected(expected, box_with_array)
result = rng - other
tm.assert_index_equal(result, expected)
tm.assert_equal(result, expected)

msg = "|".join(
[
r"(:?bad operand type for unary -: 'PeriodArray')",
r"(:?cannot subtract PeriodArray from timedelta64\[[hD]\])",
r"bad operand type for unary -: 'PeriodArray'",
r"cannot subtract PeriodArray from timedelta64\[[hD]\]",
]
)
with pytest.raises(TypeError, match=msg):
other - rng

@pytest.mark.parametrize("freqstr", ["5ns", "5us", "5ms", "5s", "5T", "5h", "5d"])
def test_pi_add_timedeltalike_tick_gt1(self, three_days, freqstr):
def test_parr_add_timedeltalike_tick_gt1(self, three_days, freqstr, box_with_array):
# GH#23031 adding a time-delta-like offset to a PeriodArray that has
# tick-like frequency with n != 1
other = three_days
rng = period_range("2014-05-01", periods=6, freq=freqstr)
first = rng[0]
rng = tm.box_expected(rng, box_with_array)

expected = period_range(rng[0] + other, periods=6, freq=freqstr)
expected = period_range(first + other, periods=6, freq=freqstr)
expected = tm.box_expected(expected, box_with_array)

result = rng + other
tm.assert_index_equal(result, expected)
tm.assert_equal(result, expected)

result = other + rng
tm.assert_index_equal(result, expected)
tm.assert_equal(result, expected)

# subtraction
expected = period_range(rng[0] - other, periods=6, freq=freqstr)
expected = period_range(first - other, periods=6, freq=freqstr)
expected = tm.box_expected(expected, box_with_array)
result = rng - other
tm.assert_index_equal(result, expected)
msg = (
r"(:?bad operand type for unary -: 'PeriodArray')"
r"|(:?cannot subtract PeriodArray from timedelta64\[[hD]\])"
tm.assert_equal(result, expected)
msg = "|".join(
[
r"bad operand type for unary -: 'PeriodArray'",
r"cannot subtract PeriodArray from timedelta64\[[hD]\]",
]
)
with pytest.raises(TypeError, match=msg):
other - rng
Expand Down Expand Up @@ -1078,9 +1087,13 @@ def test_pi_sub_isub_timedeltalike_daily(self, three_days):
rng -= other
tm.assert_index_equal(rng, expected)

def test_pi_add_sub_timedeltalike_freq_mismatch_daily(self, not_daily):
def test_parr_add_sub_timedeltalike_freq_mismatch_daily(
self, not_daily, box_with_array
):
other = not_daily
rng = period_range("2014-05-01", "2014-05-15", freq="D")
rng = tm.box_expected(rng, box_with_array)

msg = "Input has different freq(=.+)? from Period.*?\\(freq=D\\)"
with pytest.raises(IncompatibleFrequency, match=msg):
rng + other
Expand All @@ -1102,9 +1115,12 @@ def test_pi_add_iadd_timedeltalike_hourly(self, two_hours):
rng += other
tm.assert_index_equal(rng, expected)

def test_pi_add_timedeltalike_mismatched_freq_hourly(self, not_hourly):
def test_parr_add_timedeltalike_mismatched_freq_hourly(
self, not_hourly, box_with_array
):
other = not_hourly
rng = period_range("2014-01-01 10:00", "2014-01-05 10:00", freq="H")
rng = tm.box_expected(rng, box_with_array)
msg = "Input has different freq(=.+)? from Period.*?\\(freq=H\\)"

with pytest.raises(IncompatibleFrequency, match=msg):
Expand Down
Loading