pydata / pydata/xarray

ENH: Preserve attrs when converting to pandas dataframe

Open
#5,327 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

Is your feature request related to a problem? Please describe.

import xarray

xds = xarray.DataArray([1], name="a", dims="a", attrs={"long_name": "Description about data"})
xds.attrs

Output:

{'long_name': 'Description about data'}
xds.to_dataframe().a.attrs

Output:

{}

Describe the solution you'd like
It would be nice if the attributes of the DataArray were preserved in each pandas.Series and the attributes of each Dataset were preserved on the pandas.Dataframe

Additional context

Things to be wary about is that it the pandas documentation says the attrs is experimental.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the DataArray.to_dataframe conversion and the corresponding Dataset conversion entry point. Check how pandas Series.attrs and DataFrame.attrs are handled, then add coverage showing DataArray attributes on the resulting Series and Dataset attributes on the resulting DataFrame. Done means the requested attributes are preserved without relying on undocumented pandas behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python
Domain
data
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.