Как я уже отмечал в этом частично связанном вопросе , невозможно сортировать последовательности смешанного типа:
# Python3.6
sorted(['foo', 'bar', 10, 200, 3])
# => TypeError: '<' not supported between instances of 'str' and 'int'
Это влияет на запросы нарезки в пандах. Следующий пример иллюстрирует мою проблему.
import pandas as pd
import numpy as np
index = [(10,3),(10,1),(2,2),('foo',4),('bar',5)]
index = pd.MultiIndex.from_tuples(index)
data = np.random.randn(len(index),2)
table = pd.DataFrame(data=data, index=index)
idx=pd.IndexSlice
table.loc[idx[:10,:],:]
# The last line will raise an UnsortedIndexError because
# 'foo' and 'bar' appear in the wrong order.
Сообщение об исключении читается следующим образом:
UnsortedIndexError: 'MultiIndex slicing requires the index to be lexsorted: slicing on levels [0], lexsort depth 0'
В python2.x я восстановился после этого исключения путем лексической сортировки индекса:
# Python2.x:
table = table.sort_index()
# 0 1
# 2 2 0.020841 0.717178
# 10 1 1.608883 0.807834
# 3 0.566967 1.978718
# bar 5 -0.683814 -0.382024
# foo 4 0.150284 -0.750709
table.loc[idx[:10,:],:]
# 0 1
# 2 2 0.020841 0.717178
# 10 1 1.608883 0.807834
# 3 0.566967 1.978718
Однако в python3 я получаю исключение, о котором упоминал в начале:
TypeError: '<' not supported between instances of 'str' and 'int'
Как оправиться от этого? Преобразование индекса в строки перед сортировкой не вариант, потому что это нарушает правильное упорядочение индекса:
# Python2/3
index = [(10,3),(10,1),(2,2),('foo',4),('bar',5)]
index = list(map(lambda x: tuple(map(str,x)), index))
index = pd.MultiIndex.from_tuples(index)
data = np.random.randn(len(index),2)
table = pd.DataFrame(data=data, index=index)
table = table.sort_index()
# 0 1
# 10 1 0.020841 0.717178
# 3 1.608883 0.807834
# 2 2 0.566967 1.978718
# bar 5 -0.683814 -0.382024
# foo 4 0.150284 -0.750709
При таком порядке цензура на основе значений будет нарушена.
table.loc[idx[:10,:],:] # Raises a TypeError
table.loc[idx[:'10',:],:] # Misses to return the indices [2,:]
Как мне восстановиться после этого?