-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: attrs lost for Series in DataFrame #35425
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
Your right, |
Ah, so it's an intentional decision, yeah, I can see why that makes sense. Can I do a PR to clarify that in the documentation? Also, is there a recommended way of dealing with the use case of wanting metadata to be attached to a column? I'd like to use custom accessors to add in some convenience functions which need to store some metadata either in the attrs attribute or in a custom one and (ideally) some of these accessors can be called on a dataframe or a series. Since finalize isn't called on all the columns, then one way I can think of doing this is to have the series accessor write the metadata into the DataFrame that the Series belongs do. Is there any way to access the 'parent' DataFrame from a Series, or some better way to do this? |
Sure, that would be great. I think we'll eventually want a page in the docs User Guide dedicated to
IMO, the metadata applies to the dataset as a whole, so I would have something like df.attrs["descriptions"] = {"col1": "description 1", "col2": "description 2"] And then we need to make sure that |
Yeah, there are still some PRs (like your one #33338) which should be closed before it's fully documented. I added a small note to the documentation page for the series in PR #35456
That's basically what I had in mind as well, but it'd be nice (if possible) to have it propagate the metadata through the first time when running For example, I wanted to use this a bit like: @pd.api.extensions.register_series_accessor('test')
class TestSeriesAccessor:
def __init__(self, pandas_object: pd.Series):
self._obj = pandas_object
def method(self) -> pd.Series:
res = self._obj
# imagine some useful operations, that want to store metadata
res.attrs['test'] = "some kind of metadata added by the method"
return res Then the behaviour would be like: >>> df = pd.DataFrame({'col1': [1, 2, 3, 4, 5], 'col2': [1, 2, 3, 4, 5]})
>>> col1_test = df.col1.test.method()
>>> col1_test.attrs
{'test': 'some kind of metadata added by the method'}
>>> df["col1_test"] = col1_test
>>> df.attrs # My dream:
{'col1_test': {'test': 'some kind of metadata added by the method'}} But that would require calling |
Another example where the current behaviour can lead to confusing behaviour, is when setting
|
I have checked that this issue has not already been reported.
Series
attrs being lost when put into aDataFrame
.I have confirmed this bug exists on the latest version of pandas.
(optional) I have confirmed this bug exists on the master branch of pandas.
Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.
Code Sample, a copy-pastable example
See this GitHub Gist Notebook.
Summary of the issue is:
Problem description
First issue is that attrs are not propagated through when a
Series
is added to aDataFrame
(likely related to #28283).Second issue is that if you call
__finalize__
to make sure that the attrs are present, once you add another column in or modify the dataframe, the attrs already present for a column are lost. My guess is that when the DataFrame is modified, finalize is not called per-column again, but I haven't looked into it much.I'd be happy to help with this issue, however there are already many issues related to this topic and a few branches where others are working on attrs/metadata/finalize, so I'm a bit hesitant to start work as it'll probably conflict with what has already been done.
What would be the best place to discuss collaboration on this?
Expected Output
Expectation would be that you can attach some attrs to a Series, place it into a dataframe, modify/work with the dataframe, then access the attrs again.
Output of
pd.show_versions()
import pandas as pd
pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.8.3.final.0
python-bits : 64
OS : Linux
OS-release : 5.7.0-3-MANJARO
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8
pandas : 1.0.5
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 19.2.3
setuptools : 41.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.16.1
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None
The text was updated successfully, but these errors were encountered: