Skip to content

REF get-axis-number #43263

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

Closed
Closed
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
1 change: 0 additions & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import warnings
import weakref


import numpy as np

from pandas._config import config
Expand Down
9 changes: 1 addition & 8 deletions pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)),
Expand Down
5 changes: 3 additions & 2 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Sequence,
)
import warnings
from pandas import DataFrame

import numpy as np

Expand Down Expand Up @@ -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"]:
if DataFrame()._get_axis_number(axis)==0:
result = data.apply(func, axis=0, **kwargs)
elif axis in [1, "columns"]:
if DataFrame()._get_axis_number(axis)==1:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't make changes to style.py this already has PRs fixing this. please revert.

Copy link
Author

@ForthenchoPacino ForthenchoPacino Aug 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aight. on it
just to confirm, though, were my changes correct? or were they unnecessary?

result = data.T.apply(func, axis=0, **kwargs).T # see GH 42005
else:
result = func(data, **kwargs)
Expand Down
16 changes: 8 additions & 8 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issue has been revised to avoid fixing test files. Too many new contributors were making mistakes by changing the nature of what was being tested.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, no touching test files, then?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont edit test files.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aight. Won't. Thanks for telling. I'll revert the changes


with np.errstate(all="ignore"):

Expand All @@ -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"]])
Expand All @@ -1021,11 +1022,10 @@ 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"]]
)
Expand Down Expand Up @@ -1102,7 +1102,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
Expand All @@ -1115,7 +1115,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)
Expand All @@ -1141,7 +1141,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
Expand All @@ -1166,7 +1166,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)


Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/apply/test_frame_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/frame/methods/test_at_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/frame/methods/test_between_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 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 axis in ["columns", 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
Expand All @@ -183,14 +183,14 @@ 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 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)

if axis in ["index", 0]:
if DataFrame()._get_axis_number(axis)==0:
ts.columns = mask
with pytest.raises(TypeError, match=msg):
ts.between_time(stime, etime, axis=1)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/methods/test_set_axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can do this here, but use existing objects such as df._get_axis_number(axis) == 1. Also note you have got the 0 and 1 wrong here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. Thanks for point that out, I'll correct it and re-commit

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)}"
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

axis has already been reduced to an integer in line 489. Sufficient for if axis == 0

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. So, has this specific edit been rendered unnecessary?

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]})
Expand Down