@@ -4496,121 +4496,6 @@ def test_numpy_reshape(self):
4496
4496
tm .assert_raises_regex (ValueError , msg , np .reshape ,
4497
4497
cat , cat .shape , order = 'F' )
4498
4498
4499
- < << << << HEAD
4500
- def test_na_actions (self ):
4501
-
4502
- cat = Categorical ([1 , 2 , 3 , np .nan ], categories = [1 , 2 , 3 ])
4503
- vals = ["a" , "b" , np .nan , "d" ]
4504
- df = DataFrame ({"cats" : cat , "vals" : vals })
4505
- cat2 = Categorical ([1 , 2 , 3 , 3 ], categories = [1 , 2 , 3 ])
4506
- vals2 = ["a" , "b" , "b" , "d" ]
4507
- df_exp_fill = DataFrame ({"cats" : cat2 , "vals" : vals2 })
4508
- cat3 = Categorical ([1 , 2 , 3 ], categories = [1 , 2 , 3 ])
4509
- vals3 = ["a" , "b" , np .nan ]
4510
- df_exp_drop_cats = DataFrame ({"cats" : cat3 , "vals" : vals3 })
4511
- cat4 = Categorical ([1 , 2 ], categories = [1 , 2 , 3 ])
4512
- vals4 = ["a" , "b" ]
4513
- df_exp_drop_all = DataFrame ({"cats" : cat4 , "vals" : vals4 })
4514
-
4515
- # fillna
4516
- res = df .fillna (value = {"cats" : 3 , "vals" : "b" })
4517
- tm .assert_frame_equal (res , df_exp_fill )
4518
-
4519
- def f ():
4520
- df .fillna (value = {"cats" : 4 , "vals" : "c" })
4521
-
4522
- pytest .raises (ValueError , f )
4523
-
4524
- res = df .fillna (method = 'pad' )
4525
- tm .assert_frame_equal (res , df_exp_fill )
4526
-
4527
- res = df .dropna (subset = ["cats" ])
4528
- tm .assert_frame_equal (res , df_exp_drop_cats )
4529
-
4530
- res = df .dropna ()
4531
- tm .assert_frame_equal (res , df_exp_drop_all )
4532
-
4533
- # make sure that fillna takes missing values into account
4534
- c = Categorical ([np .nan , "b" , np .nan ], categories = ["a" , "b" ])
4535
- df = DataFrame ({"cats" : c , "vals" : [1 , 2 , 3 ]})
4536
-
4537
- cat_exp = Categorical (["a" , "b" , "a" ], categories = ["a" , "b" ])
4538
- df_exp = DataFrame ({"cats" : cat_exp , "vals" : [1 , 2 , 3 ]})
4539
-
4540
- res = df .fillna ("a" )
4541
- tm .assert_frame_equal (res , df_exp )
4542
-
4543
- # GH 14021
4544
- # np.nan should always be a is a valid filler
4545
- cat = Categorical ([np .nan , 2 , np .nan ])
4546
- val = Categorical ([np .nan , np .nan , np .nan ])
4547
- df = DataFrame ({"cats" : cat , "vals" : val })
4548
- res = df .fillna (df .median ())
4549
- v_exp = [np .nan , np .nan , np .nan ]
4550
- df_exp = DataFrame ({"cats" : [2 , 2 , 2 ], "vals" : v_exp },
4551
- dtype = 'category' )
4552
- tm .assert_frame_equal (res , df_exp )
4553
-
4554
- result = df .cats .fillna (np .nan )
4555
- tm .assert_series_equal (result , df .cats )
4556
- result = df .vals .fillna (np .nan )
4557
- tm .assert_series_equal (result , df .vals )
4558
-
4559
- idx = DatetimeIndex (['2011-01-01 09:00' , '2016-01-01 23:45' ,
4560
- '2011-01-01 09:00' , NaT , NaT ])
4561
- df = DataFrame ({'a' : Categorical (idx )})
4562
- tm .assert_frame_equal (df .fillna (value = NaT ), df )
4563
-
4564
- idx = PeriodIndex (
4565
- ['2011-01' , '2011-01' , '2011-01' , NaT , NaT ], freq = 'M' )
4566
- df = DataFrame ({'a' : Categorical (idx )})
4567
- tm .assert_frame_equal (df .fillna (value = NaT ), df )
4568
-
4569
- idx = TimedeltaIndex (['1 days' , '2 days' , '1 days' , NaT , NaT ])
4570
- df = DataFrame ({'a' : Categorical (idx )})
4571
- tm .assert_frame_equal (df .fillna (value = NaT ), df )
4572
-
4573
- @pytest .mark .parametrize ('fill_value, expected_output' , [
4574
- ('a' , ['a' , 'a' , 'b' , 'a' , 'a' ]),
4575
- ({1 : 'a' , 3 : 'b' , 4 : 'b' }, ['a' , 'a' , 'b' , 'b' , 'b' ]),
4576
- ({1 : 'a' }, ['a' , 'a' , 'b' , np .nan , np .nan ]),
4577
- ({1 : 'a' , 3 : 'b' }, ['a' , 'a' , 'b' , 'b' , np .nan ]),
4578
- (pd .Series ('a' ), ['a' , np .nan , 'b' , np .nan , np .nan ]),
4579
- (pd .Series ('a' , index = [1 ]), ['a' , 'a' , 'b' , np .nan , np .nan ]),
4580
- (pd .Series ({1 : 'a' , 3 : 'b' }), ['a' , 'a' , 'b' , 'b' , np .nan ]),
4581
- (pd .Series (['a' , 'b' ], index = [3 , 4 ]), ['a' , np .nan , 'b' , 'a' , 'b' ])
4582
- ])
4583
- def test_fillna_series_categorical (self , fill_value , expected_output ):
4584
- # GH 17033
4585
- # Test fillna for a Categorical series
4586
- data = ['a' , np .nan , 'b' , np .nan , np .nan ]
4587
- s = pd .Series (pd .Categorical (data , categories = ['a' , 'b' ]))
4588
- exp = pd .Series (pd .Categorical (expected_output , categories = ['a' , 'b' ]))
4589
- tm .assert_series_equal (s .fillna (fill_value ), exp )
4590
-
4591
- def test_fillna_series_categorical_errormsg (self ):
4592
- data = ['a' , np .nan , 'b' , np .nan , np .nan ]
4593
- s = pd .Series (pd .Categorical (data , categories = ['a' , 'b' ]))
4594
-
4595
- with tm .assert_raises_regex (ValueError ,
4596
- "fill value must be in categories" ):
4597
- s .fillna ('d' )
4598
-
4599
- with tm .assert_raises_regex (ValueError ,
4600
- "fill value must be in categories" ):
4601
- s .fillna (pd .Series ('d' ))
4602
-
4603
- with tm .assert_raises_regex (ValueError ,
4604
- "fill value must be in categories" ):
4605
- s .fillna ({1 : 'd' , 3 : 'a' })
4606
-
4607
- with tm .assert_raises_regex (TypeError ,
4608
- '"value" parameter must be a scalar or '
4609
- 'dict, but you passed a "list"' ):
4610
- s .fillna (['a' , 'b' ])
4611
-
4612
- == == == =
4613
- >> >> >> > move tests and add whatsnew
4614
4499
def test_astype_to_other (self ):
4615
4500
4616
4501
s = self .cat ['value_group' ]
0 commit comments