55
66import numpy as np
77
8- import pandas as pd
98import pandas .util .testing as tm
109from pandas import (Categorical , Index , Series , DataFrame , CategoricalIndex )
1110from pandas .core .dtypes .dtypes import CategoricalDtype
11+ from pandas .tests .categorical .common import (TestCategorical ,
12+ TestCategoricalBlock )
1213
1314
14- class TestCategoricalGeneric (object ):
15-
16- def setup_method (self , method ):
17- self .factor = Categorical (['a' , 'b' , 'b' , 'a' , 'a' , 'c' , 'c' , 'c' ],
18- ordered = True )
15+ class TestCategoricalGeneric (TestCategorical ):
1916
2017 def test_categories_none (self ):
2118 factor = Categorical (['a' , 'b' , 'b' , 'a' ,
@@ -66,38 +63,6 @@ def test_describe(self):
6663 name = 'categories' ))
6764 tm .assert_frame_equal (desc , expected )
6865
69- def test_getitem (self ):
70- assert self .factor [0 ] == 'a'
71- assert self .factor [- 1 ] == 'c'
72-
73- subf = self .factor [[0 , 1 , 2 ]]
74- tm .assert_numpy_array_equal (subf ._codes ,
75- np .array ([0 , 1 , 1 ], dtype = np .int8 ))
76-
77- subf = self .factor [np .asarray (self .factor ) == 'c' ]
78- tm .assert_numpy_array_equal (subf ._codes ,
79- np .array ([2 , 2 , 2 ], dtype = np .int8 ))
80-
81- def test_setitem (self ):
82-
83- # int/positional
84- c = self .factor .copy ()
85- c [0 ] = 'b'
86- assert c [0 ] == 'b'
87- c [- 1 ] = 'a'
88- assert c [- 1 ] == 'a'
89-
90- # boolean
91- c = self .factor .copy ()
92- indexer = np .zeros (len (c ), dtype = 'bool' )
93- indexer [0 ] = True
94- indexer [- 1 ] = True
95- c [indexer ] = 'c'
96- expected = Categorical (['c' , 'b' , 'b' , 'a' , 'a' , 'c' , 'c' , 'c' ],
97- ordered = True )
98-
99- tm .assert_categorical_equal (c , expected )
100-
10166 def test_set_categories_inplace (self ):
10267 cat = self .factor .copy ()
10368 cat .set_categories (['a' , 'b' , 'c' , 'd' ], inplace = True )
@@ -212,81 +177,8 @@ def f():
212177 res = cat_rev > "b"
213178 tm .assert_numpy_array_equal (res , exp )
214179
215- def test_print (self ):
216- expected = ["[a, b, b, a, a, c, c, c]" ,
217- "Categories (3, object): [a < b < c]" ]
218- expected = "\n " .join (expected )
219- actual = repr (self .factor )
220- assert actual == expected
221-
222-
223- class TestCategoricalGenericBlock (object ):
224-
225- def setup_method (self , method ):
226- self .factor = Categorical (['a' , 'b' , 'b' , 'a' , 'a' , 'c' , 'c' , 'c' ])
227-
228- df = DataFrame ({'value' : np .random .randint (0 , 10000 , 100 )})
229- labels = ["{0} - {1}" .format (i , i + 499 ) for i in range (0 , 10000 , 500 )]
230- cat_labels = Categorical (labels , labels )
231-
232- df = df .sort_values (by = ['value' ], ascending = True )
233- df ['value_group' ] = pd .cut (df .value , range (0 , 10500 , 500 ),
234- right = False , labels = cat_labels )
235- self .cat = df
236-
237- def test_basic (self ):
238-
239- # test basic creation / coercion of categoricals
240- s = Series (self .factor , name = 'A' )
241- assert s .dtype == 'category'
242- assert len (s ) == len (self .factor )
243- str (s .values )
244- str (s )
245-
246- # in a frame
247- df = DataFrame ({'A' : self .factor })
248- result = df ['A' ]
249- tm .assert_series_equal (result , s )
250- result = df .iloc [:, 0 ]
251- tm .assert_series_equal (result , s )
252- assert len (df ) == len (self .factor )
253- str (df .values )
254- str (df )
255-
256- df = DataFrame ({'A' : s })
257- result = df ['A' ]
258- tm .assert_series_equal (result , s )
259- assert len (df ) == len (self .factor )
260- str (df .values )
261- str (df )
262-
263- # multiples
264- df = DataFrame ({'A' : s , 'B' : s , 'C' : 1 })
265- result1 = df ['A' ]
266- result2 = df ['B' ]
267- tm .assert_series_equal (result1 , s )
268- tm .assert_series_equal (result2 , s , check_names = False )
269- assert result2 .name == 'B'
270- assert len (df ) == len (self .factor )
271- str (df .values )
272- str (df )
273-
274- # GH8623
275- x = DataFrame ([[1 , 'John P. Doe' ], [2 , 'Jane Dove' ],
276- [1 , 'John P. Doe' ]],
277- columns = ['person_id' , 'person_name' ])
278- x ['person_name' ] = Categorical (x .person_name
279- ) # doing this breaks transform
280-
281- expected = x .iloc [0 ].person_name
282- result = x .person_name .iloc [0 ]
283- assert result == expected
284-
285- result = x .person_name [0 ]
286- assert result == expected
287-
288- result = x .person_name .loc [0 ]
289- assert result == expected
180+
181+ class TestCategoricalGenericBlock (TestCategoricalBlock ):
290182
291183 def test_describe (self ):
292184
@@ -310,18 +202,6 @@ def test_describe(self):
310202 res = df3 .describe ()
311203 tm .assert_numpy_array_equal (res ["cat" ].values , res ["s" ].values )
312204
313- def test_groupby_sort (self ):
314-
315- # http://stackoverflow.com/questions/23814368/sorting-pandas-categorical-labels-after-groupby
316- # This should result in a properly sorted Series so that the plot
317- # has a sorted x axis
318- # self.cat.groupby(['value_group'])['value_group'].count().plot(kind='bar')
319-
320- res = self .cat .groupby (['value_group' ])['value_group' ].count ()
321- exp = res [sorted (res .index , key = lambda x : float (x .split ()[0 ]))]
322- exp .index = CategoricalIndex (exp .index , name = exp .index .name )
323- tm .assert_series_equal (res , exp )
324-
325205 def test_astype_to_other (self ):
326206
327207 s = self .cat ['value_group' ]
0 commit comments