Skip to content

Commit

Permalink
API: Store name outside attrs
Browse files Browse the repository at this point in the history
This aligns with xarray and h5py:
pandas-dev#29062 (comment)
  • Loading branch information
TomAugspurger committed Jan 7, 2020
1 parent cea6853 commit a8fa1de
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
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: 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

0 comments on commit a8fa1de

Please sign in to comment.