|
12 | 12 |
|
13 | 13 | import pandas as pd
|
14 | 14 | import pandas.util.testing as tm
|
15 |
| -from pandas import DataFrame, Series, Index, MultiIndex |
| 15 | +from pandas import DataFrame, Series, Index, MultiIndex, Categorical |
16 | 16 | from pandas import compat
|
17 | 17 | from pandas.compat import StringIO, range, lrange
|
| 18 | +from pandas.types.dtypes import CategoricalDtype |
18 | 19 |
|
19 | 20 |
|
20 | 21 | class CParserTests(object):
|
@@ -184,6 +185,26 @@ def test_pass_dtype(self):
|
184 | 185 | self.assertEqual(result['one'].dtype, 'u1')
|
185 | 186 | self.assertEqual(result['two'].dtype, 'object')
|
186 | 187 |
|
| 188 | + def test_categorical_dtype(self): |
| 189 | + # GH 10153 |
| 190 | + data = """a,b,c |
| 191 | +1,a,3.4 |
| 192 | +1,a,3.4 |
| 193 | +2,b,4.5""" |
| 194 | + expected = pd.DataFrame({'a': Categorical([1, 1, 2]), |
| 195 | + 'b': Categorical(['a', 'a', 'b']), |
| 196 | + 'c': Categorical([3.4, 3.4, 4.5])}) |
| 197 | + actual = self.read_csv(StringIO(data), dtype='category') |
| 198 | + tm.assert_frame_equal(actual, expected) |
| 199 | + |
| 200 | + actual = self.read_csv(StringIO(data), dtype=CategoricalDtype()) |
| 201 | + tm.assert_frame_equal(actual, expected) |
| 202 | + |
| 203 | + actual = self.read_csv(StringIO(data), dtype={'a': 'category', |
| 204 | + 'b': 'category', |
| 205 | + 'c': CategoricalDtype()}) |
| 206 | + tm.assert_frame_equal(actual, expected) |
| 207 | + |
187 | 208 | def test_pass_dtype_as_recarray(self):
|
188 | 209 | if compat.is_platform_windows() and self.low_memory:
|
189 | 210 | raise nose.SkipTest(
|
|
0 commit comments