【发布时间】:2012-12-25 06:57:37
【问题描述】:
我有一个相当大的数据集 (2678271, 52) 和一个消耗 6.5% 机器内存的 5 维索引。 当我打电话时
df.sortlevel(k)
我收到以下错误:
MemoryError Traceback (most recent call last)
in ()
----> 1 df = df.sortlevel(4)
/usr/local/lib/python2.7/dist-packages/pandas-0.9.1-py2.7-linux-x86_64.egg/pandas/core/frame.pyc in sortlevel(self, level, axis, ascending)
2978 raise Exception('can only sort by level with a hierarchical index')
2979
-> 2980 new_axis, indexer = the_axis.sortlevel(level, ascending=ascending)
2981
2982 if self._data.is_mixed_dtype():
/usr/local/lib/python2.7/dist-packages/pandas-0.9.1-py2.7-linux-x86_64.egg/pandas/core/index.pyc in sortlevel(self, level, ascending)
1856 indexer = _indexer_from_factorized((primary,) + tuple(labels),
1857 (primshp,) + tuple(shape),
-> 1858 compress=False)
1859 if not ascending:
1860 indexer = indexer[::-1]
/usr/local/lib/python2.7/dist-packages/pandas-0.9.1-py2.7-linux-x86_64.egg/pandas/core/groupby.pyc in _indexer_from_factorized(labels, shape, compress)
2124 max_group = np.prod(shape)
2125
-> 2126 indexer, _ = lib.groupsort_indexer(comp_ids.astype(np.int64), max_group)
2127
2128 return indexer
/usr/local/lib/python2.7/dist-packages/pandas-0.9.1-py2.7-linux-x86_64.egg/pandas/lib.so in pandas.lib.groupsort_indexer (pandas/src/tseries.c:55052)()
MemoryError:
是否存在引发此错误的硬编码条件?或者是否有可能即使数据只使用了 6.5% 的内存(根据 htop),操作也会吃掉剩余的内存?
【问题讨论】:
-
在 0.10 中有不少性能增强。您可以尝试使用最新版本的熊猫吗? pandas.pydata.org/pandas-docs/stable/whatsnew.html
-
0.10 中仍有一些东西让我很难切换。在这种情况下,我必须等待 0.10.1。但是在这个问题上是否有可以解释这种行为的具体变化?
-
inplace选项已添加到sortlevel,这可能会减少内存使用量:github.com/pydata/pandas/issues/1873