|
4 | 4 | import numpy as np
|
5 | 5 | from pandas.compat import zip
|
6 | 6 |
|
7 |
| -from pandas import DataFrame, Series, unique |
| 7 | +from pandas import DataFrame, Series, unique, Index, Categorical, CategoricalIndex |
8 | 8 | import pandas.util.testing as tm
|
9 | 9 | from pandas.util.testing import assertRaisesRegexp
|
10 | 10 | import pandas.core.common as com
|
@@ -97,6 +97,32 @@ def test_label_precision(self):
|
97 | 97 | '(0.54, 0.72]']
|
98 | 98 | self.assert_numpy_array_equal(result.categories, ex_levels)
|
99 | 99 |
|
| 100 | + def test_label_coercion(self): |
| 101 | + # GH10140 |
| 102 | + |
| 103 | + df = DataFrame({'x' : 100 * np.random.random(100)}) |
| 104 | + df['y'] = df.x**2 |
| 105 | + |
| 106 | + binedges = np.arange(0,110,10) |
| 107 | + binlabels = np.arange(5,105,10) |
| 108 | + |
| 109 | + for bl in [Index(binlabels), Categorical(binlabels), Index(binlabels).map(str)]: |
| 110 | + expected = Index(bl).dtype |
| 111 | + result = cut(df.x, bins=binedges, labels=bl) |
| 112 | + self.assertEqual(result.dtype, expected) |
| 113 | + z = df.groupby(result).y.mean() |
| 114 | + self.assertEqual(z.index.dtype, expected) |
| 115 | + |
| 116 | + # reversed categories |
| 117 | + bl = Categorical(binlabels,categories=binlabels[::-1],ordered=True) |
| 118 | + expected = Index(bl).dtype |
| 119 | + result = cut(df.x, bins=binedges, labels=bl) |
| 120 | + self.assertEqual(result.dtype, expected) |
| 121 | + z = df.groupby(result).y.mean() |
| 122 | + self.assertEqual(z.index.dtype, expected) |
| 123 | + tm.assert_index_equal(z.index, |
| 124 | + CategoricalIndex(Categorical.from_codes(np.arange(len(bl)),categories=bl.categories,ordered=True),name='x')) |
| 125 | + |
100 | 126 | def test_na_handling(self):
|
101 | 127 | arr = np.arange(0, 0.75, 0.01)
|
102 | 128 | arr[::3] = np.nan
|
|
0 commit comments