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 1 commit
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
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
Copy link
Member

Choose a reason for hiding this comment

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

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