arrow icon indicating copy to clipboard operation
arrow copied to clipboard

[Python] Add support for pandas DataFrame.attrs

Open fangchenli opened this issue 1 year ago • 6 comments

Describe the enhancement requested

https://github.com/pandas-dev/pandas/issues/54321 added support for DataFrame.attrs during conversion between a DataFrame and a Parquet file. However, this should be done from the pyarrow side. Presumably in https://github.com/apache/arrow/blob/1a01b40c41ea849e348ef1e122587224346a772c/python/pyarrow/pandas_compat.py#L198

https://github.com/pandas-dev/pandas/issues/60675 is a related request for feather file.

Component(s)

Python

fangchenli avatar Jan 29 '25 16:01 fangchenli

Hey @fangchenli, my name's Bogdan, I'm part of EuroPython Sprints 2025, I was looking through the issues in this repo, and this one has piqued my interest, since I'm a daily user of pandas and pyarrow, and I've wanted to contribute for some time already. Regarding the issue itself: I looked through the issues that you've attached, and noticed that right now you take existing metadata in Pandas during file reading or writing, and inject custom attributes to existing JSON. As you've suggested, I decided to inject the attributes directly on pyarrow's end. The work is still in progress, but you can already see the underlying idea in the attached PR. Please let me know if you have any additional comments or suggestions, I will be happy to accommodate.

rmnskb avatar Jul 20 '25 15:07 rmnskb

Thank you for picking up this issue. It looks good to me so far. The Arrow team will likely provide more insightful comments here.

fangchenli avatar Jul 20 '25 19:07 fangchenli

Issue resolved by pull request 47147 https://github.com/apache/arrow/pull/47147

rok avatar Aug 09 '25 09:08 rok

@fangchenli now that this is merged, should we communicate it somewhere? Regarding attrs in feather, should that get an arrow issue? https://github.com/pandas-dev/pandas/issues/60675

rok avatar Aug 09 '25 09:08 rok

Thanks for your working! I have two questions:

  1. Does this support attrs of columns (e.g., df["col"].attrs["unit"])? Seems only df.attrs has been dealed in #47147 .
  2. Can this work when using pyarrow.Table.from_pandas(df)?

wohenbushuang avatar Sep 11 '25 04:09 wohenbushuang

Hey @wohenbushuang !

  1. This change supports only the attributes of pd.DataFrame, as they were in scope for this issue. I'm not entirely sure if the pd.Series attributes can be handled on the Arrow side 🤔
  2. Yes! The test for this change checks that this functionality is integrated.

rmnskb avatar Sep 13 '25 22:09 rmnskb