PERF: use "_name" instead of "name" as Series metadata to avoid validation #51784
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.
See #46505 (comment) for context (and related to #51109).
The
_metadata
of a pandas object is what gets propagated in__finalize__
calls. For example the reason thatser.round()
preserves its name is because of this mechanism (of course, we could also just doname=self.name
when constructing the new series, which is also what we do in some places, but this is to explain how it works currently).But so in practice, in such cases we are setting a name that already was a name of a Series, in which case we shouldn't need to "validate" that it is a valid name (i.e. that it is hashable). Setting the "name" attribute (the equivalent of
ser.name = value
) is a costly part of the currentSeries.__finalize__
, and setting the private "_name" should (I think?) be functionally equivalent while avoiding the validation.So for the functionality of
__finalize__
, I think this shouldn't break anything. But there might be other places, or subclasses, that rely on the exact content of_metadata = ["name"]
?