Skip to content

Commit 50842c0

Browse files
API: Store name outside attrs (#30798)
* API: Store name outside attrs This aligns with xarray and h5py: #29062 (comment)
1 parent 425ec90 commit 50842c0

File tree

7 files changed

+28
-3
lines changed

7 files changed

+28
-3
lines changed

doc/source/reference/frame.rst

+2
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ Metadata
273273

274274
:attr:`DataFrame.attrs` is a dictionary for storing global metadata for this DataFrame.
275275

276+
.. warning:: ``DataFrame.attrs`` is considered experimental and may change without warning.
277+
276278
.. autosummary::
277279
:toctree: api/
278280

doc/source/reference/series.rst

+2
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,8 @@ Metadata
525525

526526
:attr:`Series.attrs` is a dictionary for storing global metadata for this Series.
527527

528+
.. warning:: ``Series.attrs`` is considered experimental and may change without warning.
529+
528530
.. autosummary::
529531
:toctree: api/
530532

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ Other enhancements
218218
- Added new writer for exporting Stata dta files in version 118, ``StataWriter118``. This format supports exporting strings containing Unicode characters (:issue:`23573`)
219219
- :meth:`Series.map` now accepts ``collections.abc.Mapping`` subclasses as a mapper (:issue:`29733`)
220220
- The ``pandas.datetime`` class is now deprecated. Import from ``datetime`` instead (:issue:`30296`)
221+
- Added an experimental :attr:`~DataFrame.attrs` for storing global metadata about a dataset (:issue:`29062`)
221222
- :meth:`Timestamp.fromisocalendar` is now compatible with python 3.8 and above (:issue:`28115`)
222223

223224

pandas/core/generic.py

+4
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ def _init_mgr(self, mgr, axes=None, dtype=None, copy=False):
231231
def attrs(self) -> Dict[Optional[Hashable], Any]:
232232
"""
233233
Dictionary of global attributes on this object.
234+
235+
.. warning::
236+
237+
attrs is experimental and may change without warning.
234238
"""
235239
if self._attrs is None:
236240
self._attrs = {}

pandas/core/series.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
159159

160160
_typ = "series"
161161

162-
_metadata: List[str] = []
162+
_name: Optional[Hashable]
163+
_metadata: List[str] = ["name"]
163164
_accessors = {"dt", "cat", "str", "sparse"}
164165
_deprecations = (
165166
base.IndexOpsMixin._deprecations
@@ -425,13 +426,13 @@ def dtypes(self):
425426

426427
@property
427428
def name(self) -> Optional[Hashable]:
428-
return self.attrs.get("name", None)
429+
return self._name
429430

430431
@name.setter
431432
def name(self, value: Optional[Hashable]) -> None:
432433
if not is_hashable(value):
433434
raise TypeError("Series.name must be a hashable type")
434-
self.attrs["name"] = value
435+
object.__setattr__(self, "_name", value)
435436

436437
@property
437438
def values(self):

pandas/tests/frame/test_api.py

+8
Original file line numberDiff line numberDiff line change
@@ -551,3 +551,11 @@ async def test_tab_complete_warning(self, ip):
551551
with tm.assert_produces_warning(None):
552552
with provisionalcompleter("ignore"):
553553
list(ip.Completer.completions("df.", 1))
554+
555+
def test_attrs(self):
556+
df = pd.DataFrame({"A": [2, 3]})
557+
assert df.attrs == {}
558+
df.attrs["version"] = 1
559+
560+
result = df.rename(columns=str)
561+
assert result.attrs == {"version": 1}

pandas/tests/series/test_api.py

+7
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,13 @@ def test_integer_series_size(self):
512512
s = Series(range(9), dtype="Int64")
513513
assert s.size == 9
514514

515+
def test_attrs(self):
516+
s = pd.Series([0, 1], name="abc")
517+
assert s.attrs == {}
518+
s.attrs["version"] = 1
519+
result = s + 1
520+
assert result.attrs == {"version": 1}
521+
515522

516523
class TestCategoricalSeries:
517524
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)