@@ -2176,51 +2176,86 @@ def test_basic(self):
21762176 result = x .person_name .loc [0 ]
21772177 assert result == expected
21782178
2179- def test_creation_astype (self ):
2180- l = ["a" , "b" , "c" , "a" ]
2181- s = pd .Series (l )
2182- exp = pd .Series (Categorical (l ))
2183- res = s .astype ('category' )
2179+ def test_series_creation_astype (self ):
2180+ labels = list ('abca' )
2181+ exp = Series (Categorical (labels ))
2182+ res = Series (labels ).astype ('category' )
21842183 tm .assert_series_equal (res , exp )
21852184
2186- l = [1 , 2 , 3 , 1 ]
2187- s = pd .Series (l )
2188- exp = pd .Series (Categorical (l ))
2189- res = s .astype ('category' )
2185+ labels = [1 , 2 , 3 , 1 ]
2186+ exp = Series (Categorical (labels ))
2187+ res = Series (labels ).astype ('category' )
21902188 tm .assert_series_equal (res , exp )
21912189
2192- df = pd .DataFrame ({"cats" : [1 , 2 , 3 , 4 , 5 , 6 ],
2193- "vals" : [1 , 2 , 3 , 4 , 5 , 6 ]})
2194- cats = Categorical ([1 , 2 , 3 , 4 , 5 , 6 ])
2195- exp_df = pd .DataFrame ({"cats" : cats , "vals" : [1 , 2 , 3 , 4 , 5 , 6 ]})
2196- df ["cats" ] = df ["cats" ].astype ("category" )
2197- tm .assert_frame_equal (exp_df , df )
2190+ labels_int = [1 , 2 , 3 , 4 , 5 , 6 ]
2191+ exp = DataFrame ({"cats" : Categorical (labels_int ), "vals" : labels_int })
2192+ res = DataFrame ({"cats" : labels_int , "vals" : labels_int })
2193+ res ["cats" ] = res ["cats" ].astype ("category" )
2194+ tm .assert_frame_equal (res , exp )
21982195
2199- df = pd .DataFrame ({"cats" : ['a' , 'b' , 'b' , 'a' , 'a' , 'd' ],
2200- "vals" : [1 , 2 , 3 , 4 , 5 , 6 ]})
2201- cats = Categorical (['a' , 'b' , 'b' , 'a' , 'a' , 'd' ])
2202- exp_df = pd .DataFrame ({"cats" : cats , "vals" : [1 , 2 , 3 , 4 , 5 , 6 ]})
2203- df ["cats" ] = df ["cats" ].astype ("category" )
2204- tm .assert_frame_equal (exp_df , df )
2196+ labels_str = list ('abbaad' )
2197+ exp = DataFrame ({"cats" : Categorical (labels_str ), "vals" : labels_int })
2198+ res = DataFrame ({"cats" : labels_str , "vals" : labels_int })
2199+ res ["cats" ] = res ["cats" ].astype ("category" )
2200+ tm .assert_frame_equal (res , exp )
22052201
22062202 # with keywords
2207- l = [ "a" , "b" , "c" , "a" ]
2208- s = pd . Series (l )
2209- exp = pd . Series (Categorical (l , ordered = True ))
2203+ labels = list ( 'abca' )
2204+ s = Series (labels )
2205+ exp = Series (Categorical (labels , ordered = True ))
22102206 res = s .astype (CategoricalDtype (None , ordered = True ))
22112207 tm .assert_series_equal (res , exp )
22122208
2213- exp = pd . Series ( Categorical (
2214- l , categories = list ( 'abcdef' ) , ordered = True ))
2215- res = s .astype (CategoricalDtype (list ( 'abcdef' ) , ordered = True ))
2209+ cats = list ( 'abcdef' )
2210+ exp = Series ( Categorical ( labels , categories = cats , ordered = True ))
2211+ res = s .astype (CategoricalDtype (cats , ordered = True ))
22162212 tm .assert_series_equal (res , exp )
22172213
2214+ def test_frame_creation_astype (self ):
2215+ # GH 12860
2216+ cats = list ('abcde' )
2217+ x = Categorical (list ('abcd' ), categories = cats )
2218+ y = Categorical (list ('bcde' ), categories = cats )
2219+ exp = DataFrame ({'x' : x , 'y' : y })
2220+
2221+ data = {'x' : list ('abcd' ), 'y' : list ('bcde' )}
2222+ res = DataFrame (data ).astype ('category' )
2223+ tm .assert_frame_equal (res , exp )
2224+
2225+ res = DataFrame (data ).astype (CategoricalDtype ())
2226+ tm .assert_frame_equal (res , exp )
2227+
2228+ # categories keyword
2229+ cats = list ('abdef' )
2230+ x = Categorical (['a' , 'b' , np .nan , 'd' ], categories = cats )
2231+ y = Categorical (['b' , np .nan , 'd' , 'e' ], categories = cats )
2232+ exp = DataFrame ({'x' : x , 'y' : y })
2233+
2234+ res = DataFrame (data ).astype ('category' , categories = cats )
2235+ tm .assert_frame_equal (res , exp )
2236+
2237+ res = DataFrame (data ).astype (CategoricalDtype (categories = cats ))
2238+ tm .assert_frame_equal (res , exp )
2239+
2240+ # ordered keyword
2241+ cats = [1 , 2 , 3 , 4 , 0 ]
2242+ x = Categorical (range (1 , 5 ), categories = cats , ordered = True )
2243+ y = Categorical (range (4 ), categories = cats , ordered = True )
2244+ exp = DataFrame ({'x' : x , 'y' : y })
2245+
2246+ data = {'x' : range (1 , 5 ), 'y' : range (4 )}
2247+ res = DataFrame (data ).astype ('category' , ordered = True )
2248+ tm .assert_frame_equal (res , exp )
2249+
2250+ res = DataFrame (data ).astype (CategoricalDtype (ordered = True ))
2251+ tm .assert_frame_equal (res , exp )
2252+
22182253 @pytest .mark .parametrize ('columns' , [['x' ], ['x' , 'y' ], ['x' , 'y' , 'z' ]])
22192254 def test_empty_astype (self , columns ):
22202255 # GH 18004
2221- msg = '> 1 ndim Categorical are not supported at this time'
2222- with tm . assert_raises_regex ( NotImplementedError , msg ):
2223- DataFrame ( columns = columns ). astype ( 'category' )
2256+ exp = DataFrame ({ c : Categorical ([]) for c in columns }, index = [])
2257+ res = DataFrame ( columns = columns ). astype ( 'category' )
2258+ tm . assert_frame_equal ( res , exp )
22242259
22252260 def test_construction_series (self ):
22262261
0 commit comments