I use pd.cutto discretize a data set. Everything works great. However, I have a question with the type of the object Categorical, which is the data type returned pd.cut. The docs say the object is Categoricaltreated as an array of strings, so I'm not surprised to see that these labels are lexically sorted when grouped.
For example, the following code:
df = pd.DataFrame({'value': np.random.randint(0, 10000, 100)})
labels = []
for i in range(0, 10000, 500):
labels.append("{0} - {1}".format(i, i + 499))
df.sort(columns=['value'], inplace=True, ascending=True)
df['value_group'] = pd.cut(df.value, range(0, 10500, 500), right=False, labels=labels)
df.groupby(['value_group'])['value_group'].count().plot(kind='bar')
It displays the following diagram:

(notice 500-599 in the middle)
Before grouping, the structure is in the following order:
In [94]: df['value_group']
Out [94]:
59 0 - 499
58 0 - 499
0 500 - 999
94 500 - 999
76 500 - 999
95 1000 - 1499
17 1000 - 1499
48 1000 - 1499
, , , - - char, . ['A) 0 - 499', 'B) 500-999', ... ], . , , - , (, ). ?