-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
REF: Store metadata in an attrs dict #29062
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
FrozenSet, | ||
Hashable, | ||
List, | ||
Mapping, | ||
Optional, | ||
Sequence, | ||
Set, | ||
|
@@ -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, | ||
): | ||
|
||
|
@@ -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 """ | ||
|
@@ -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): | ||
""" | ||
|
@@ -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): | ||
|
||
|
@@ -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 | ||
|
@@ -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] | ||
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The 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
_metadata
for subclasses. With this PR, pandas doesn't use_metadata
itself anymore (previously, we just used it forSeries.name
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
ok can you open an issue to deprecate that