Skip to content

rename function with agg #36977

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
Jacques2101 opened this issue Oct 8, 2020 · 8 comments
Closed

rename function with agg #36977

Jacques2101 opened this issue Oct 8, 2020 · 8 comments

Comments

@Jacques2101
Copy link

Jacques2101 commented Oct 8, 2020

I just update to 1.1.3 and it seems that there is a problem (bug?) to rename function after an aggregate call. All the functions call are named so I can't use rename as previously.
How can I do to rename my function ?
I cannot do anymore

 .rename(columns={'<lambda_0>': 'function 0',
                  '<lambda_1>': 'function 1',....

(this is of course an example of functions names)

import pandas as pd
pdf = pd.DataFrame([[4, 9]] * 3, columns=['A', 'B'])
pdf.agg(["sum", lambda df: df.sum(), lambda df: df.sum()])

Output

		A	B
sum		12	27
<lambda>	12	27
<lambda>	12	27

Expected Output

		A	B
sum		12	27
<lambda_0>	12	27
<lambda_1>	12	27

Output of pd.show_versions()

INSTALLED VERSIONS

commit : db08276
python : 3.8.5.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Mon Aug 31 22:12:52 PDT 2020; root:xnu-6153.141.2~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.3
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.3
setuptools : 50.3.0.post20201006
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.2
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.18.1
pandas_datareader: 0.9.0
bs4 : 4.9.1
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.1
numexpr : None
odfpy : None
openpyxl : 3.0.5
pandas_gbq : None
pyarrow : 0.15.1
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.5.0
sqlalchemy : None
tables : None
tabulate : 0.8.7
xarray : 0.16.1
xlrd : 1.2.0
xlwt : None
numba : None

[paste the output of pd.show_versions() here leaving a blank line after the details tag]

@Jacques2101 Jacques2101 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 8, 2020
@TomAugspurger
Copy link
Contributor

I believe the old behavior was considered a bug (xref #36189). I didn't follow that issue though, so can you confirm that @charlesdong1991?

@TomAugspurger TomAugspurger removed the Needs Triage Issue that has not been reviewed by a pandas team member label Oct 8, 2020
@charlesdong1991
Copy link
Member

charlesdong1991 commented Oct 8, 2020

Thanks @Jacques2101 for the issue post!!

Yes, the old behaviour was considered as a regression bug, see the discussion in #36189 (comment)

so to summarize, the index shouldn't be changed (at least for now no discussion to make it public), and such change (lambda_0) was shortly exposed to the public and has been fixed in 1.1.3

I hereby close the issue, but please feel free to ask if you have any doubts/questions!

@Jacques2101
Copy link
Author

ok. It was great to have an index to change the name after...
I tried to change my code to (this is a toy data. Mine are more complicated) :

def func(df):
    return pd.Series([lambda x: x.sum(), 
                     lambda x: x.min()], 
                     index=['sum', 'min'])


pdf = pd.DataFrame([[4, 9]] * 3, columns=['A', 'B'])
pdf.apply(func)  

but now I have as output this. How to have the value of the function ?
Thx

A B
<function func.. at 0x7f96bbd9... <function func.. at 0x7f96b51c...
<function func.. at 0x7f96b51c... <function func.. at 0x7f96b51c...

@charlesdong1991
Copy link
Member

probably you've simplifed a lot for the example, based on your example, it looks like you don't need to put a function for this, what if you remove the function, and apply directly with series, something like:

s = pd.Series([lambda x: x.sum(), lambda x: x.min()], index=['sum', 'min'])
pdf = pd.DataFrame([[4, 9]] * 3, columns=['A', 'B'])
pdf.apply(s)

i think this should return result?

@Jacques2101
Copy link
Author

indeed I have a result this time but I lost the name of the function and it was my objective.

A	B

lambda> 120 270
lambda> 4 9

@Jacques2101
Copy link
Author

Jacques2101 commented Oct 8, 2020

my data and functions are more like that :

def perf(prices, n):
    '''
    Calculates the annualized performance of a price series.
    '''
    prices_w = prices.resample('W').last()
    return 100*(prices_w.iloc[-1]/prices_w.iloc[-52*n])**(1/n)-100


func = pd.Series([lambda x: perf(x, 1),
                  lambda x: perf(x, 3)],
                 index=['f1', 'f2'])

ndata = 10000
dates = pd.date_range('20130101', periods=ndata)
df = pd.DataFrame(np.random.randint(low=1, high=100, size=(ndata, 7)), index=dates, columns=list('ABCDEFG')).T

df.apply(func, axis=1)

I have data with your solution but I lost again the name function (here f1, f2)

@charlesdong1991
Copy link
Member

thanks for your follow-up. @Jacques2101

indeed I have a result this time but I lost the name of the function and it was my objective.

it is not supported to have the renamings by providing index names in series

I have data with your solution but I lost again the name function (here f1, f2)

as said, it is not supported this way. A quick solution would be below which I think is as simple as df.rename.

import pandas as pd
import numpy as np


def perf(prices, n):
    '''
    Calculates the annualized performance of a price series.
    '''
    return 100*(prices.iloc[-1]/prices.iloc[-n])**(1/n)-100


func = pd.Series([lambda x: perf(x, 1),
                  lambda x: perf(x, 10)],
                 index=['f1', 'f2'])


dates = pd.date_range('20130101', periods=1000)
df = pd.DataFrame(np.random.randint(low=1, high=100, size=(1000, 7)), index=dates, columns=list('ABCDEFG')).T

new_df = df.apply(func, axis=1)
new_df.columns = ['new_name1', 'new_name2']

@Jacques2101
Copy link
Author

it works. It was simpler than I thought...thx
can I ask you an other thing :
in the 'perf' function I calculate the performance (based on weekly data whereas my data is daily) but I would like also have a function that calculate the calendar performance. I mean the performance between the last day of 2018 and last day of 2019 (the year is an input of my function).
Do you have any idea ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants