Skip to content

BUG: GH4080, int conversion of underlying series to float needs updating in parent frame #4081

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ pandas 0.12
iterated over when regex=False (:issue:`4115`)
- Fixed bug in ``convert_objects(convert_numeric=True)`` where a mixed numeric and
object Series/Frame was not converting properly (:issue:`4119`)
- Bug in Series update where the parent frame is not updating its blocks based on
dtype changes (:issue:`4080`)


pandas 0.11.0
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pylint: disable=W0231,E1101

import weakref
import numpy as np
import pandas.lib as lib
from pandas.core.base import PandasObject
Expand Down Expand Up @@ -667,11 +668,17 @@ def _get_item_cache(self, item):
values = self._data.get(item)
res = self._box_item_values(item, values)
cache[item] = res
res._cacher = (item,weakref.ref(self))
Copy link
Member

Choose a reason for hiding this comment

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

i envy your ability to see this solution so fast

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nah this was basically my solution on the other issue (which took a while to figure out); but was replaced with a slightly different form of considation (I this one is actually better though)

return res

def _box_item_values(self, key, values):
raise NotImplementedError

def _maybe_cache_changed(self, item, value):
""" the object has called back to us saying
maybe it has changed """
self._data.maybe_changed(item, value)

def _clear_item_cache(self):
self._item_cache.clear()

Expand Down
6 changes: 6 additions & 0 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,12 @@ def _interleave(self, items):

return result

def maybe_changed(self, item, value):
""" our obj may have changed dtype """
current = self.get(item)
if current.dtype != value.dtype:
self.set(item, value)

def xs(self, key, axis=1, copy=True):
if axis < 1:
raise AssertionError('Can only take xs across axis >= 1, got %d'
Expand Down
9 changes: 8 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,12 @@ def __setstate__(self, state):
self.index = _handle_legacy_indexes([index])[0]
self.name = name

def _maybe_update_cacher(self):
""" see if we need to update our parent cacher """
cacher = getattr(self,'_cacher',None)
if cacher is not None:
cacher[1]()._maybe_cache_changed(cacher[0],self)

# indexers
@property
def axes(self):
Expand Down Expand Up @@ -2157,7 +2163,8 @@ def update(self, other):
"""
other = other.reindex_like(self)
mask = notnull(other)
com._maybe_upcast_putmask(self.values,mask,other,change=self.values)
com._maybe_upcast_putmask(self.values,mask,other,change=self)
self._maybe_update_cacher()

#----------------------------------------------------------------------
# Reindexing, sorting
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8707,6 +8707,16 @@ def test_update_dtypes(self):
columns=['A','B','bool1','bool2'])
assert_frame_equal(df, expected)

# GH 4080, int conversions
df = DataFrame(dict((c, [1,2,3]) for c in ['a', 'b', 'c']))
df.set_index(['a', 'b', 'c'], inplace=True)
s = Series([1], index=[(2,2,2)])
df['val'] = 0
df['val'].update(s)
expected = DataFrame(dict(a = [1,2,3], b = [1,2,3], c = [1,2,3], val = [0.0,1.0,0.0]))
expected.set_index(['a', 'b', 'c'], inplace=True)
assert_frame_equal(df,expected)

def test_update_nooverwrite(self):
df = DataFrame([[1.5, nan, 3.],
[1.5, nan, 3.],
Expand Down