-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
DEPR: deprecate .add_prefix and .add_suffix #18347
Conversation
Codecov Report
@@ Coverage Diff @@
## master #18347 +/- ##
==========================================
- Coverage 91.59% 91.57% -0.03%
==========================================
Files 150 150
Lines 48959 48961 +2
==========================================
- Hits 44843 44835 -8
- Misses 4116 4126 +10
Continue to review full report at Codecov.
|
dbc445e
to
b125c7b
Compare
59ff13b
to
eff7b11
Compare
pandas/tests/frame/test_api.py
Outdated
with_pct_suffix = self.frame.add_suffix('%') | ||
expected = pd.Index(['{}%'.format(c) for c in self.frame.columns]) | ||
tm.assert_index_equal(with_pct_suffix.columns, expected) | ||
with pytest.warns(FutureWarning): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jreback @jorisvandenbossche : Are we still going with tm.assert_produces_warning
, or is using the pytest
construct okay to use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes I prefer to use our tm.assert_produces_warning
for now (if we change then we should simply globally change, but in a separate set of PRs)
pandas/tests/frame/test_api.py
Outdated
with_pct_suffix = self.frame.add_suffix('%') | ||
expected = pd.Index(['{}%'.format(c) for c in self.frame.columns]) | ||
tm.assert_index_equal(with_pct_suffix.columns, expected) | ||
with pytest.warns(FutureWarning): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes I prefer to use our tm.assert_produces_warning
for now (if we change then we should simply globally change, but in a separate set of PRs)
0 1 4 | ||
1 2 5 | ||
2 3 6 | ||
>>> df.rename(index="index_{}".format, columns={"A": "a", "B": "c"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any use of .add_prefix/suffix
in the main docs? if so can you update
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's not used in the docs. It's shown in 10min_to_pandas
as a result of tab completion, but that should stay until removal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no please change the docs now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I miswrote: it's not in the docs, expect as a result of that tab comletion example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh these need to be added to
_deprecations in series and dataframe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, added.
@jorisvandenbossche you had some concerns here? |
79ab0aa
to
db08605
Compare
pandas/core/frame.py
Outdated
@@ -304,7 +304,8 @@ def _constructor(self): | |||
|
|||
_constructor_sliced = Series | |||
_deprecations = NDFrame._deprecations | frozenset( | |||
['sortlevel', 'get_value', 'set_value', 'from_csv']) | |||
['sortlevel', 'get_value', 'set_value', 'from_csv', 'add_prefix', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead add to NDFrame._deprecations (which are both used in Series/Frame)
Yes, as I mentioned in the other issue, not sure if we should deprecate this one. Based on stackoverflow questions (and doc page views) this is actually used a bit (in contrast to eg asobject) |
where do you see doc page views? |
db08605
to
3997642
Compare
They are used fairly often on stack overflow. I use them. You can see about 20 posts with them this month on stack overflow. That said, personally, I'd like to shrink pandas as much as possible and wouldn't mind seeing them gone. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to give feedback on how you did the deprecation: added two small comments, but it's certainly good!
(so go ahead with some less controversial deprecations :-))
That said, I am still in doubt we should do this one.
pandas/core/generic.py
Outdated
""" | ||
warnings.warn("'add_prefix' is deprecated and will be removed in a " | ||
"future version.", FutureWarning, stacklevel=2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mention the alternative in the message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
pandas/core/generic.py
Outdated
@@ -2590,6 +2595,8 @@ def _update_inplace(self, result, verify_is_copy=True): | |||
|
|||
def add_prefix(self, prefix): | |||
""" | |||
DEPRECATED: Use ``obj.rename('prefix_{}'.format)`` or similar instead. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit in doubt whether I prefer this alternative or rather obj.rename(lambda x: "prefix_" + x)
or obj.rename(lambda x: 'prefix_{}'.format(x))
(it's just that I think such a bare format is not that easy to understand for beginners that it is a function, but of course lambda's are also a strange beast)
BTW, you need to add columns=
to have the correct alternative I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree here, using the lambda is a bit simpler for beginners.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, done.
@jorisvandenbossche: Ok, I can see that pople use them, but they're just so useless. There was at one point a discussion on adding a >>> df.columns = df.columns.format("prefix_{}_suffix")
>>> df.columns.format("prefix_{}_suffix", inplace=True) # equivalent, but inplace This would be much more generally useful than add_prefix/-suffix, and would operate on the relevant object, which IMO is often more readable than methods on the dataframe. Just an idea, as I could often use such methods and |
3997642
to
1e83d89
Compare
We actually have this almost already
wouldn't be a stretch to allow a single element here, e.g. these are much more appealing that a method on Series/DataFrame |
I agree, though two points:
So, I'd be the most positive to adding a |
This is by-definition and on-purpose. string methods should not work on non-strings, full-stop. We also want composability. So and I think there is an issue about this somewhere, having a generic |
I posted an issue #17211 on this. |
As I said in the issue some months ago, I am +1 on adding a |
1e83d89
to
e797713
Compare
Hello @topper-123! Thanks for updating the PR. Cheers ! There are no PEP8 issues in this Pull Request. 🍻 Comment last updated on December 24, 2017 at 10:45 Hours UTC |
e797713
to
3a5785a
Compare
ok, so I think we need to add a |
closing, we had a discussion about this, it is a bit premature to do this as these are useful. so alternative is to add a |
Should this be removed from the checklist in #6581? |
git diff upstream/master -u -- "*.py" | flake8 --diff
Deprecate
.add_prefix
and.add_suffix
as per #18262.An issue is that
.add_prefix
and.add_suffix
call the like-named methods ininternals.py::BlockManager
. ShouldBlockManager.add_prefix/add_suffix
be changed somehow, e.g. have a warning as well?EDIT: This is my first deprecation PR, so I'll be more than appreciative of criticism, so I understand these better in the future.
EDIT 2: With
.add_prefix
/add_postfix
deprecated, I think it would be good to show in the doc strings for.rename
how to do similar things, so I've changed its doc string a bit.