Skip to content

BUG: Fixed non-unique indexing memory allocation issue with .ix/.loc (GH4280) #4283

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
Jul 18, 2013
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
3 changes: 2 additions & 1 deletion doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ pandas 0.12
names (:issue:`3873`)
- Bug in non-unique indexing via ``iloc`` (:issue:`4017`); added ``takeable`` argument to
``reindex`` for location-based taking
- Allow non-unique indexing in series via ``.ix/.loc`` and ``__getitem`` (:issue:`4246)
- Allow non-unique indexing in series via ``.ix/.loc`` and ``__getitem__`` (:issue:`4246`)
- Fixed non-unique indexing memory allocation issue with ``.ix/.loc`` (:issue:`4280`)

- Fixed bug in groupby with empty series referencing a variable before assignment. (:issue:`3510`)
- Allow index name to be used in groupby for non MultiIndex (:issue:`4014`)
Expand Down
3 changes: 2 additions & 1 deletion doc/source/v0.12.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@ Bug Fixes
names (:issue:`3873`)
- Bug in non-unique indexing via ``iloc`` (:issue:`4017`); added ``takeable`` argument to
``reindex`` for location-based taking
- Allow non-unique indexing in series via ``.ix/.loc`` and ``__getitem`` (:issue:`4246)
- Allow non-unique indexing in series via ``.ix/.loc`` and ``__getitem__`` (:issue:`4246`)
- Fixed non-unique indexing memory allocation issue with ``.ix/.loc`` (:issue:`4280`)

- ``DataFrame.from_records`` did not accept empty recarrays (:issue:`3682`)
- ``read_html`` now correctly skips tests (:issue:`3741`)
Expand Down
18 changes: 16 additions & 2 deletions pandas/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,19 @@ cdef class IndexEngine:
dict d = {}
object val
int count = 0, count_missing = 0
Py_ssize_t i, j, n, n_t
Py_ssize_t i, j, n, n_t, n_alloc

self._ensure_mapping_populated()
values = self._get_index_values()
stargets = set(targets)
n = len(values)
n_t = len(targets)
result = np.empty(n*n_t, dtype=np.int64)
if n > 10000:
n_alloc = 10000
else:
n_alloc = n

result = np.empty(n_alloc, dtype=np.int64)
missing = np.empty(n_t, dtype=np.int64)

# form the set of the results (like ismember)
Expand All @@ -304,12 +309,21 @@ cdef class IndexEngine:
# found
if val in d:
for j in d[val]:

# realloc if needed
if count >= n_alloc:
n_alloc += 10000
result = np.resize(result, n_alloc)

result[count] = j
count += 1

# value not found
else:

if count >= n_alloc:
n_alloc += 10000
result = np.resize(result, n_alloc)
result[count] = -1
count += 1
missing[count_missing] = i
Expand Down
34 changes: 34 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,40 @@ def test_mi_access(self):
result = df2['A']['B2']
assert_frame_equal(result,expected)

def test_non_unique_loc_memory_error(self):

# GH 4280
# non_unique index with a large selection triggers a memory error

columns = list('ABCDEFG')
def gen_test(l,l2):
return pd.concat([ DataFrame(randn(l,len(columns)),index=range(l),columns=columns),
DataFrame(np.ones((l2,len(columns))),index=[0]*l2,columns=columns) ])


def gen_expected(df,mask):
l = len(mask)
return pd.concat([
df.take([0],convert=False),
DataFrame(np.ones((l,len(columns))),index=[0]*l,columns=columns),
df.take(mask[1:],convert=False) ])

df = gen_test(900,100)
self.assert_(not df.index.is_unique)

mask = np.arange(100)
result = df.loc[mask]
expected = gen_expected(df,mask)
assert_frame_equal(result,expected)

df = gen_test(900000,100000)
self.assert_(not df.index.is_unique)

mask = np.arange(100000)
result = df.loc[mask]
expected = gen_expected(df,mask)
assert_frame_equal(result,expected)

if __name__ == '__main__':
import nose
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
Expand Down