Skip to content

Commit c166fe9

Browse files
authored
CLN/TST: parametrize (#44888)
1 parent ec85e03 commit c166fe9

26 files changed

+92
-90
lines changed

asv_bench/benchmarks/arithmetic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def setup(self, op, shape):
144144
# should already be the case, but just to be sure
145145
df._consolidate_inplace()
146146

147-
# TODO: GH#33198 the setting here shoudlnt need two steps
147+
# TODO: GH#33198 the setting here shouldn't need two steps
148148
arr1 = np.random.randn(n_rows, max(n_cols // 4, 3)).astype("f8")
149149
arr2 = np.random.randn(n_rows, n_cols // 2).astype("i8")
150150
arr3 = np.random.randn(n_rows, n_cols // 4).astype("f8")

pandas/compat/pickle_compat.py

-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ def load_reduce(self):
3535
args = stack.pop()
3636
func = stack[-1]
3737

38-
if len(args) and type(args[0]) is type:
39-
n = args[0].__name__ # noqa
40-
4138
try:
4239
stack[-1] = func(*args)
4340
return

pandas/core/api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# flake8: noqa
1+
# flake8: noqa:F401
22

33
from pandas._libs import (
44
NaT,

pandas/core/arrays/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def asi8(self) -> npt.NDArray[np.int64]:
291291
# ----------------------------------------------------------------
292292
# Rendering Methods
293293

294-
def _format_native_types(self, na_rep="NaT", date_format=None):
294+
def _format_native_types(self, *, na_rep="NaT", date_format=None):
295295
"""
296296
Helper method for astype when converting to strings.
297297

pandas/core/arrays/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ def astype(self, dtype, copy: bool = True):
670670

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

pandas/core/arrays/period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ def _formatter(self, boxed: bool = False):
632632

633633
@dtl.ravel_compat
634634
def _format_native_types(
635-
self, na_rep="NaT", date_format=None, **kwargs
635+
self, *, na_rep="NaT", date_format=None, **kwargs
636636
) -> np.ndarray:
637637
"""
638638
actually format my specific types

pandas/core/arrays/timedeltas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def _formatter(self, boxed: bool = False):
426426

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

pandas/core/computation/api.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
# flake8: noqa
2-
1+
__all__ = ["eval"]
32
from pandas.core.computation.eval import eval

pandas/core/dtypes/api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# flake8: noqa
1+
# flake8: noqa:F401
22

33
from pandas.core.dtypes.common import (
44
is_array_like,

pandas/core/dtypes/cast.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def maybe_downcast_to_dtype(result: ArrayLike, dtype: str | np.dtype) -> ArrayLi
248248

249249
if isinstance(dtype, str):
250250
if dtype == "infer":
251-
inferred_type = lib.infer_dtype(ensure_object(result), skipna=False)
251+
inferred_type = lib.infer_dtype(result, skipna=False)
252252
if inferred_type == "boolean":
253253
dtype = "bool"
254254
elif inferred_type == "integer":

pandas/core/dtypes/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,7 @@ def is_bool_dtype(arr_or_dtype) -> bool:
13181318
except (TypeError, ValueError):
13191319
return False
13201320

1321-
if isinstance(arr_or_dtype, CategoricalDtype):
1321+
if isinstance(dtype, CategoricalDtype):
13221322
arr_or_dtype = arr_or_dtype.categories
13231323
# now we use the special definition for Index
13241324

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

13351335
return issubclass(dtype.type, np.bool_)

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ def to_native_types(self, slicer=None, **kwargs) -> np.ndarray:
13871387
values = values[slicer]
13881388
return values._format_native_types(**kwargs)
13891389

1390-
def _format_native_types(self, na_rep="", quoting=None, **kwargs):
1390+
def _format_native_types(self, *, na_rep="", quoting=None, **kwargs):
13911391
"""
13921392
Actually format specific types of the index.
13931393
"""

pandas/core/indexes/interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ def _format_with_header(self, header: list[str], na_rep: str) -> list[str]:
812812
# matches base class except for whitespace padding
813813
return header + list(self._format_native_types(na_rep=na_rep))
814814

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

pandas/core/indexes/multi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ def _formatter_func(self, tup):
12831283
formatter_funcs = [level._formatter_func for level in self.levels]
12841284
return tuple(func(val) for func, val in zip(formatter_funcs, tup))
12851285

1286-
def _format_native_types(self, na_rep="nan", **kwargs):
1286+
def _format_native_types(self, *, na_rep="nan", **kwargs):
12871287
new_levels = []
12881288
new_codes = []
12891289

pandas/core/indexes/numeric.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def _is_all_dates(self) -> bool:
343343
return False
344344

345345
def _format_native_types(
346-
self, na_rep="", float_format=None, decimal=".", quoting=None, **kwargs
346+
self, *, na_rep="", float_format=None, decimal=".", quoting=None, **kwargs
347347
):
348348
from pandas.io.formats.format import FloatArrayFormatter
349349

pandas/core/reshape/api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# flake8: noqa
1+
# flake8: noqa:F401
22

33
from pandas.core.reshape.concat import concat
44
from pandas.core.reshape.melt import (

pandas/errors/__init__.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
# flake8: noqa
2-
31
"""
42
Expose public exceptions & warnings
53
"""
64

7-
from pandas._config.config import OptionError
5+
from pandas._config.config import OptionError # noqa:F401
86

9-
from pandas._libs.tslibs import (
7+
from pandas._libs.tslibs import ( # noqa:F401
108
OutOfBoundsDatetime,
119
OutOfBoundsTimedelta,
1210
)

pandas/tests/arithmetic/test_datetime64.py

-8
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,6 @@ def test_nat_comparisons(
228228
@pytest.mark.parametrize("dtype", [None, object])
229229
def test_nat_comparisons_scalar(self, dtype, data, box_with_array):
230230
box = box_with_array
231-
if box_with_array is tm.to_array and dtype is object:
232-
# dont bother testing ndarray comparison methods as this fails
233-
# on older numpys (since they check object identity)
234-
return
235231

236232
left = Series(data, dtype=dtype)
237233
left = tm.box_expected(left, box)
@@ -433,10 +429,6 @@ def test_dti_cmp_datetimelike(self, other, tz_naive_fixture):
433429

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

441433
left = DatetimeIndex([Timestamp("2011-01-01"), NaT, Timestamp("2011-01-03")])
442434
right = DatetimeIndex([NaT, NaT, Timestamp("2011-01-03")])

pandas/tests/arithmetic/test_numeric.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,6 @@ def test_numeric_cmp_string_numexpr_path(self, box_with_array):
128128

129129

130130
class TestNumericArraylikeArithmeticWithDatetimeLike:
131-
132-
# TODO: also check name retentention
133131
@pytest.mark.parametrize("box_cls", [np.array, Index, Series])
134132
@pytest.mark.parametrize(
135133
"left", lefts, ids=lambda x: type(x).__name__ + str(x.dtype)
@@ -149,7 +147,6 @@ def test_mul_td64arr(self, left, box_cls):
149147
result = right * left
150148
tm.assert_equal(result, expected)
151149

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

1244-
# TODO: mod, divmod?
1241+
# TODO: divmod?
12451242
@pytest.mark.parametrize(
12461243
"op",
12471244
[
@@ -1251,6 +1248,7 @@ def test_binops_pow(self):
12511248
operator.floordiv,
12521249
operator.truediv,
12531250
operator.pow,
1251+
operator.mod,
12541252
],
12551253
)
12561254
def test_arithmetic_with_frame_or_series(self, op):

pandas/tests/arithmetic/test_period.py

+35-19
Original file line numberDiff line numberDiff line change
@@ -636,12 +636,12 @@ def test_pi_sub_pi_with_nat(self):
636636
expected = pd.Index([pd.NaT, 0 * off, 0 * off, 0 * off, 0 * off])
637637
tm.assert_index_equal(result, expected)
638638

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

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

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

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

10111013
result = rng + other
1012-
tm.assert_index_equal(result, expected)
1014+
tm.assert_equal(result, expected)
10131015

10141016
result = other + rng
1015-
tm.assert_index_equal(result, expected)
1017+
tm.assert_equal(result, expected)
10161018

10171019
# subtraction
10181020
expected = PeriodIndex(["2014-04-28", "2014-04-30", "2014-05-02"], freq="2D")
1021+
expected = tm.box_expected(expected, box_with_array)
10191022
result = rng - other
1020-
tm.assert_index_equal(result, expected)
1023+
tm.assert_equal(result, expected)
10211024

10221025
msg = "|".join(
10231026
[
1024-
r"(:?bad operand type for unary -: 'PeriodArray')",
1025-
r"(:?cannot subtract PeriodArray from timedelta64\[[hD]\])",
1027+
r"bad operand type for unary -: 'PeriodArray'",
1028+
r"cannot subtract PeriodArray from timedelta64\[[hD]\]",
10261029
]
10271030
)
10281031
with pytest.raises(TypeError, match=msg):
10291032
other - rng
10301033

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

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

10401046
result = rng + other
1041-
tm.assert_index_equal(result, expected)
1047+
tm.assert_equal(result, expected)
10421048

10431049
result = other + rng
1044-
tm.assert_index_equal(result, expected)
1050+
tm.assert_equal(result, expected)
10451051

10461052
# subtraction
1047-
expected = period_range(rng[0] - other, periods=6, freq=freqstr)
1053+
expected = period_range(first - other, periods=6, freq=freqstr)
1054+
expected = tm.box_expected(expected, box_with_array)
10481055
result = rng - other
1049-
tm.assert_index_equal(result, expected)
1050-
msg = (
1051-
r"(:?bad operand type for unary -: 'PeriodArray')"
1052-
r"|(:?cannot subtract PeriodArray from timedelta64\[[hD]\])"
1056+
tm.assert_equal(result, expected)
1057+
msg = "|".join(
1058+
[
1059+
r"bad operand type for unary -: 'PeriodArray'",
1060+
r"cannot subtract PeriodArray from timedelta64\[[hD]\]",
1061+
]
10531062
)
10541063
with pytest.raises(TypeError, match=msg):
10551064
other - rng
@@ -1078,9 +1087,13 @@ def test_pi_sub_isub_timedeltalike_daily(self, three_days):
10781087
rng -= other
10791088
tm.assert_index_equal(rng, expected)
10801089

1081-
def test_pi_add_sub_timedeltalike_freq_mismatch_daily(self, not_daily):
1090+
def test_parr_add_sub_timedeltalike_freq_mismatch_daily(
1091+
self, not_daily, box_with_array
1092+
):
10821093
other = not_daily
10831094
rng = period_range("2014-05-01", "2014-05-15", freq="D")
1095+
rng = tm.box_expected(rng, box_with_array)
1096+
10841097
msg = "Input has different freq(=.+)? from Period.*?\\(freq=D\\)"
10851098
with pytest.raises(IncompatibleFrequency, match=msg):
10861099
rng + other
@@ -1102,9 +1115,12 @@ def test_pi_add_iadd_timedeltalike_hourly(self, two_hours):
11021115
rng += other
11031116
tm.assert_index_equal(rng, expected)
11041117

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

11101126
with pytest.raises(IncompatibleFrequency, match=msg):

0 commit comments

Comments
 (0)