Skip to content

BUG: Possible segfault when chained indexing with an object array under numpy 1.7.1 (GH6016) #6031

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
Jan 21, 2014
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/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ Bug Fixes
- Bug in Series.xs with a multi-index (:issue:`6018`)
- Bug in Series construction of mixed type with datelike and an integer (which should result in
object type and not automatic conversion) (:issue:`6028`)
- Possible segfault when chained indexing with an object array under numpy 1.7.1 (:issue:`6016`)

pandas 0.13.0
-------------
Expand Down
1 change: 1 addition & 0 deletions pandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
_np_version = np.version.short_version
_np_version_under1p6 = LooseVersion(_np_version) < '1.6'
_np_version_under1p7 = LooseVersion(_np_version) < '1.7'
_np_version_under1p8 = LooseVersion(_np_version) < '1.8'

from pandas.version import version as __version__
from pandas.info import __doc__
Expand Down
11 changes: 8 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,9 +1005,14 @@ 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.set(item, value)
"""
the object has called back to us saying
maybe it has changed

numpy < 1.8 has an issue with object arrays and aliasing
GH6026
"""
self._data.set(item, value, check=pd._np_version_under1p8)

@property
def _is_cached(self):
Expand Down
32 changes: 26 additions & 6 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from pandas.compat import range, lrange, lmap, callable, map, zip, u
from pandas.tseries.timedeltas import _coerce_scalar_to_timedelta_type


class Block(PandasObject):

"""
Expand Down Expand Up @@ -279,7 +278,7 @@ def get(self, item):
def iget(self, i):
return self.values[i]

def set(self, item, value):
def set(self, item, value, check=False):
"""
Modify Block in-place with new item value

Expand Down Expand Up @@ -1360,6 +1359,26 @@ def convert(self, convert_dates=True, convert_numeric=True, convert_timedeltas=T

return blocks

def set(self, item, value, check=False):
"""
Modify Block in-place with new item value

Returns
-------
None
"""

loc = self.items.get_loc(item)

# GH6026
if check:
try:
if (self.values[loc] == value).all():
return
except:
pass
self.values[loc] = value

def _maybe_downcast(self, blocks, downcast=None):

if downcast is not None:
Expand Down Expand Up @@ -1601,7 +1620,7 @@ def astype(self, dtype, copy=False, raise_on_error=True):
return self._astype(dtype, copy=copy, raise_on_error=raise_on_error,
klass=klass)

def set(self, item, value):
def set(self, item, value, check=False):
"""
Modify Block in-place with new item value

Expand Down Expand Up @@ -1714,7 +1733,7 @@ def prepare_for_merge(self, **kwargs):
def post_merge(self, items, **kwargs):
return self

def set(self, item, value):
def set(self, item, value, check=False):
self.values = value

def get(self, item):
Expand Down Expand Up @@ -2879,10 +2898,11 @@ def delete(self, item):
if not is_unique:
self._consolidate_inplace()

def set(self, item, value):
def set(self, item, value, check=False):
"""
Set new item in-place. Does not consolidate. Adds new Block if not
contained in the current set of items
if check, then validate that we are not setting the same data in-place
"""
if not isinstance(value, SparseArray):
if value.ndim == self.ndim - 1:
Expand All @@ -2898,7 +2918,7 @@ def _set_item(item, arr):
self._delete_from_block(i, item)
self._add_new_block(item, arr, loc=None)
else:
block.set(item, arr)
block.set(item, arr, check=check)

try:

Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1950,6 +1950,29 @@ def test_setitem_cache_updating(self):
self.assert_(df.ix[0,'c'] == 0.0)
self.assert_(df.ix[7,'c'] == 1.0)

def test_setitem_chained_setfault(self):

# GH6026
# setfaults under numpy 1.7.1 (ok on 1.8)
data = ['right', 'left', 'left', 'left', 'right', 'left', 'timeout']
mdata = ['right', 'left', 'left', 'left', 'right', 'left', 'none']

df = DataFrame({'response': np.array(data)})
mask = df.response == 'timeout'
df.response[mask] = 'none'
assert_frame_equal(df, DataFrame({'response': mdata }))

recarray = np.rec.fromarrays([data], names=['response'])
df = DataFrame(recarray)
mask = df.response == 'timeout'
df.response[mask] = 'none'
assert_frame_equal(df, DataFrame({'response': mdata }))

df = DataFrame({'response': data, 'response1' : data })
mask = df.response == 'timeout'
df.response[mask] = 'none'
assert_frame_equal(df, DataFrame({'response': mdata, 'response1' : data }))

def test_detect_chained_assignment(self):

pd.set_option('chained_assignment','raise')
Expand Down