-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Comments
I believe the old behavior was considered a bug (xref #36189). I didn't follow that issue though, so can you confirm that @charlesdong1991? |
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 ( I hereby close the issue, but please feel free to ask if you have any doubts/questions! |
ok. It was great to have an index to change the name after...
but now I have as output this. How to have the value of the function ?
|
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? |
indeed I have a result this time but I lost the name of the function and it was my objective.
lambda> 120 270 |
my data and functions are more like that :
I have data with your solution but I lost again the name function (here f1, f2) |
thanks for your follow-up. @Jacques2101
it is not supported to have the renamings by providing index names in series
as said, it is not supported this way. A quick solution would be below which I think is as simple as 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'] |
it works. It was simpler than I thought...thx |
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
(this is of course an example of functions names)
Output
Expected Output
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]The text was updated successfully, but these errors were encountered: