Skip to content

Commit

Permalink
fixup Index.name
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAugspurger committed Oct 18, 2019
1 parent d1826bb commit e6183cd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
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 @@ -204,6 +204,7 @@ Deprecations
- ``Index.set_value`` has been deprecated. For a given index ``idx``, array ``arr``,
value in ``idx`` of ``idx_val`` and a new value of ``val``, ``idx.set_value(arr, idx_val, val)``
is equivalent to ``arr[idx.get_loc(idx_val)] = val``, which should be used instead (:issue:`28621`).
-

.. _whatsnew_1000.prior_deprecations:

Expand Down
13 changes: 1 addition & 12 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import builtins
from collections import OrderedDict
import textwrap
from typing import Dict, FrozenSet, Hashable, Optional
from typing import Dict, FrozenSet, Optional
import warnings

import numpy as np
Expand All @@ -30,7 +30,6 @@
is_timedelta64_ns_dtype,
)
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
from pandas.core.dtypes.inference import is_hashable
from pandas.core.dtypes.missing import isna

from pandas.core import algorithms, common as com
Expand Down Expand Up @@ -664,16 +663,6 @@ class IndexOpsMixin:
]
) # type: FrozenSet[str]

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

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

def transpose(self, *args, **kwargs):
"""
Return the transpose, which is by definition self.
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import re
from textwrap import dedent
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
Expand Down Expand Up @@ -189,6 +190,9 @@ class NDFrame(PandasObject, SelectionMixin):
_is_copy = None
_data = None # type: BlockManager

if TYPE_CHECKING:
_attrs = {} # type: Dict[Hashable, Any]

# ----------------------------------------------------------------------
# Constructors

Expand All @@ -198,7 +202,7 @@ def __init__(
axes: Optional[List[Index]] = None,
copy: bool = False,
dtype: Optional[Dtype] = None,
attrs: Mapping[Hashable, Any] = None,
attrs: Optional[Mapping[Hashable, Any]] = None,
fastpath: bool = False,
):

Expand Down Expand Up @@ -241,7 +245,7 @@ def _init_mgr(self, mgr, axes=None, dtype=None, copy=False):
# ----------------------------------------------------------------------

@property
def attrs(self):
def attrs(self) -> Dict[Hashable, Any]:
"""
Dictionary of global attributes on this object.
"""
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
import operator
from textwrap import dedent
from typing import Any, FrozenSet, Hashable, Mapping, Union
from typing import FrozenSet, Union
import warnings

import numpy as np
Expand Down Expand Up @@ -266,7 +266,6 @@ def __new__(
name=None,
fastpath=None,
tupleize_cols=True,
attrs: Mapping[Hashable, Any] = None,
**kwargs
) -> "Index":

Expand Down
15 changes: 13 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from io import StringIO
from shutil import get_terminal_size
from textwrap import dedent
from typing import Any, Callable
from typing import Any, Callable, Hashable, List, Optional
import warnings

import numpy as np
Expand Down Expand Up @@ -44,6 +44,7 @@
ABCSeries,
ABCSparseArray,
)
from pandas.core.dtypes.inference import is_hashable
from pandas.core.dtypes.missing import (
isna,
na_value_for_dtype,
Expand Down Expand Up @@ -172,7 +173,7 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
Copy input data.
"""

_metadata = []
_metadata = [] # type: List[str]
_accessors = {"dt", "cat", "str", "sparse"}
_deprecations = (
base.IndexOpsMixin._deprecations
Expand Down Expand Up @@ -470,6 +471,16 @@ def dtypes(self):
"""
return self._data.dtype

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

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

@property
def ftype(self):
"""
Expand Down

0 comments on commit e6183cd

Please sign in to comment.