我有一个潜在的熊猫错误,或者我只是盯着这个太久了。我以前在多索引上使用 xs 没有遇到过问题。代码如下,我已经验证了 Python 2 on pandas 版本 0.16.2 和 Python 3 运行 pandas 版本 0.17.0:
In [1]:
import sys
if sys.version[0] == '2':
from StringIO import StringIO
if sys.version[0] == '3':
from io import StringIO
import pandas as pd
sstring = """\
m,p,tstep,value,jday,normed_value,datetime
6,407,0,1,564.5,5.75,1964-07-18 12:00:00
6,407,0,1,564.5,5.75,1964-07-18 12:00:00
7,407,0,1,564.5,5.75,1964-07-18 12:00:00
8,408,0,1,564.5,6.75,1964-07-18 12:00:00
"""
subset = pd.read_csv(StringIO(sstring),
index_col=['m', 'p'],
parse_dates=['datetime'])
subset.xs(6, level='m')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-1-dbc4b09ce656> in <module>()
21 print(subset)
22
---> 23 subset.xs(6, level='m')
C:\Anaconda\lib\site-packages\pandas\core\generic.pyc in xs(self, key, axis, level, copy, drop_level)
1458
1459 result = self.ix[indexer]
-> 1460 setattr(result, result._get_axis_name(axis), new_ax)
1461 return result
1462
C:\Anaconda\lib\site-packages\pandas\core\generic.pyc in __setattr__(self, name, value)
2159 try:
2160 object.__getattribute__(self, name)
-> 2161 return object.__setattr__(self, name, value)
2162 except AttributeError:
2163 pass
pandas\src\properties.pyx in pandas.lib.AxisProperty.__set__ (pandas\lib.c:42548)()
C:\Anaconda\lib\site-packages\pandas\core\generic.pyc in _set_axis(self, axis, labels)
411
412 def _set_axis(self, axis, labels):
--> 413 self._data.set_axis(axis, labels)
414 self._clear_item_cache()
415
C:\Anaconda\lib\site-packages\pandas\core\internals.pyc in set_axis(self, axis, new_labels)
2217 if new_len != old_len:
2218 raise ValueError('Length mismatch: Expected axis has %d elements, '
-> 2219 'new values have %d elements' % (old_len, new_len))
2220
2221 self.axes[axis] = new_labels
ValueError: Length mismatch: Expected axis has 4 elements, new values have 2 elements
但是,不指定级别的工作方式与使用 .loc 一样,如下所示:
In [16]:
print(subset.xs(6))
print(subset.loc[6])
tstep value jday normed_value datetime
p
407 0 1 564.5 5.75 1964-07-18 12:00:00
407 0 1 564.5 5.75 1964-07-18 12:00:00
tstep value jday normed_value datetime
p
407 0 1 564.5 5.75 1964-07-18 12:00:00
407 0 1 564.5 5.75 1964-07-18 12:00:00
有没有人对这种行为有所了解?