Skip to content

Commit

Permalink
REF: Store metadata in attrs dict
Browse files Browse the repository at this point in the history
This aids in the implementation of
pandas-dev#28394. Over there, I'm having
issues with using `NDFrame.__finalize__` to copy attributes, in part
because getattribute on NDFrame is so complicated.

This simplifies this because we only need to look in NDFrame.attrs,
which is just a plain dictionary.

Aside from the addition of a public NDFrame.attrs dictionary, there
aren't any user-facing API changes.
  • Loading branch information
TomAugspurger committed Oct 17, 2019
1 parent 509eb14 commit d1826bb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 20 deletions.
1 change: 0 additions & 1 deletion doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ 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: 12 additions & 1 deletion 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, Optional
from typing import Dict, FrozenSet, Hashable, Optional
import warnings

import numpy as np
Expand All @@ -30,6 +30,7 @@
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 @@ -663,6 +664,16 @@ 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
33 changes: 32 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
FrozenSet,
Hashable,
List,
Mapping,
Optional,
Sequence,
Set,
Expand Down Expand Up @@ -197,6 +198,7 @@ def __init__(
axes: Optional[List[Index]] = None,
copy: bool = False,
dtype: Optional[Dtype] = None,
attrs: Mapping[Hashable, Any] = None,
fastpath: bool = False,
):

Expand All @@ -213,6 +215,11 @@ def __init__(
object.__setattr__(self, "_is_copy", None)
object.__setattr__(self, "_data", data)
object.__setattr__(self, "_item_cache", {})
if attrs is None:
attrs = {}
else:
attrs = dict(attrs)
object.__setattr__(self, "_attrs", attrs)

def _init_mgr(self, mgr, axes=None, dtype=None, copy=False):
""" passed a manager and a axes dict """
Expand All @@ -233,6 +240,19 @@ def _init_mgr(self, mgr, axes=None, dtype=None, copy=False):

# ----------------------------------------------------------------------

@property
def attrs(self):
"""
Dictionary of global attributes on this object.
"""
if self._attrs is None:
self._attrs = {}
return self._attrs

@attrs.setter
def attrs(self, value: Mapping[Hashable, Any]) -> None:
self._attrs = dict(value)

@property
def is_copy(self):
"""
Expand Down Expand Up @@ -2029,7 +2049,13 @@ def to_dense(self):

def __getstate__(self):
meta = {k: getattr(self, k, None) for k in self._metadata}
return dict(_data=self._data, _typ=self._typ, _metadata=self._metadata, **meta)
return dict(
_data=self._data,
_typ=self._typ,
_metadata=self._metadata,
attrs=self.attrs,
**meta
)

def __setstate__(self, state):

Expand All @@ -2038,6 +2064,8 @@ def __setstate__(self, state):
elif isinstance(state, dict):
typ = state.get("_typ")
if typ is not None:
attrs = state.get("_attrs", {})
object.__setattr__(self, "_attrs", attrs)

# set in the order of internal names
# to avoid definitional recursion
Expand Down Expand Up @@ -5213,6 +5241,9 @@ def __finalize__(self, other, method=None, **kwargs):
"""
if isinstance(other, NDFrame):
for name in other.attrs:
self.attrs[name] = other.attrs[name]
# For subclasses using _metadata.
for name in self._metadata:
object.__setattr__(self, name, getattr(other, name, None))
return self
Expand Down
3 changes: 2 additions & 1 deletion 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 FrozenSet, Union
from typing import Any, FrozenSet, Hashable, Mapping, Union
import warnings

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

Expand Down
17 changes: 1 addition & 16 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
is_dict_like,
is_extension_array_dtype,
is_extension_type,
is_hashable,
is_integer,
is_iterator,
is_list_like,
Expand Down Expand Up @@ -173,7 +172,7 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
Copy input data.
"""

_metadata = ["name"]
_metadata = []
_accessors = {"dt", "cat", "str", "sparse"}
_deprecations = (
base.IndexOpsMixin._deprecations
Expand Down Expand Up @@ -324,7 +323,6 @@ def __init__(
data = SingleBlockManager(data, index, fastpath=True)

generic.NDFrame.__init__(self, data, fastpath=True)

self.name = name
self._set_axis(0, index, fastpath=True)

Expand Down Expand Up @@ -457,19 +455,6 @@ def _update_inplace(self, result, **kwargs):
# we want to call the generic version and not the IndexOpsMixin
return generic.NDFrame._update_inplace(self, result, **kwargs)

@property
def name(self):
"""
Return name of the Series.
"""
return self._name

@name.setter
def name(self, value):
if value is not None and not is_hashable(value):
raise TypeError("Series.name must be a hashable type")
object.__setattr__(self, "_name", value)

# ndarray compatibility
@property
def dtype(self):
Expand Down

0 comments on commit d1826bb

Please sign in to comment.