Skip to content

Commit e6183cd

Browse files
committed
fixup Index.name
1 parent d1826bb commit e6183cd

File tree

5 files changed

+22
-18
lines changed

5 files changed

+22
-18
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ Deprecations
204204
- ``Index.set_value`` has been deprecated. For a given index ``idx``, array ``arr``,
205205
value in ``idx`` of ``idx_val`` and a new value of ``val``, ``idx.set_value(arr, idx_val, val)``
206206
is equivalent to ``arr[idx.get_loc(idx_val)] = val``, which should be used instead (:issue:`28621`).
207+
-
207208

208209
.. _whatsnew_1000.prior_deprecations:
209210

pandas/core/base.py

+1-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import builtins
55
from collections import OrderedDict
66
import textwrap
7-
from typing import Dict, FrozenSet, Hashable, Optional
7+
from typing import Dict, FrozenSet, Optional
88
import warnings
99

1010
import numpy as np
@@ -30,7 +30,6 @@
3030
is_timedelta64_ns_dtype,
3131
)
3232
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
33-
from pandas.core.dtypes.inference import is_hashable
3433
from pandas.core.dtypes.missing import isna
3534

3635
from pandas.core import algorithms, common as com
@@ -664,16 +663,6 @@ class IndexOpsMixin:
664663
]
665664
) # type: FrozenSet[str]
666665

667-
@property
668-
def name(self) -> Optional[Hashable]:
669-
return self.attrs.get("name", None)
670-
671-
@name.setter
672-
def name(self, value: Hashable) -> None:
673-
if not is_hashable(value):
674-
raise TypeError("Series.name must be a hashable type")
675-
self.attrs["name"] = value
676-
677666
def transpose(self, *args, **kwargs):
678667
"""
679668
Return the transpose, which is by definition self.

pandas/core/generic.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import re
99
from textwrap import dedent
1010
from typing import (
11+
TYPE_CHECKING,
1112
Any,
1213
Callable,
1314
Dict,
@@ -189,6 +190,9 @@ class NDFrame(PandasObject, SelectionMixin):
189190
_is_copy = None
190191
_data = None # type: BlockManager
191192

193+
if TYPE_CHECKING:
194+
_attrs = {} # type: Dict[Hashable, Any]
195+
192196
# ----------------------------------------------------------------------
193197
# Constructors
194198

@@ -198,7 +202,7 @@ def __init__(
198202
axes: Optional[List[Index]] = None,
199203
copy: bool = False,
200204
dtype: Optional[Dtype] = None,
201-
attrs: Mapping[Hashable, Any] = None,
205+
attrs: Optional[Mapping[Hashable, Any]] = None,
202206
fastpath: bool = False,
203207
):
204208

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

243247
@property
244-
def attrs(self):
248+
def attrs(self) -> Dict[Hashable, Any]:
245249
"""
246250
Dictionary of global attributes on this object.
247251
"""

pandas/core/indexes/base.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import datetime
22
import operator
33
from textwrap import dedent
4-
from typing import Any, FrozenSet, Hashable, Mapping, Union
4+
from typing import FrozenSet, Union
55
import warnings
66

77
import numpy as np
@@ -266,7 +266,6 @@ def __new__(
266266
name=None,
267267
fastpath=None,
268268
tupleize_cols=True,
269-
attrs: Mapping[Hashable, Any] = None,
270269
**kwargs
271270
) -> "Index":
272271

pandas/core/series.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from io import StringIO
66
from shutil import get_terminal_size
77
from textwrap import dedent
8-
from typing import Any, Callable
8+
from typing import Any, Callable, Hashable, List, Optional
99
import warnings
1010

1111
import numpy as np
@@ -44,6 +44,7 @@
4444
ABCSeries,
4545
ABCSparseArray,
4646
)
47+
from pandas.core.dtypes.inference import is_hashable
4748
from pandas.core.dtypes.missing import (
4849
isna,
4950
na_value_for_dtype,
@@ -172,7 +173,7 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
172173
Copy input data.
173174
"""
174175

175-
_metadata = []
176+
_metadata = [] # type: List[str]
176177
_accessors = {"dt", "cat", "str", "sparse"}
177178
_deprecations = (
178179
base.IndexOpsMixin._deprecations
@@ -470,6 +471,16 @@ def dtypes(self):
470471
"""
471472
return self._data.dtype
472473

474+
@property
475+
def name(self) -> Optional[Hashable]:
476+
return self.attrs.get("name", None)
477+
478+
@name.setter
479+
def name(self, value: Hashable) -> None:
480+
if not is_hashable(value):
481+
raise TypeError("Series.name must be a hashable type")
482+
self.attrs["name"] = value
483+
473484
@property
474485
def ftype(self):
475486
"""

0 commit comments

Comments
 (0)