diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3b788cc2df227..e5116aa90bbeb 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -95,7 +95,7 @@ repos: entry: python scripts/check_for_inconsistent_pandas_namespace.py language: python types: [python] - files: ^pandas/tests/frame/ + files: ^pandas/tests/ - id: incorrect-code-directives name: Check for incorrect code block or IPython directives language: pygrep diff --git a/pandas/tests/arithmetic/test_interval.py b/pandas/tests/arithmetic/test_interval.py index 46db9100b8b93..f1815b3e05367 100644 --- a/pandas/tests/arithmetic/test_interval.py +++ b/pandas/tests/arithmetic/test_interval.py @@ -50,7 +50,7 @@ def left_right_dtypes(request): @pytest.fixture -def array(left_right_dtypes): +def interval_array(left_right_dtypes): """ Fixture to generate an IntervalArray of various dtypes containing NA if possible """ @@ -98,42 +98,42 @@ def interval_constructor(self, request): """ return request.param - def elementwise_comparison(self, op, array, other): + def elementwise_comparison(self, op, interval_array, other): """ Helper that performs elementwise comparisons between `array` and `other` """ - other = other if is_list_like(other) else [other] * len(array) - expected = np.array([op(x, y) for x, y in zip(array, other)]) + other = other if is_list_like(other) else [other] * len(interval_array) + expected = np.array([op(x, y) for x, y in zip(interval_array, other)]) if isinstance(other, Series): return Series(expected, index=other.index) return expected - def test_compare_scalar_interval(self, op, array): + def test_compare_scalar_interval(self, op, interval_array): # matches first interval - other = array[0] - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + other = interval_array[0] + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_numpy_array_equal(result, expected) # matches on a single endpoint but not both - other = Interval(array.left[0], array.right[1]) - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + other = Interval(interval_array.left[0], interval_array.right[1]) + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_numpy_array_equal(result, expected) def test_compare_scalar_interval_mixed_closed(self, op, closed, other_closed): - array = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed) + interval_array = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed) other = Interval(0, 1, closed=other_closed) - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_numpy_array_equal(result, expected) - def test_compare_scalar_na(self, op, array, nulls_fixture, request): - result = op(array, nulls_fixture) - expected = self.elementwise_comparison(op, array, nulls_fixture) + def test_compare_scalar_na(self, op, interval_array, nulls_fixture, request): + result = op(interval_array, nulls_fixture) + expected = self.elementwise_comparison(op, interval_array, nulls_fixture) - if nulls_fixture is pd.NA and array.dtype.subtype != "int64": + if nulls_fixture is pd.NA and interval_array.dtype.subtype != "int64": mark = pytest.mark.xfail( reason="broken for non-integer IntervalArray; see GH 31882" ) @@ -154,38 +154,40 @@ def test_compare_scalar_na(self, op, array, nulls_fixture, request): Period("2017-01-01", "D"), ], ) - def test_compare_scalar_other(self, op, array, other): - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + def test_compare_scalar_other(self, op, interval_array, other): + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_numpy_array_equal(result, expected) - def test_compare_list_like_interval(self, op, array, interval_constructor): + def test_compare_list_like_interval(self, op, interval_array, interval_constructor): # same endpoints - other = interval_constructor(array.left, array.right) - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + other = interval_constructor(interval_array.left, interval_array.right) + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_equal(result, expected) # different endpoints - other = interval_constructor(array.left[::-1], array.right[::-1]) - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + other = interval_constructor( + interval_array.left[::-1], interval_array.right[::-1] + ) + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_equal(result, expected) # all nan endpoints other = interval_constructor([np.nan] * 4, [np.nan] * 4) - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_equal(result, expected) def test_compare_list_like_interval_mixed_closed( self, op, interval_constructor, closed, other_closed ): - array = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed) + interval_array = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed) other = interval_constructor(range(2), range(1, 3), closed=other_closed) - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_equal(result, expected) @pytest.mark.parametrize( @@ -206,17 +208,17 @@ def test_compare_list_like_interval_mixed_closed( ), ], ) - def test_compare_list_like_object(self, op, array, other): - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + def test_compare_list_like_object(self, op, interval_array, other): + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_numpy_array_equal(result, expected) - def test_compare_list_like_nan(self, op, array, nulls_fixture, request): + def test_compare_list_like_nan(self, op, interval_array, nulls_fixture, request): other = [nulls_fixture] * 4 - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) - if nulls_fixture is pd.NA and array.dtype.subtype != "i8": + if nulls_fixture is pd.NA and interval_array.dtype.subtype != "i8": reason = "broken for non-integer IntervalArray; see GH 31882" mark = pytest.mark.xfail(reason=reason) request.node.add_marker(mark) @@ -239,18 +241,18 @@ def test_compare_list_like_nan(self, op, array, nulls_fixture, request): ], ids=lambda x: str(x.dtype), ) - def test_compare_list_like_other(self, op, array, other): - result = op(array, other) - expected = self.elementwise_comparison(op, array, other) + def test_compare_list_like_other(self, op, interval_array, other): + result = op(interval_array, other) + expected = self.elementwise_comparison(op, interval_array, other) tm.assert_numpy_array_equal(result, expected) @pytest.mark.parametrize("length", [1, 3, 5]) @pytest.mark.parametrize("other_constructor", [IntervalArray, list]) def test_compare_length_mismatch_errors(self, op, other_constructor, length): - array = IntervalArray.from_arrays(range(4), range(1, 5)) + interval_array = IntervalArray.from_arrays(range(4), range(1, 5)) other = other_constructor([Interval(0, 1)] * length) with pytest.raises(ValueError, match="Lengths must match to compare"): - op(array, other) + op(interval_array, other) @pytest.mark.parametrize( "constructor, expected_type, assert_func", diff --git a/pandas/tests/arrays/sparse/test_array.py b/pandas/tests/arrays/sparse/test_array.py index 4666c13c2af61..e073871f96bb4 100644 --- a/pandas/tests/arrays/sparse/test_array.py +++ b/pandas/tests/arrays/sparse/test_array.py @@ -522,7 +522,7 @@ def test_astype_all(self, any_real_dtype): tm.assert_numpy_array_equal(np.asarray(res.to_dense()), vals.astype(typ)) @pytest.mark.parametrize( - "array, dtype, expected", + "arr, dtype, expected", [ ( SparseArray([0, 1]), @@ -557,8 +557,8 @@ def test_astype_all(self, any_real_dtype): ), ], ) - def test_astype_more(self, array, dtype, expected): - result = array.astype(dtype) + def test_astype_more(self, arr, dtype, expected): + result = arr.astype(dtype) tm.assert_sp_array_equal(result, expected) def test_astype_nan_raises(self): diff --git a/pandas/tests/arrays/string_/test_string.py b/pandas/tests/arrays/string_/test_string.py index d5254adc1ee24..0574061a6a544 100644 --- a/pandas/tests/arrays/string_/test_string.py +++ b/pandas/tests/arrays/string_/test_string.py @@ -223,31 +223,31 @@ def test_mul(dtype, request): @pytest.mark.xfail(reason="GH-28527") def test_add_strings(dtype): - array = pd.array(["a", "b", "c", "d"], dtype=dtype) + arr = pd.array(["a", "b", "c", "d"], dtype=dtype) df = pd.DataFrame([["t", "u", "v", "w"]]) - assert array.__add__(df) is NotImplemented + assert arr.__add__(df) is NotImplemented - result = array + df + result = arr + df expected = pd.DataFrame([["at", "bu", "cv", "dw"]]).astype(dtype) tm.assert_frame_equal(result, expected) - result = df + array + result = df + arr expected = pd.DataFrame([["ta", "ub", "vc", "wd"]]).astype(dtype) tm.assert_frame_equal(result, expected) @pytest.mark.xfail(reason="GH-28527") def test_add_frame(dtype): - array = pd.array(["a", "b", np.nan, np.nan], dtype=dtype) + arr = pd.array(["a", "b", np.nan, np.nan], dtype=dtype) df = pd.DataFrame([["x", np.nan, "y", np.nan]]) - assert array.__add__(df) is NotImplemented + assert arr.__add__(df) is NotImplemented - result = array + df + result = arr + df expected = pd.DataFrame([["ax", np.nan, np.nan, np.nan]]).astype(dtype) tm.assert_frame_equal(result, expected) - result = df + array + result = df + arr expected = pd.DataFrame([["xa", np.nan, np.nan, np.nan]]).astype(dtype) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index ce39de88dd0dc..3cb67b7a57a8e 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -1160,7 +1160,7 @@ def test_strftime_nat(self): @pytest.mark.parametrize( - "array,casting_nats", + "arr,casting_nats", [ ( TimedeltaIndex(["1 Day", "3 Hours", "NaT"])._data, @@ -1174,17 +1174,17 @@ def test_strftime_nat(self): ], ids=lambda x: type(x).__name__, ) -def test_casting_nat_setitem_array(array, casting_nats): - expected = type(array)._from_sequence([NaT, array[1], array[2]]) +def test_casting_nat_setitem_array(arr, casting_nats): + expected = type(arr)._from_sequence([NaT, arr[1], arr[2]]) for nat in casting_nats: - arr = array.copy() + arr = arr.copy() arr[0] = nat tm.assert_equal(arr, expected) @pytest.mark.parametrize( - "array,non_casting_nats", + "arr,non_casting_nats", [ ( TimedeltaIndex(["1 Day", "3 Hours", "NaT"])._data, @@ -1201,7 +1201,7 @@ def test_casting_nat_setitem_array(array, casting_nats): ], ids=lambda x: type(x).__name__, ) -def test_invalid_nat_setitem_array(array, non_casting_nats): +def test_invalid_nat_setitem_array(arr, non_casting_nats): msg = ( "value should be a '(Timestamp|Timedelta|Period)', 'NaT', or array of those. " "Got '(timedelta64|datetime64|int)' instead." @@ -1209,42 +1209,42 @@ def test_invalid_nat_setitem_array(array, non_casting_nats): for nat in non_casting_nats: with pytest.raises(TypeError, match=msg): - array[0] = nat + arr[0] = nat @pytest.mark.parametrize( - "array", + "arr", [ pd.date_range("2000", periods=4).array, pd.timedelta_range("2000", periods=4).array, ], ) -def test_to_numpy_extra(array): +def test_to_numpy_extra(arr): if np_version_under1p18: # np.isnan(NaT) raises, so use pandas' isnan = pd.isna else: isnan = np.isnan - array[0] = NaT - original = array.copy() + arr[0] = NaT + original = arr.copy() - result = array.to_numpy() + result = arr.to_numpy() assert isnan(result[0]) - result = array.to_numpy(dtype="int64") + result = arr.to_numpy(dtype="int64") assert result[0] == -9223372036854775808 - result = array.to_numpy(dtype="int64", na_value=0) + result = arr.to_numpy(dtype="int64", na_value=0) assert result[0] == 0 - result = array.to_numpy(na_value=array[1].to_numpy()) + result = arr.to_numpy(na_value=arr[1].to_numpy()) assert result[0] == result[1] - result = array.to_numpy(na_value=array[1].to_numpy(copy=False)) + result = arr.to_numpy(na_value=arr[1].to_numpy(copy=False)) assert result[0] == result[1] - tm.assert_equal(array, original) + tm.assert_equal(arr, original) @pytest.mark.parametrize("as_index", [True, False]) diff --git a/pandas/tests/base/test_conversion.py b/pandas/tests/base/test_conversion.py index bb1ff2abd9bc4..7045a0abbeb81 100644 --- a/pandas/tests/base/test_conversion.py +++ b/pandas/tests/base/test_conversion.py @@ -177,7 +177,7 @@ def test_iter_box(self): @pytest.mark.parametrize( - "array, expected_type, dtype", + "arr, expected_type, dtype", [ (np.array([0, 1], dtype=np.int64), np.ndarray, "int64"), (np.array(["a", "b"]), np.ndarray, "object"), @@ -216,9 +216,9 @@ def test_iter_box(self): ), ], ) -def test_values_consistent(array, expected_type, dtype): - l_values = Series(array)._values - r_values = pd.Index(array)._values +def test_values_consistent(arr, expected_type, dtype): + l_values = Series(arr)._values + r_values = pd.Index(arr)._values assert type(l_values) is expected_type assert type(l_values) is type(r_values) @@ -245,7 +245,7 @@ def test_numpy_array_all_dtypes(any_numpy_dtype): @pytest.mark.parametrize( - "array, attr", + "arr, attr", [ (pd.Categorical(["a", "b"]), "_codes"), (pd.core.arrays.period_array(["2000", "2001"], freq="D"), "_data"), @@ -265,17 +265,17 @@ def test_numpy_array_all_dtypes(any_numpy_dtype): ), ], ) -def test_array(array, attr, index_or_series): +def test_array(arr, attr, index_or_series): box = index_or_series - if array.dtype.name in ("Int64", "Sparse[int64, 0]") and box is pd.Index: - pytest.skip(f"No index type for {array.dtype}") - result = box(array, copy=False).array + if arr.dtype.name in ("Int64", "Sparse[int64, 0]") and box is pd.Index: + pytest.skip(f"No index type for {arr.dtype}") + result = box(arr, copy=False).array if attr: - array = getattr(array, attr) + arr = getattr(arr, attr) result = getattr(result, attr) - assert result is array + assert result is arr def test_array_multiindex_raises(): @@ -286,7 +286,7 @@ def test_array_multiindex_raises(): @pytest.mark.parametrize( - "array, expected", + "arr, expected", [ (np.array([1, 2], dtype=np.int64), np.array([1, 2], dtype=np.int64)), (pd.Categorical(["a", "b"]), np.array(["a", "b"], dtype=object)), @@ -337,14 +337,14 @@ def test_array_multiindex_raises(): ), ], ) -def test_to_numpy(array, expected, index_or_series_or_array, request): +def test_to_numpy(arr, expected, index_or_series_or_array, request): box = index_or_series_or_array - thing = box(array) + thing = box(arr) - if array.dtype.name in ("Int64", "Sparse[int64, 0]") and box is pd.Index: - pytest.skip(f"No index type for {array.dtype}") + if arr.dtype.name in ("Int64", "Sparse[int64, 0]") and box is pd.Index: + pytest.skip(f"No index type for {arr.dtype}") - if array.dtype.name == "int64" and box is pd.array: + if arr.dtype.name == "int64" and box is pd.array: mark = pytest.mark.xfail(reason="thing is Int64 and to_numpy() returns object") request.node.add_marker(mark) diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index 78a62c832833f..b3c6015475674 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -616,9 +616,9 @@ def test_maybe_convert_objects_bool_nan(self): def test_mixed_dtypes_remain_object_array(self): # GH14956 - array = np.array([datetime(2015, 1, 1, tzinfo=pytz.utc), 1], dtype=object) - result = lib.maybe_convert_objects(array, convert_datetime=True) - tm.assert_numpy_array_equal(result, array) + arr = np.array([datetime(2015, 1, 1, tzinfo=pytz.utc), 1], dtype=object) + result = lib.maybe_convert_objects(arr, convert_datetime=True) + tm.assert_numpy_array_equal(result, arr) class TestTypeInference: diff --git a/pandas/tests/extension/base/getitem.py b/pandas/tests/extension/base/getitem.py index 286ed9c736f31..a7b99c2e09e88 100644 --- a/pandas/tests/extension/base/getitem.py +++ b/pandas/tests/extension/base/getitem.py @@ -324,11 +324,11 @@ def test_take_non_na_fill_value(self, data_missing): fill_value = data_missing[1] # valid na = data_missing[0] - array = data_missing._from_sequence( + arr = data_missing._from_sequence( [na, fill_value, na], dtype=data_missing.dtype ) - result = array.take([-1, 1], fill_value=fill_value, allow_fill=True) - expected = array.take([1, 1]) + result = arr.take([-1, 1], fill_value=fill_value, allow_fill=True) + expected = arr.take([1, 1]) self.assert_extension_array_equal(result, expected) def test_take_pandas_style_negative_raises(self, data, na_value): @@ -375,8 +375,8 @@ def test_reindex_non_na_fill_value(self, data_missing): valid = data_missing[1] na = data_missing[0] - array = data_missing._from_sequence([na, valid], dtype=data_missing.dtype) - ser = pd.Series(array) + arr = data_missing._from_sequence([na, valid], dtype=data_missing.dtype) + ser = pd.Series(arr) result = ser.reindex([0, 1, 2], fill_value=valid) expected = pd.Series( data_missing._from_sequence([na, valid, valid], dtype=data_missing.dtype) diff --git a/pandas/tests/indexes/period/test_setops.py b/pandas/tests/indexes/period/test_setops.py index 5b9528d185827..ce5c46dd55c0d 100644 --- a/pandas/tests/indexes/period/test_setops.py +++ b/pandas/tests/indexes/period/test_setops.py @@ -351,10 +351,10 @@ def test_intersection_equal_duplicates(self): def test_union_duplicates(self): # GH#36289 - idx = pd.period_range("2011-01-01", periods=2) + idx = period_range("2011-01-01", periods=2) idx_dup = idx.append(idx) - idx2 = pd.period_range("2011-01-02", periods=2) + idx2 = period_range("2011-01-02", periods=2) idx2_dup = idx2.append(idx2) result = idx_dup.union(idx2_dup) diff --git a/pandas/tests/indexing/interval/test_interval.py b/pandas/tests/indexing/interval/test_interval.py index eaf597e6bf978..eb13ec1f366af 100644 --- a/pandas/tests/indexing/interval/test_interval.py +++ b/pandas/tests/indexing/interval/test_interval.py @@ -124,7 +124,7 @@ def test_mi_intervalindex_slicing_with_scalar(self): pd.Index( ["RID1", "RID1", "RID2", "RID2", "RID1", "RID1", "RID2", "RID2"] ), - pd.IntervalIndex.from_arrays( + IntervalIndex.from_arrays( [0, 1, 10, 11, 0, 1, 10, 11], [1, 2, 11, 12, 1, 2, 11, 12] ), ] diff --git a/pandas/tests/indexing/multiindex/test_multiindex.py b/pandas/tests/indexing/multiindex/test_multiindex.py index e00b77003ebdc..41c2c61154f08 100644 --- a/pandas/tests/indexing/multiindex/test_multiindex.py +++ b/pandas/tests/indexing/multiindex/test_multiindex.py @@ -75,7 +75,7 @@ def test_nested_tuples_duplicates(self): dti = pd.to_datetime(["20190101", "20190101", "20190102"]) idx = Index(["a", "a", "c"]) - mi = pd.MultiIndex.from_arrays([dti, idx], names=["index1", "index2"]) + mi = MultiIndex.from_arrays([dti, idx], names=["index1", "index2"]) df = DataFrame({"c1": [1, 2, 3], "c2": [np.nan, np.nan, np.nan]}, index=mi) diff --git a/pandas/tests/indexing/multiindex/test_slice.py b/pandas/tests/indexing/multiindex/test_slice.py index 90f2d07ad77f6..42edaa2fe6c3a 100644 --- a/pandas/tests/indexing/multiindex/test_slice.py +++ b/pandas/tests/indexing/multiindex/test_slice.py @@ -534,9 +534,7 @@ def test_loc_axis_single_level_multi_col_indexing_multiindex_col_df(self): # GH29519 df = DataFrame( np.arange(27).reshape(3, 9), - columns=pd.MultiIndex.from_product( - [["a1", "a2", "a3"], ["b1", "b2", "b3"]] - ), + columns=MultiIndex.from_product([["a1", "a2", "a3"], ["b1", "b2", "b3"]]), ) result = df.loc(axis=1)["a1":"a2"] expected = df.iloc[:, :-3] @@ -548,9 +546,7 @@ def test_loc_axis_single_level_single_col_indexing_multiindex_col_df(self): # GH29519 df = DataFrame( np.arange(27).reshape(3, 9), - columns=pd.MultiIndex.from_product( - [["a1", "a2", "a3"], ["b1", "b2", "b3"]] - ), + columns=MultiIndex.from_product([["a1", "a2", "a3"], ["b1", "b2", "b3"]]), ) result = df.loc(axis=1)["a1"] expected = df.iloc[:, :3] diff --git a/pandas/tests/indexing/test_check_indexer.py b/pandas/tests/indexing/test_check_indexer.py index 865ecb129cdfa..0e52c075d5af5 100644 --- a/pandas/tests/indexing/test_check_indexer.py +++ b/pandas/tests/indexing/test_check_indexer.py @@ -26,8 +26,8 @@ ], ) def test_valid_input(indexer, expected): - array = np.array([1, 2, 3]) - result = check_array_indexer(array, indexer) + arr = np.array([1, 2, 3]) + result = check_array_indexer(arr, indexer) tm.assert_numpy_array_equal(result, expected) @@ -53,22 +53,22 @@ def test_boolean_na_returns_indexer(indexer): ], ) def test_bool_raise_length(indexer): - array = np.array([1, 2, 3]) + arr = np.array([1, 2, 3]) msg = "Boolean index has wrong length" with pytest.raises(IndexError, match=msg): - check_array_indexer(array, indexer) + check_array_indexer(arr, indexer) @pytest.mark.parametrize( "indexer", [[0, 1, None], pd.array([0, 1, pd.NA], dtype="Int64")] ) def test_int_raise_missing_values(indexer): - array = np.array([1, 2, 3]) + arr = np.array([1, 2, 3]) msg = "Cannot index with an integer indexer containing NA values" with pytest.raises(ValueError, match=msg): - check_array_indexer(array, indexer) + check_array_indexer(arr, indexer) @pytest.mark.parametrize( @@ -82,16 +82,16 @@ def test_int_raise_missing_values(indexer): ], ) def test_raise_invalid_array_dtypes(indexer): - array = np.array([1, 2, 3]) + arr = np.array([1, 2, 3]) msg = "arrays used as indices must be of integer or boolean type" with pytest.raises(IndexError, match=msg): - check_array_indexer(array, indexer) + check_array_indexer(arr, indexer) @pytest.mark.parametrize("indexer", [None, Ellipsis, slice(0, 3), (None,)]) def test_pass_through_non_array_likes(indexer): - array = np.array([1, 2, 3]) + arr = np.array([1, 2, 3]) - result = check_array_indexer(array, indexer) + result = check_array_indexer(arr, indexer) assert result == indexer diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 9dbce283d2a8f..1d5886085a5a5 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -525,10 +525,10 @@ def test_loc_setitem_consistency_slice_column_len(self): Region_1,Site_2,3977723089,A,5/20/2015 8:33,5/20/2015 9:09,Yes,No""" df = pd.read_csv(StringIO(data), header=[0, 1], index_col=[0, 1, 2]) - df.loc[:, ("Respondent", "StartDate")] = pd.to_datetime( + df.loc[:, ("Respondent", "StartDate")] = to_datetime( df.loc[:, ("Respondent", "StartDate")] ) - df.loc[:, ("Respondent", "EndDate")] = pd.to_datetime( + df.loc[:, ("Respondent", "EndDate")] = to_datetime( df.loc[:, ("Respondent", "EndDate")] ) df.loc[:, ("Respondent", "Duration")] = ( @@ -568,7 +568,7 @@ def test_loc_modify_datetime(self): {"date": [1485264372711, 1485265925110, 1540215845888, 1540282121025]} ) - df["date_dt"] = pd.to_datetime(df["date"], unit="ms", cache=True) + df["date_dt"] = to_datetime(df["date"], unit="ms", cache=True) df.loc[:, "date_dt_cp"] = df.loc[:, "date_dt"] df.loc[[2, 3], "date_dt_cp"] = df.loc[[2, 3], "date_dt"] @@ -584,7 +584,7 @@ def test_loc_modify_datetime(self): ) columns = ["date_dt", "date_dt_cp"] - expected[columns] = expected[columns].apply(pd.to_datetime) + expected[columns] = expected[columns].apply(to_datetime) tm.assert_frame_equal(df, expected) @@ -808,8 +808,8 @@ def test_loc_coercion(self): def test_setitem_new_key_tz(self, indexer_sl): # GH#12862 should not raise on assigning the second value vals = [ - pd.to_datetime(42).tz_localize("UTC"), - pd.to_datetime(666).tz_localize("UTC"), + to_datetime(42).tz_localize("UTC"), + to_datetime(666).tz_localize("UTC"), ] expected = Series(vals, index=["foo", "bar"]) @@ -1458,7 +1458,7 @@ def test_loc_getitem_access_none_value_in_multiindex(self): # GH#34318: test that you can access a None value using .loc # through a Multiindex - ser = Series([None], pd.MultiIndex.from_arrays([["Level1"], ["Level2"]])) + ser = Series([None], MultiIndex.from_arrays([["Level1"], ["Level2"]])) result = ser.loc[("Level1", "Level2")] assert result is None @@ -1474,7 +1474,7 @@ def test_loc_getitem_access_none_value_in_multiindex(self): def test_loc_setitem_multiindex_slice(self): # GH 34870 - index = pd.MultiIndex.from_tuples( + index = MultiIndex.from_tuples( zip( ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"], ["one", "two", "one", "two", "one", "two", "one", "two"], @@ -1629,14 +1629,14 @@ def test_loc_setitem_with_expansion_and_existing_dst(self): start = Timestamp("2017-10-29 00:00:00+0200", tz="Europe/Madrid") end = Timestamp("2017-10-29 03:00:00+0100", tz="Europe/Madrid") ts = Timestamp("2016-10-10 03:00:00", tz="Europe/Madrid") - idx = pd.date_range(start, end, closed="left", freq="H") + idx = date_range(start, end, closed="left", freq="H") assert ts not in idx # i.e. result.loc setitem is with-expansion result = DataFrame(index=idx, columns=["value"]) result.loc[ts, "value"] = 12 expected = DataFrame( [np.nan] * len(idx) + [12], - index=idx.append(pd.DatetimeIndex([ts])), + index=idx.append(DatetimeIndex([ts])), columns=["value"], dtype=object, ) @@ -1645,7 +1645,7 @@ def test_loc_setitem_with_expansion_and_existing_dst(self): def test_setitem_with_expansion(self): # indexing - setting an element df = DataFrame( - data=pd.to_datetime(["2015-03-30 20:12:32", "2015-03-12 00:11:11"]), + data=to_datetime(["2015-03-30 20:12:32", "2015-03-12 00:11:11"]), columns=["time"], ) df["new_col"] = ["new", "old"] @@ -1660,7 +1660,7 @@ def test_setitem_with_expansion(self): expected = Series([v[0], df.loc[1, "time"]], name="time") tm.assert_series_equal(df2.time, expected) - v = df.loc[df.new_col == "new", "time"] + pd.Timedelta("1s") + v = df.loc[df.new_col == "new", "time"] + Timedelta("1s") df.loc[df.new_col == "new", "time"] = v tm.assert_series_equal(df.loc[df.new_col == "new", "time"], v) @@ -2296,15 +2296,13 @@ def test_loc_axis_1_slice(): df = DataFrame( np.ones((10, 8)), index=tuple("ABCDEFGHIJ"), - columns=pd.MultiIndex.from_tuples(cols), + columns=MultiIndex.from_tuples(cols), ) result = df.loc(axis=1)[(2014, 9):(2015, 8)] expected = DataFrame( np.ones((10, 4)), index=tuple("ABCDEFGHIJ"), - columns=pd.MultiIndex.from_tuples( - [(2014, 9), (2014, 10), (2015, 7), (2015, 8)] - ), + columns=MultiIndex.from_tuples([(2014, 9), (2014, 10), (2015, 7), (2015, 8)]), ) tm.assert_frame_equal(result, expected) @@ -2312,7 +2310,7 @@ def test_loc_axis_1_slice(): def test_loc_set_dataframe_multiindex(): # GH 14592 expected = DataFrame( - "a", index=range(2), columns=pd.MultiIndex.from_product([range(2), range(2)]) + "a", index=range(2), columns=MultiIndex.from_product([range(2), range(2)]) ) result = expected.copy() result.loc[0, [(0, 1)]] = result.loc[0, [(0, 1)]] @@ -2340,7 +2338,7 @@ def test_loc_with_positional_slice_deprecation(): def test_loc_slice_disallows_positional(): # GH#16121, GH#24612, GH#31810 - dti = pd.date_range("2016-01-01", periods=3) + dti = date_range("2016-01-01", periods=3) df = DataFrame(np.random.random((3, 2)), index=dti) ser = df[0] @@ -2372,7 +2370,7 @@ def test_loc_datetimelike_mismatched_dtypes(): df = DataFrame( np.random.randn(5, 3), columns=["a", "b", "c"], - index=pd.date_range("2012", freq="H", periods=5), + index=date_range("2012", freq="H", periods=5), ) # create dataframe with non-unique DatetimeIndex df = df.iloc[[0, 2, 2, 3]].copy() diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index ca453b55eae2e..977b92e217868 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -84,13 +84,13 @@ def test_copy(self, do_changes, do_render): self.styler.set_table_attributes('class="foo" data-bar') self.styler.hidden_index = not self.styler.hidden_index self.styler.hide_columns("A") - classes = pd.DataFrame( + classes = DataFrame( [["favorite-val red", ""], [None, "blue my-val"]], index=self.df.index, columns=self.df.columns, ) self.styler.set_td_classes(classes) - ttips = pd.DataFrame( + ttips = DataFrame( data=[["Favorite", ""], [np.nan, "my"]], columns=self.df.columns, index=self.df.index, diff --git a/pandas/tests/series/test_ufunc.py b/pandas/tests/series/test_ufunc.py index a5aa319e4772f..15b2ff36cff1e 100644 --- a/pandas/tests/series/test_ufunc.py +++ b/pandas/tests/series/test_ufunc.py @@ -31,17 +31,17 @@ def arrays_for_binary_ufunc(): @pytest.mark.parametrize("sparse", SPARSE, ids=SPARSE_IDS) def test_unary_ufunc(ufunc, sparse): # Test that ufunc(pd.Series) == pd.Series(ufunc) - array = np.random.randint(0, 10, 10, dtype="int64") - array[::2] = 0 + arr = np.random.randint(0, 10, 10, dtype="int64") + arr[::2] = 0 if sparse: - array = SparseArray(array, dtype=pd.SparseDtype("int64", 0)) + arr = SparseArray(arr, dtype=pd.SparseDtype("int64", 0)) index = list(string.ascii_letters[:10]) name = "name" - series = pd.Series(array, index=index, name=name) + series = pd.Series(arr, index=index, name=name) result = ufunc(series) - expected = pd.Series(ufunc(array), index=index, name=name) + expected = pd.Series(ufunc(arr), index=index, name=name) tm.assert_series_equal(result, expected) @@ -148,14 +148,14 @@ def test_binary_ufunc_scalar(ufunc, sparse, flip, arrays_for_binary_ufunc): # Test that # * ufunc(pd.Series, scalar) == pd.Series(ufunc(array, scalar)) # * ufunc(pd.Series, scalar) == ufunc(scalar, pd.Series) - array, _ = arrays_for_binary_ufunc + arr, _ = arrays_for_binary_ufunc if sparse: - array = SparseArray(array) + arr = SparseArray(arr) other = 2 - series = pd.Series(array, name="name") + series = pd.Series(arr, name="name") series_args = (series, other) - array_args = (array, other) + array_args = (arr, other) if flip: series_args = tuple(reversed(series_args)) @@ -207,14 +207,14 @@ def test_multiple_output_binary_ufuncs(ufunc, sparse, shuffle, arrays_for_binary def test_multiple_output_ufunc(sparse, arrays_for_binary_ufunc): # Test that the same conditions from unary input apply to multi-output # ufuncs - array, _ = arrays_for_binary_ufunc + arr, _ = arrays_for_binary_ufunc if sparse: - array = SparseArray(array) + arr = SparseArray(arr) - series = pd.Series(array, name="name") + series = pd.Series(arr, name="name") result = np.modf(series) - expected = np.modf(array) + expected = np.modf(arr) assert isinstance(result, tuple) assert isinstance(expected, tuple) diff --git a/pandas/tests/strings/test_cat.py b/pandas/tests/strings/test_cat.py index cdaccf0dad8e6..68aa6b3c4a136 100644 --- a/pandas/tests/strings/test_cat.py +++ b/pandas/tests/strings/test_cat.py @@ -1,7 +1,6 @@ import numpy as np import pytest -import pandas as pd from pandas import ( DataFrame, Index, @@ -366,7 +365,7 @@ def test_cat_on_filtered_index(): assert str_multiple.loc[1] == "2011 2 2" -@pytest.mark.parametrize("klass", [tuple, list, np.array, pd.Series, pd.Index]) +@pytest.mark.parametrize("klass", [tuple, list, np.array, Series, Index]) def test_cat_different_classes(klass): # https://github.com/pandas-dev/pandas/issues/33425 s = Series(["a", "b", "c"]) diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py index 26d336bee65ea..876df69ae7f63 100644 --- a/pandas/tests/test_algos.py +++ b/pandas/tests/test_algos.py @@ -1478,7 +1478,7 @@ def test_unique_index(self): ) @pytest.mark.parametrize( - "arr, unique", + "arr, uniques", [ ( [(0, 0), (0, 1), (1, 0), (1, 1), (0, 0), (0, 1), (1, 0), (1, 1)], @@ -1491,10 +1491,10 @@ def test_unique_index(self): ([("a", 1), ("b", 2), ("a", 3), ("a", 1)], [("a", 1), ("b", 2), ("a", 3)]), ], ) - def test_unique_tuples(self, arr, unique): + def test_unique_tuples(self, arr, uniques): # https://github.com/pandas-dev/pandas/issues/16519 - expected = np.empty(len(unique), dtype=object) - expected[:] = unique + expected = np.empty(len(uniques), dtype=object) + expected[:] = uniques result = pd.unique(arr) tm.assert_numpy_array_equal(result, expected)