Skip to content

Call finalize in Series.dt #36554

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

Merged
merged 1 commit into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ Other
^^^^^
- Bug in :meth:`DataFrame.replace` and :meth:`Series.replace` incorrectly raising ``AssertionError`` instead of ``ValueError`` when invalid parameter combinations are passed (:issue:`36045`)
- Bug in :meth:`DataFrame.replace` and :meth:`Series.replace` with numeric values and string ``to_replace`` (:issue:`34789`)
- Fixed metadata propagation in the :class:`Series.dt` accessor (:issue:`28283`)
- Bug in :meth:`Series.transform` would give incorrect results or raise when the argument ``func`` was dictionary (:issue:`35811`)
- Bug in :meth:`Index.union` behaving differently depending on whether operand is a :class:`Index` or other list-like (:issue:`36384`)

Expand Down
12 changes: 9 additions & 3 deletions pandas/core/indexes/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _delegate_property_get(self, name):
else:
index = self._parent.index
# return the result as a Series, which is by definition a copy
result = Series(result, index=index, name=self.name)
result = Series(result, index=index, name=self.name).__finalize__(self._parent)

# setting this object will show a SettingWithCopyWarning/Error
result._is_copy = (
Expand Down Expand Up @@ -106,7 +106,9 @@ def _delegate_method(self, name, *args, **kwargs):
if not is_list_like(result):
return result

result = Series(result, index=self._parent.index, name=self.name)
result = Series(result, index=self._parent.index, name=self.name).__finalize__(
self._parent
)

# setting this object will show a SettingWithCopyWarning/Error
result._is_copy = (
Expand Down Expand Up @@ -371,7 +373,11 @@ def components(self):
3 0 0 0 3 0 0 0
4 0 0 0 4 0 0 0
"""
return self._get_values().components.set_index(self._parent.index)
return (
self._get_values()
.components.set_index(self._parent.index)
.__finalize__(self._parent)
)

@property
def freq(self):
Expand Down
4 changes: 0 additions & 4 deletions pandas/tests/generic/test_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,6 @@ def test_string_method(method):
],
ids=idfn,
)
@not_implemented_mark
def test_datetime_method(method):
s = pd.Series(pd.date_range("2000", periods=4))
s.attrs = {"a": 1}
Expand Down Expand Up @@ -714,7 +713,6 @@ def test_datetime_method(method):
"days_in_month",
],
)
@not_implemented_mark
def test_datetime_property(attr):
s = pd.Series(pd.date_range("2000", periods=4))
s.attrs = {"a": 1}
Expand All @@ -725,7 +723,6 @@ def test_datetime_property(attr):
@pytest.mark.parametrize(
"attr", ["days", "seconds", "microseconds", "nanoseconds", "components"]
)
@not_implemented_mark
def test_timedelta_property(attr):
s = pd.Series(pd.timedelta_range("2000", periods=4))
s.attrs = {"a": 1}
Expand All @@ -734,7 +731,6 @@ def test_timedelta_property(attr):


@pytest.mark.parametrize("method", [operator.methodcaller("total_seconds")])
@not_implemented_mark
def test_timedelta_methods(method):
s = pd.Series(pd.timedelta_range("2000", periods=4))
s.attrs = {"a": 1}
Expand Down