Skip to content
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

API: Store name outside attrs #30798

Merged
merged 3 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ Other enhancements
- Added new writer for exporting Stata dta files in version 118, ``StataWriter118``. This format supports exporting strings containing Unicode characters (:issue:`23573`)
- :meth:`Series.map` now accepts ``collections.abc.Mapping`` subclasses as a mapper (:issue:`29733`)
- The ``pandas.datetime`` class is now deprecated. Import from ``datetime`` instead (:issue:`30296`)
- Added an experimental :attr:`~DataFrame.attrs` for storing global metadata about a dataset (:issue:`29062`)
- :meth:`Timestamp.fromisocalendar` is now compatible with python 3.8 and above (:issue:`28115`)


Expand Down
4 changes: 4 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ def _init_mgr(self, mgr, axes=None, dtype=None, copy=False):
def attrs(self) -> Dict[Optional[Hashable], Any]:
"""
Dictionary of global attributes on this object.

.. warning::

attrs is experimental and may change without warning.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Can you also add this in the reference docs (where you added it in https://github.com/pandas-dev/pandas/pull/29062/files)

"""
if self._attrs is None:
self._attrs = {}
Expand Down
7 changes: 4 additions & 3 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ class Series(base.IndexOpsMixin, generic.NDFrame):

_typ = "series"

_metadata: List[str] = []
_name: Optional[Hashable]
_metadata: List[str] = ["name"]
_accessors = {"dt", "cat", "str", "sparse"}
_deprecations = (
base.IndexOpsMixin._deprecations
Expand Down Expand Up @@ -425,13 +426,13 @@ def dtypes(self):

@property
def name(self) -> Optional[Hashable]:
return self.attrs.get("name", None)
return self._name

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

@property
def values(self):
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,3 +551,11 @@ async def test_tab_complete_warning(self, ip):
with tm.assert_produces_warning(None):
with provisionalcompleter("ignore"):
list(ip.Completer.completions("df.", 1))

def test_attrs(self):
df = pd.DataFrame({"A": [2, 3]})
assert df.attrs == {}
df.attrs["version"] = 1

result = df.rename(columns=str)
assert result.attrs == {"version": 1}
7 changes: 7 additions & 0 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,13 @@ def test_integer_series_size(self):
s = Series(range(9), dtype="Int64")
assert s.size == 9

def test_attrs(self):
s = pd.Series([0, 1], name="abc")
assert s.attrs == {}
s.attrs["version"] = 1
result = s + 1
assert result.attrs == {"version": 1}


class TestCategoricalSeries:
@pytest.mark.parametrize(
Expand Down