Skip to content
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

BUG: Fixed NDFrame.transform('abs') #20800

Merged
merged 3 commits into from
Apr 25, 2018

Conversation

TomAugspurger
Copy link
Contributor

Closes #19760

So this is a hacky workaround. I think we would ideally whitelist a set of valid NDFrame.transform methods, but I don't want to break things. What would that list include?

  • abs
  • pct_change
  • shift
  • tshift
  • cum*

@TomAugspurger TomAugspurger added this to the 0.23.0 milestone Apr 23, 2018
@TomAugspurger TomAugspurger added Regression Functionality that used to work in a prior pandas version Apply Apply, Aggregate, Transform, Map labels Apr 23, 2018
@codecov
Copy link

codecov bot commented Apr 24, 2018

Codecov Report

Merging #20800 into master will decrease coverage by 0.06%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #20800      +/-   ##
==========================================
- Coverage   91.85%   91.78%   -0.07%     
==========================================
  Files         153      153              
  Lines       49310    49273      -37     
==========================================
- Hits        45292    45224      -68     
- Misses       4018     4049      +31
Flag Coverage Δ
#multiple 90.17% <100%> (-0.08%) ⬇️
#single 41.88% <0%> (-0.01%) ⬇️
Impacted Files Coverage Δ
pandas/core/apply.py 96.78% <100%> (+0.04%) ⬆️
pandas/plotting/_converter.py 63.2% <0%> (-3.61%) ⬇️
pandas/core/nanops.py 95.13% <0%> (-1.17%) ⬇️
pandas/core/resample.py 96.06% <0%> (-0.37%) ⬇️
pandas/core/arrays/categorical.py 95.7% <0%> (-0.08%) ⬇️
pandas/core/indexes/base.py 96.63% <0%> (-0.06%) ⬇️
pandas/core/common.py 92.01% <0%> (-0.03%) ⬇️
pandas/core/indexes/datetimelike.py 96.7% <0%> (-0.02%) ⬇️
pandas/io/formats/latex.py 100% <0%> (ø) ⬆️
pandas/core/arrays/base.py 84.14% <0%> (ø) ⬆️
... and 8 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 31e77b0...715ac25. Read the comment docs.

@WillAyd
Copy link
Member

WillAyd commented Apr 24, 2018

I'd also add fill and rank to your list. FWIW there's probably a lot of overlap with the groupby module and what it considers to be a transformation vs an aggregation. Wonder if we should keep all of these functions and their "application type" in a shared module for both DataFrame and GroupBy ops

@@ -111,7 +111,9 @@ def get_result(self):

# string dispatch
if isinstance(self.f, compat.string_types):
self.kwds['axis'] = self.axis
if self.f not in {'abs'}:
Copy link
Contributor

Choose a reason for hiding this comment

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

NO, anytime 'hacky' work-around you need to re-think the patch.

Copy link
Contributor

Choose a reason for hiding this comment

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

either you need signature introspection (in python 3, may not be possible in python 2), alternatively can catch this type of error, but should be much higher level (in .aggregate), and not here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ugh, signature introspection seems hackier than hard-coding, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll see how hard it is to get Py2 & 3 working by inspecting the signature.

@jreback jreback removed this from the 0.23.0 milestone Apr 24, 2018
@TomAugspurger
Copy link
Contributor Author

This should be for 0.23. It's a regression and was included in the docs.

@TomAugspurger TomAugspurger added this to the 0.23.0 milestone Apr 24, 2018
@@ -111,8 +111,11 @@ def get_result(self):

# string dispatch
if isinstance(self.f, compat.string_types):
self.kwds['axis'] = self.axis
return getattr(self.obj, self.f)(*self.args, **self.kwds)
func = getattr(self.obj, self.f)
Copy link
Contributor

Choose a reason for hiding this comment

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

much better, and now IIRC we actually do something like this in groupby apply (for the same reason). can you add some comments here on what is going on

@@ -880,6 +880,13 @@ def f():
with np.errstate(all='ignore'):
df.agg({'A': ['abs', 'sum'], 'B': ['mean', 'max']})

def test_transform_abs_name(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need more tests? (for other funcs)

result = df.transform('abs')
expected = pd.DataFrame({"A": [1, 2]})
result = df.transform(method)
expected = operator.methodcaller(method)(df)
Copy link
Member

Choose a reason for hiding this comment

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

Learned something new today. Just out of curiosity, is this any different from doing getattr(df, method)()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in this case, no. methodcaller is nicer though when you need to bind parameters, but don't have the object yet (common in pytest fixtures).

@@ -880,11 +881,14 @@ def f():
with np.errstate(all='ignore'):
df.agg({'A': ['abs', 'sum'], 'B': ['mean', 'max']})

def test_transform_abs_name(self):
@pytest.mark.parametrize('method', [
'abs', 'shift', 'pct_change', 'cumsum', 'rank',
Copy link
Member

Choose a reason for hiding this comment

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

Could also add ffill and bfill

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Splitting to #20821

@TomAugspurger TomAugspurger merged commit 4aac0c8 into pandas-dev:master Apr 25, 2018
@TomAugspurger TomAugspurger deleted the transform-abs branch April 25, 2018 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants