Skip to content

BUG-24408 Series.dt does not maintain own copy of index #24426

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 6 commits into from
Dec 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,7 @@ Datetimelike
- Bug in :class:`DatetimeIndex` and :class:`TimedeltaIndex` where indexing with ``Ellipsis`` would incorrectly lose the index's ``freq`` attribute (:issue:`21282`)
- Clarified error message produced when passing an incorrect ``freq`` argument to :class:`DatetimeIndex` with ``NaT`` as the first entry in the passed data (:issue:`11587`)
- Bug in :func:`to_datetime` where ``box`` and ``utc`` arguments were ignored when passing a :class:`DataFrame` or ``dict`` of unit mappings (:issue:`23760`)
- Bug in :attr:`Series.dt`` where the cache would not update properly after an in-place operation (:issue:`24408`)

Timedelta
^^^^^^^^^
Expand Down
8 changes: 3 additions & 5 deletions pandas/core/indexes/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def __init__(self, data, orig):
self._parent = data
self.orig = orig
self.name = getattr(data, 'name', None)
self.index = getattr(data, 'index', None)
self._freeze()

def _get_values(self):
Expand Down Expand Up @@ -71,8 +70,7 @@ def _delegate_property_get(self, name):
result = take_1d(result, self.orig.cat.codes)
index = self.orig.index
else:
index = self.index

index = self._parent.index
# return the result as a Series, which is by definition a copy
result = Series(result, index=index, name=self.name)

Expand All @@ -98,7 +96,7 @@ def _delegate_method(self, name, *args, **kwargs):
if not is_list_like(result):
return result

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

# setting this object will show a SettingWithCopyWarning/Error
result._is_copy = ("modifications to a method of a datetimelike "
Expand Down Expand Up @@ -261,7 +259,7 @@ def components(self):
3 0 0 0 3 0 0 0
4 0 0 0 4 0 0 0
""" # noqa: E501
return self._get_values().components.set_index(self.index)
return self._get_values().components.set_index(self._parent.index)

@property
def freq(self):
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/series/test_datetime_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,13 @@ def test_dt_accessor_invalid(self, ser):
ser.dt
assert not hasattr(ser, 'dt')

def test_dt_accessor_updates_on_inplace(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you test this for the other accessors as well. I think you can assert that the result.index is not the s.index to test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can leave this test and add some in pandas/series/test_api.py

s = Series(pd.date_range('2018-01-01', periods=10))
s[2] = None
s.fillna(pd.Timestamp('2018-01-01'), inplace=True)
result = s.dt.date
assert result[0] == result[2]

def test_between(self):
s = Series(bdate_range('1/1/2000', periods=20).astype(object))
s[::2] = np.nan
Expand Down