Skip to content

Commit 286d907

Browse files
committed
ENH: parse categoricals in read_csv
1 parent 2beab41 commit 286d907

File tree

2 files changed

+242
-39
lines changed

2 files changed

+242
-39
lines changed

pandas/io/tests/parser/c_parser_only.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212

1313
import pandas as pd
1414
import pandas.util.testing as tm
15-
from pandas import DataFrame, Series, Index, MultiIndex
15+
from pandas import DataFrame, Series, Index, MultiIndex, Categorical
1616
from pandas import compat
1717
from pandas.compat import StringIO, range, lrange
18+
from pandas.types.dtypes import CategoricalDtype
1819

1920

2021
class CParserTests(object):
@@ -184,6 +185,26 @@ def test_pass_dtype(self):
184185
self.assertEqual(result['one'].dtype, 'u1')
185186
self.assertEqual(result['two'].dtype, 'object')
186187

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+
187208
def test_pass_dtype_as_recarray(self):
188209
if compat.is_platform_windows() and self.low_memory:
189210
raise nose.SkipTest(

0 commit comments

Comments
 (0)