From 9adc255c0ff0b9ebf702c4f913fc2a7667d3c821 Mon Sep 17 00:00:00 2001 From: ForthenchoPacino Date: Sat, 28 Aug 2021 02:06:20 +0530 Subject: [PATCH 1/2] my fixes to the code --- pandas/core/generic.py | 1 + pandas/io/formats/style.py | 7 ++++--- pandas/tests/apply/test_frame_apply.py | 17 +++++++++-------- pandas/tests/apply/test_frame_transform.py | 4 ++-- pandas/tests/frame/methods/test_at_time.py | 4 ++-- pandas/tests/frame/methods/test_between_time.py | 9 +++++---- pandas/tests/frame/methods/test_set_axis.py | 2 +- pandas/tests/groupby/test_grouping.py | 2 +- pandas/tests/window/test_rolling.py | 2 +- 9 files changed, 26 insertions(+), 22 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 36c0d024369e6..5188add47ef49 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -23,6 +23,7 @@ ) import warnings import weakref +from pandas import DataFrame import numpy as np diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 81bd14629cfd3..575901ca9f8cd 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -14,6 +14,7 @@ Sequence, ) import warnings +from pandas import DataFrame import numpy as np @@ -1148,9 +1149,9 @@ def _apply( subset = slice(None) if subset is None else subset subset = non_reducing_slice(subset) data = self.data.loc[subset] - if axis in [0, "index"]: - result = data.apply(func, axis=0, **kwargs) - elif axis in [1, "columns"]: + if DataFrame()._get_axis_number(axis)==0: + result = data.apply(func, axis=0, **kwargs) + if DataFrame()._get_axis_number(axis)==1: result = data.T.apply(func, axis=0, **kwargs).T # see GH 42005 else: result = func(data, **kwargs) diff --git a/pandas/tests/apply/test_frame_apply.py b/pandas/tests/apply/test_frame_apply.py index 62983b5327a26..e4589c4464a40 100644 --- a/pandas/tests/apply/test_frame_apply.py +++ b/pandas/tests/apply/test_frame_apply.py @@ -995,7 +995,7 @@ def test_consistency_for_boxed(box, int_frame_const_col): def test_agg_transform(axis, float_frame): - other_axis = 1 if axis in {0, "index"} else 0 + other_axis = 1 if DataFrame()._get_axis_number(axis)==0 else 0 with np.errstate(all="ignore"): @@ -1010,7 +1010,8 @@ def test_agg_transform(axis, float_frame): # list-like result = float_frame.apply([np.sqrt], axis=axis) expected = f_sqrt.copy() - if axis in {0, "index"}: + from pandas import DataFrame + if DataFrame()._get_axis_number(axis)==0: expected.columns = MultiIndex.from_product([float_frame.columns, ["sqrt"]]) else: expected.index = MultiIndex.from_product([float_frame.index, ["sqrt"]]) @@ -1021,11 +1022,11 @@ def test_agg_transform(axis, float_frame): # functions per series and then concatting result = float_frame.apply([np.abs, np.sqrt], axis=axis) expected = zip_frames([f_abs, f_sqrt], axis=other_axis) - if axis in {0, "index"}: + if DataFrame()._get_axis_number(axis)==0: expected.columns = MultiIndex.from_product( [float_frame.columns, ["absolute", "sqrt"]] ) - else: + expected.index = MultiIndex.from_product( [float_frame.index, ["absolute", "sqrt"]] ) @@ -1102,7 +1103,7 @@ def test_agg_multiple_mixed_no_warning(): def test_agg_reduce(axis, float_frame): - other_axis = 1 if axis in {0, "index"} else 0 + other_axis = 1 if DataFrame()._get_axis_number(axis)==0 else 0 name1, name2 = float_frame.axes[other_axis].unique()[:2].sort_values() # all reducers @@ -1115,7 +1116,7 @@ def test_agg_reduce(axis, float_frame): axis=1, ) expected.columns = ["mean", "max", "sum"] - expected = expected.T if axis in {0, "index"} else expected + expected = expected.T if DataFrame()._get_axis_number(axis)==0 else expected result = float_frame.agg(["mean", "max", "sum"], axis=axis) tm.assert_frame_equal(result, expected) @@ -1141,7 +1142,7 @@ def test_agg_reduce(axis, float_frame): name2: Series([float_frame.loc(other_axis)[name2].sum()], index=["sum"]), } ) - expected = expected.T if axis in {1, "columns"} else expected + expected = expected.T if DataFrame()._get_axis_number(axis)==1 else expected tm.assert_frame_equal(result, expected) # dict input with lists with multiple @@ -1166,7 +1167,7 @@ def test_agg_reduce(axis, float_frame): }, axis=1, ) - expected = expected.T if axis in {1, "columns"} else expected + expected = expected.T if DataFrame()._get_axis_number(axis)==1 else expected tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/apply/test_frame_transform.py b/pandas/tests/apply/test_frame_transform.py index 47173d14c543d..bf6234d5b06f2 100644 --- a/pandas/tests/apply/test_frame_transform.py +++ b/pandas/tests/apply/test_frame_transform.py @@ -47,10 +47,10 @@ def test_transform_ufunc(axis, float_frame, frame_or_series): ) def test_transform_listlike(axis, float_frame, ops, names): # GH 35964 - other_axis = 1 if axis in {0, "index"} else 0 + other_axis = 1 if DataFrame()._get_axis_number(axis)==0 else 0 with np.errstate(all="ignore"): expected = zip_frames([op(float_frame) for op in ops], axis=other_axis) - if axis in {0, "index"}: + if DataFrame()._get_axis_number(axis)==0: expected.columns = MultiIndex.from_product([float_frame.columns, names]) else: expected.index = MultiIndex.from_product([float_frame.index, names]) diff --git a/pandas/tests/frame/methods/test_at_time.py b/pandas/tests/frame/methods/test_at_time.py index 2d05176d20f5f..a53fc553c02a6 100644 --- a/pandas/tests/frame/methods/test_at_time.py +++ b/pandas/tests/frame/methods/test_at_time.py @@ -102,9 +102,9 @@ def test_at_time_axis(self, axis): indices = rng[(rng.hour == 9) & (rng.minute == 30) & (rng.second == 0)] - if axis in ["index", 0]: + if DataFrame()._get_axis_number(axis)==0: expected = ts.loc[indices, :] - elif axis in ["columns", 1]: + if DataFrame()._get_axis_number(axis)==1: expected = ts.loc[:, indices] result = ts.at_time("9:30", axis=axis) diff --git a/pandas/tests/frame/methods/test_between_time.py b/pandas/tests/frame/methods/test_between_time.py index 0daa267767269..f0dbbebeb2ee2 100644 --- a/pandas/tests/frame/methods/test_between_time.py +++ b/pandas/tests/frame/methods/test_between_time.py @@ -164,12 +164,12 @@ def test_between_time_axis_aliases(self, axis): stime, etime = ("08:00:00", "09:00:00") exp_len = 7 - if axis in ["index", 0]: + if self.get_axis_number(axis)==0: ts.index = rng assert len(ts.between_time(stime, etime)) == exp_len assert len(ts.between_time(stime, etime, axis=0)) == exp_len - if axis in ["columns", 1]: + if self.get_axis_number(axis)==1: ts.columns = rng selected = ts.between_time(stime, etime, axis=1).columns assert len(selected) == exp_len @@ -183,14 +183,15 @@ def test_between_time_axis_raises(self, axis): stime, etime = ("08:00:00", "09:00:00") msg = "Index must be DatetimeIndex" - if axis in ["columns", 1]: + if self.get_axis_number(axis)==1: ts.index = mask with pytest.raises(TypeError, match=msg): ts.between_time(stime, etime) with pytest.raises(TypeError, match=msg): ts.between_time(stime, etime, axis=0) - if axis in ["index", 0]: + from pandas import DataFrame; + if DataFrame()._get_axis_number(axis)==0: ts.columns = mask with pytest.raises(TypeError, match=msg): ts.between_time(stime, etime, axis=1) diff --git a/pandas/tests/frame/methods/test_set_axis.py b/pandas/tests/frame/methods/test_set_axis.py index 3284243ddac48..00a4239bc7b38 100644 --- a/pandas/tests/frame/methods/test_set_axis.py +++ b/pandas/tests/frame/methods/test_set_axis.py @@ -34,7 +34,7 @@ def test_set_axis_inplace_axis(self, axis, obj): new_index = list("abcd")[: len(obj)] expected = obj.copy() - if axis in [0, "index"]: + if DataFrame()._get_axis_number(axis)==0: expected.index = new_index else: expected.columns = new_index diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py index 3d02e784d83b0..82a49cf945858 100644 --- a/pandas/tests/groupby/test_grouping.py +++ b/pandas/tests/groupby/test_grouping.py @@ -547,7 +547,7 @@ def test_groupby_level_index_names(self, axis): df = DataFrame({"exp": ["A"] * 3 + ["B"] * 3, "var1": range(6)}).set_index( "exp" ) - if axis in (1, "columns"): + if DataFrame()._get_axis_number(axis)==0: df = df.T df.groupby(level="exp", axis=axis) msg = f"level name foo is not the name of the {df._get_axis_name(axis)}" diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py index 2edf22d96a9ba..0c637f182704e 100644 --- a/pandas/tests/window/test_rolling.py +++ b/pandas/tests/window/test_rolling.py @@ -488,7 +488,7 @@ def test_rolling_axis_count(axis_frame): axis = df._get_axis_number(axis_frame) - if axis in [0, "index"]: + if DataFrame()._get_axis_number(axis)==0: expected = DataFrame({"x": [1.0, 2.0, 2.0], "y": [1.0, 2.0, 2.0]}) else: expected = DataFrame({"x": [1.0, 1.0, 1.0], "y": [2.0, 2.0, 2.0]}) From d00d22169aa49efb51e32809bdc6e5bb12f9f8e4 Mon Sep 17 00:00:00 2001 From: ForthenchoPacino Date: Sat, 28 Aug 2021 10:10:04 +0530 Subject: [PATCH 2/2] checked the extra dataframe imports. hopefully, its fixed now --- pandas/core/arrays/datetimes.py | 1 - pandas/core/generic.py | 2 +- pandas/core/reshape/pivot.py | 9 +-------- pandas/io/formats/style.py | 2 +- pandas/tests/apply/test_frame_apply.py | 1 - pandas/tests/frame/methods/test_between_time.py | 7 +++---- 6 files changed, 6 insertions(+), 16 deletions(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 8513bbb044e83..3c3a18afd99f6 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -1329,7 +1329,6 @@ def isocalendar(self) -> DataFrame: 2020-01-01 1 Freq: D, Name: week, dtype: UInt32 """ - from pandas import DataFrame values = self._local_timestamps() sarray = fields.build_isocalendar_sarray(values) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 5188add47ef49..ec61c0f8beb51 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -23,7 +23,7 @@ ) import warnings import weakref -from pandas import DataFrame + import numpy as np diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index fcf00276aa8af..95ea5925f073d 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -314,9 +314,7 @@ def _add_margins( row_margin[k] = grand_margin[k] else: row_margin[k] = grand_margin[k[0]] - - from pandas import DataFrame - + margin_dummy = DataFrame(row_margin, columns=[key]).T row_names = result.index.names @@ -379,8 +377,6 @@ def _all_key(key): table_pieces.append(piece) margin_keys.append(all_key) else: - from pandas import DataFrame - cat_axis = 0 for key, piece in table.groupby(level=0, axis=cat_axis, observed=observed): if len(cols) > 1: @@ -643,9 +639,6 @@ def crosstab( colnames_mapper, unique_colnames, ) = _build_names_mapper(rownames, colnames) - - from pandas import DataFrame - data = { **dict(zip(unique_rownames, index)), **dict(zip(unique_colnames, columns)), diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 575901ca9f8cd..d0631c33276a7 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1150,7 +1150,7 @@ def _apply( subset = non_reducing_slice(subset) data = self.data.loc[subset] if DataFrame()._get_axis_number(axis)==0: - result = data.apply(func, axis=0, **kwargs) + result = data.apply(func, axis=0, **kwargs) if DataFrame()._get_axis_number(axis)==1: result = data.T.apply(func, axis=0, **kwargs).T # see GH 42005 else: diff --git a/pandas/tests/apply/test_frame_apply.py b/pandas/tests/apply/test_frame_apply.py index e4589c4464a40..11d63d6c30a5d 100644 --- a/pandas/tests/apply/test_frame_apply.py +++ b/pandas/tests/apply/test_frame_apply.py @@ -1026,7 +1026,6 @@ def test_agg_transform(axis, float_frame): expected.columns = MultiIndex.from_product( [float_frame.columns, ["absolute", "sqrt"]] ) - expected.index = MultiIndex.from_product( [float_frame.index, ["absolute", "sqrt"]] ) diff --git a/pandas/tests/frame/methods/test_between_time.py b/pandas/tests/frame/methods/test_between_time.py index f0dbbebeb2ee2..d1c3ce5be02f6 100644 --- a/pandas/tests/frame/methods/test_between_time.py +++ b/pandas/tests/frame/methods/test_between_time.py @@ -164,12 +164,12 @@ def test_between_time_axis_aliases(self, axis): stime, etime = ("08:00:00", "09:00:00") exp_len = 7 - if self.get_axis_number(axis)==0: + if DataFrame()._get_axis_number(axis)==0: ts.index = rng assert len(ts.between_time(stime, etime)) == exp_len assert len(ts.between_time(stime, etime, axis=0)) == exp_len - if self.get_axis_number(axis)==1: + if DataFrame()._get_axis_number(axis)==1: ts.columns = rng selected = ts.between_time(stime, etime, axis=1).columns assert len(selected) == exp_len @@ -183,14 +183,13 @@ def test_between_time_axis_raises(self, axis): stime, etime = ("08:00:00", "09:00:00") msg = "Index must be DatetimeIndex" - if self.get_axis_number(axis)==1: + if DataFrame()._get_axis_number(axis)==1: ts.index = mask with pytest.raises(TypeError, match=msg): ts.between_time(stime, etime) with pytest.raises(TypeError, match=msg): ts.between_time(stime, etime, axis=0) - from pandas import DataFrame; if DataFrame()._get_axis_number(axis)==0: ts.columns = mask with pytest.raises(TypeError, match=msg):