@@ -228,14 +228,15 @@ def test_constructor(self):
228228 # two arrays
229229 # - when the first is an integer dtype and the second is not
230230 # - when the resulting codes are all -1/NaN
231- with tm .assert_produces_warning (None ):
231+ msg = "Constructing a Categorical with a dtype and values containing"
232+ with tm .assert_produces_warning (FutureWarning , match = msg ):
232233 Categorical ([0 , 1 , 2 , 0 , 1 , 2 ], categories = ["a" , "b" , "c" ])
233234
234- with tm .assert_produces_warning (None ):
235+ with tm .assert_produces_warning (FutureWarning , match = msg ):
235236 Categorical ([0 , 1 , 2 , 0 , 1 , 2 ], categories = [3 , 4 , 5 ])
236237
237238 # the next one are from the old docs
238- with tm .assert_produces_warning (None ):
239+ with tm .assert_produces_warning (FutureWarning , match = msg ):
239240 Categorical ([0 , 1 , 2 , 0 , 1 , 2 ], [1 , 2 , 3 ])
240241 cat = Categorical ([1 , 2 ], categories = [1 , 2 , 3 ])
241242
@@ -247,12 +248,16 @@ def test_constructor_with_existing_categories(self):
247248 # GH25318: constructing with pd.Series used to bogusly skip recoding
248249 # categories
249250 c0 = Categorical (["a" , "b" , "c" , "a" ])
250- c1 = Categorical (["a" , "b" , "c" , "a" ], categories = ["b" , "c" ])
251+ msg = "Constructing a Categorical with a dtype and values containing"
252+ with tm .assert_produces_warning (FutureWarning , match = msg ):
253+ c1 = Categorical (["a" , "b" , "c" , "a" ], categories = ["b" , "c" ])
251254
252- c2 = Categorical (c0 , categories = c1 .categories )
255+ with tm .assert_produces_warning (FutureWarning , match = msg ):
256+ c2 = Categorical (c0 , categories = c1 .categories )
253257 tm .assert_categorical_equal (c1 , c2 )
254258
255- c3 = Categorical (Series (c0 ), categories = c1 .categories )
259+ with tm .assert_produces_warning (FutureWarning , match = msg ):
260+ c3 = Categorical (Series (c0 ), categories = c1 .categories )
256261 tm .assert_categorical_equal (c1 , c3 )
257262
258263 def test_constructor_not_sequence (self ):
@@ -430,10 +435,13 @@ def test_constructor_dtype_and_others_raises(self):
430435
431436 @pytest .mark .parametrize ("categories" , [None , ["a" , "b" ], ["a" , "c" ]])
432437 def test_constructor_str_category (self , categories , ordered ):
433- result = Categorical (
434- ["a" , "b" ], categories = categories , ordered = ordered , dtype = "category"
435- )
436- expected = Categorical (["a" , "b" ], categories = categories , ordered = ordered )
438+ warn = FutureWarning if categories == ["a" , "c" ] else None
439+ msg = "Constructing a Categorical with a dtype and values containing"
440+ with tm .assert_produces_warning (warn , match = msg ):
441+ result = Categorical (
442+ ["a" , "b" ], categories = categories , ordered = ordered , dtype = "category"
443+ )
444+ expected = Categorical (["a" , "b" ], categories = categories , ordered = ordered )
437445 tm .assert_categorical_equal (result , expected )
438446
439447 def test_constructor_str_unknown (self ):
@@ -450,10 +458,12 @@ def test_constructor_np_strs(self):
450458 def test_constructor_from_categorical_with_dtype (self ):
451459 dtype = CategoricalDtype (["a" , "b" , "c" ], ordered = True )
452460 values = Categorical (["a" , "b" , "d" ])
453- result = Categorical (values , dtype = dtype )
461+ msg = "Constructing a Categorical with a dtype and values containing"
462+ with tm .assert_produces_warning (FutureWarning , match = msg ):
463+ result = Categorical (values , dtype = dtype )
454464 # We use dtype.categories, not values.categories
455465 expected = Categorical (
456- ["a" , "b" , "d" ], categories = ["a" , "b" , "c" ], ordered = True
466+ ["a" , "b" , None ], categories = ["a" , "b" , "c" ], ordered = True
457467 )
458468 tm .assert_categorical_equal (result , expected )
459469
@@ -470,16 +480,19 @@ def test_constructor_from_categorical_with_unknown_dtype(self):
470480 def test_constructor_from_categorical_string (self ):
471481 values = Categorical (["a" , "b" , "d" ])
472482 # use categories, ordered
473- result = Categorical (
474- values , categories = ["a" , "b" , "c" ], ordered = True , dtype = "category"
475- )
483+ msg = "Constructing a Categorical with a dtype and values containing"
484+ with tm .assert_produces_warning (FutureWarning , match = msg ):
485+ result = Categorical (
486+ values , categories = ["a" , "b" , "c" ], ordered = True , dtype = "category"
487+ )
476488 expected = Categorical (
477- ["a" , "b" , "d" ], categories = ["a" , "b" , "c" ], ordered = True
489+ ["a" , "b" , None ], categories = ["a" , "b" , "c" ], ordered = True
478490 )
479491 tm .assert_categorical_equal (result , expected )
480492
481493 # No string
482- result = Categorical (values , categories = ["a" , "b" , "c" ], ordered = True )
494+ with tm .assert_produces_warning (FutureWarning , match = msg ):
495+ result = Categorical (values , categories = ["a" , "b" , "c" ], ordered = True )
483496 tm .assert_categorical_equal (result , expected )
484497
485498 def test_constructor_with_categorical_categories (self ):
@@ -661,17 +674,25 @@ def test_from_inferred_categories_dtype(self):
661674 cats = ["a" , "b" , "d" ]
662675 codes = np .array ([0 , 1 , 0 , 2 ], dtype = "i8" )
663676 dtype = CategoricalDtype (["c" , "b" , "a" ], ordered = True )
664- result = Categorical ._from_inferred_categories (cats , codes , dtype )
677+ msg = "Constructing a Categorical with a dtype and values containing"
678+ with tm .assert_produces_warning (
679+ FutureWarning , match = msg , check_stacklevel = False
680+ ):
681+ result = Categorical ._from_inferred_categories (cats , codes , dtype )
665682 expected = Categorical (
666- ["a" , "b" , "a" , "d" ], categories = ["c" , "b" , "a" ], ordered = True
683+ ["a" , "b" , "a" , None ], categories = ["c" , "b" , "a" ], ordered = True
667684 )
668685 tm .assert_categorical_equal (result , expected )
669686
670687 def test_from_inferred_categories_coerces (self ):
671688 cats = ["1" , "2" , "bad" ]
672689 codes = np .array ([0 , 0 , 1 , 2 ], dtype = "i8" )
673690 dtype = CategoricalDtype ([1 , 2 ])
674- result = Categorical ._from_inferred_categories (cats , codes , dtype )
691+ msg = "Constructing a Categorical with a dtype and values containing"
692+ with tm .assert_produces_warning (
693+ FutureWarning , match = msg , check_stacklevel = False
694+ ):
695+ result = Categorical ._from_inferred_categories (cats , codes , dtype )
675696 expected = Categorical ([1 , 1 , 2 , np .nan ])
676697 tm .assert_categorical_equal (result , expected )
677698
@@ -722,7 +743,9 @@ def test_interval(self):
722743
723744 # extra
724745 values = pd .interval_range (8 , 11 , periods = 3 )
725- cat = Categorical (values , categories = idx )
746+ msg = "Constructing a Categorical with a dtype and values containing"
747+ with tm .assert_produces_warning (FutureWarning , match = msg ):
748+ cat = Categorical (values , categories = idx )
726749 expected_codes = np .array ([8 , 9 , - 1 ], dtype = "int8" )
727750 tm .assert_numpy_array_equal (cat .codes , expected_codes )
728751 tm .assert_index_equal (cat .categories , idx )
0 commit comments