-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
REF: Store metadata in an attrs dict #29062
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,14 @@ | |
import re | ||
from textwrap import dedent | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
Callable, | ||
Dict, | ||
FrozenSet, | ||
Hashable, | ||
List, | ||
Mapping, | ||
Optional, | ||
Sequence, | ||
Set, | ||
|
@@ -188,6 +190,12 @@ class NDFrame(PandasObject, SelectionMixin): | |
_is_copy = None | ||
_data = None # type: BlockManager | ||
|
||
if TYPE_CHECKING: | ||
# TODO(PY36): replace with _attrs : Dict[Hashable, Any] | ||
# We need the TYPE_CHECKING, because _attrs is not a class attribute | ||
# and Py35 doesn't support the new syntax. | ||
_attrs = {} # type: Dict[Hashable, Any] | ||
|
||
# ---------------------------------------------------------------------- | ||
# Constructors | ||
|
||
|
@@ -197,6 +205,7 @@ def __init__( | |
axes: Optional[List[Index]] = None, | ||
copy: bool = False, | ||
dtype: Optional[Dtype] = None, | ||
attrs: Optional[Mapping[Hashable, Any]] = None, | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fastpath: bool = False, | ||
): | ||
|
||
|
@@ -213,6 +222,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 """ | ||
|
@@ -233,6 +247,19 @@ def _init_mgr(self, mgr, axes=None, dtype=None, copy=False): | |
|
||
# ---------------------------------------------------------------------- | ||
|
||
@property | ||
def attrs(self) -> Dict[Hashable, Any]: | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
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): | ||
""" | ||
|
@@ -2029,7 +2056,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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are attrs linked to _metadata in any way? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not yet, though perhaps eventually if we can find a suitable way to deprecate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wait, then we should deprecate _metadata right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most likely? I have no idea who is using it or what they're using it for though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok can you open an issue to deprecate that |
||
attrs=self.attrs, | ||
**meta | ||
) | ||
|
||
def __setstate__(self, state): | ||
|
||
|
@@ -2038,6 +2071,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 | ||
|
@@ -5213,6 +5248,9 @@ def __finalize__(self, other, method=None, **kwargs): | |
|
||
""" | ||
if isinstance(other, NDFrame): | ||
for name in other.attrs: | ||
self.attrs[name] = other.attrs[name] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. self.attrs.update(other) ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That'd work for now, but we'll need a for loop in my other PR since we'll have per-attr logic to resolve these. |
||
# For subclasses using _metadata. | ||
for name in self._metadata: | ||
object.__setattr__(self, name, getattr(other, name, None)) | ||
return self | ||
|
Uh oh!
There was an error while loading. Please reload this page.