-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Convert DataFrame.rename to keyword only; simplify axis validation #29140
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
Changes from 1 commit
f09c6df
66acc90
177a440
b66e9e2
2415e46
296a9df
6ba8518
4128780
2776c7d
8e20fa4
cca8eed
1f8e1cf
4dc8bbe
1b261f8
b5e54bd
ab1ee2e
af0b7c5
d5d812c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1294,7 +1294,7 @@ def test_rename_mapper_multi(self): | |
def test_rename_positional_named(self): | ||
# https://github.com/pandas-dev/pandas/issues/12392 | ||
df = DataFrame({"a": [1, 2], "b": [1, 2]}, index=["X", "Y"]) | ||
result = df.rename(str.lower, columns=str.upper) | ||
result = df.rename(index=str.lower, columns=str.upper) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So did this break? If so, is it solely because of the keyword only change to If so, I might prefer a proper deprecation warning for this change... But could be convinced otherwise, since I'd consider the old code bad style. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be related to https://github.com/pandas-dev/pandas/pull/29140/files#r337242915 This is a little tough to support without keeping the ambiguity, and it doesn't really work outside of this one niche application. For instance, if you tried to apply >>> df.rename(str.lower, index=str.upper, axis=1)
TypeError: Cannot specify both 'axis' and any of 'index' or 'columns'. Even explicitly stating the axis in this case would raise an error: >>> df.rename(str.lower, columns=str.upper, axis=0)
TypeError: Cannot specify both 'axis' and any of 'index' or 'columns'. So we could still support this but it would take some gymnastics and not sure it makes things clearer, though open to the thoughts of others. Maybe can be clarified further in the whatsnew to mitigate confusion? |
||
expected = DataFrame({"A": [1, 2], "B": [1, 2]}, index=["x", "y"]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
@@ -1318,12 +1318,12 @@ def test_rename_axis_style_raises(self): | |
|
||
# Multiple targets and axis | ||
with pytest.raises(TypeError, match=over_spec_msg): | ||
df.rename(str.lower, str.lower, axis="columns") | ||
df.rename(str.lower, index=str.lower, axis="columns") | ||
|
||
# Too many targets | ||
over_spec_msg = "Cannot specify all of 'mapper', 'index', 'columns'." | ||
over_spec_msg = "Cannot specify both 'mapper' and any of 'index' or 'columns'" | ||
with pytest.raises(TypeError, match=over_spec_msg): | ||
df.rename(str.lower, str.lower, str.lower) | ||
df.rename(str.lower, index=str.lower, columns=str.lower) | ||
|
||
# Duplicates | ||
with pytest.raises(TypeError, match="multiple values"): | ||
|
@@ -1357,16 +1357,11 @@ def test_reindex_api_equivalence(self): | |
for res in [res2, res3]: | ||
tm.assert_frame_equal(res1, res) | ||
|
||
def test_rename_positional(self): | ||
def test_rename_positional_raises(self): | ||
df = DataFrame(columns=["A", "B"]) | ||
with tm.assert_produces_warning(FutureWarning) as rec: | ||
result = df.rename(None, str.lower) | ||
expected = DataFrame(columns=["a", "b"]) | ||
tm.assert_frame_equal(result, expected) | ||
assert len(rec) == 1 | ||
message = str(rec[0].message) | ||
assert "rename" in message | ||
assert "Use named arguments" in message | ||
msg = r"rename\(\) takes from 1 to 2 positional arguments" | ||
with pytest.raises(TypeError, match=msg): | ||
df.rename(None, str.lower) | ||
|
||
def test_assign_columns(self, float_frame): | ||
float_frame["hi"] = "there" | ||
|
@@ -1391,14 +1386,6 @@ def test_set_index_preserve_categorical_dtype(self): | |
result = result.reindex(columns=df.columns) | ||
tm.assert_frame_equal(result, df) | ||
|
||
def test_ambiguous_warns(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These use cases are now covered by the |
||
df = DataFrame({"A": [1, 2]}) | ||
with tm.assert_produces_warning(FutureWarning): | ||
df.rename(id, id) | ||
|
||
with tm.assert_produces_warning(FutureWarning): | ||
df.rename({0: 10}, {"A": "B"}) | ||
|
||
def test_rename_signature(self): | ||
sig = inspect.signature(DataFrame.rename) | ||
parameters = set(sig.parameters) | ||
|
Uh oh!
There was an error while loading. Please reload this page.