@@ -1408,6 +1408,56 @@ def test_sel_dataarray_mindex(self):
14081408 )
14091409 )
14101410
1411+ def test_sel_categorical (self ):
1412+ ind = pd .Series (["foo" , "bar" ], dtype = "category" )
1413+ df = pd .DataFrame ({"ind" : ind , "values" : [1 , 2 ]})
1414+ ds = df .set_index ("ind" ).to_xarray ()
1415+ actual = ds .sel (ind = "bar" )
1416+ expected = ds .isel (ind = 1 )
1417+ assert_identical (expected , actual )
1418+
1419+ def test_sel_categorical_error (self ):
1420+ ind = pd .Series (["foo" , "bar" ], dtype = "category" )
1421+ df = pd .DataFrame ({"ind" : ind , "values" : [1 , 2 ]})
1422+ ds = df .set_index ("ind" ).to_xarray ()
1423+ with pytest .raises (ValueError ):
1424+ ds .sel (ind = "bar" , method = "nearest" )
1425+ with pytest .raises (ValueError ):
1426+ ds .sel (ind = "bar" , tolerance = "nearest" )
1427+
1428+ def test_categorical_index (self ):
1429+ cat = pd .CategoricalIndex (
1430+ ["foo" , "bar" , "foo" ],
1431+ categories = ["foo" , "bar" , "baz" , "qux" , "quux" , "corge" ],
1432+ )
1433+ ds = xr .Dataset (
1434+ {"var" : ("cat" , np .arange (3 ))},
1435+ coords = {"cat" : ("cat" , cat ), "c" : ("cat" , [0 , 1 , 1 ])},
1436+ )
1437+ # test slice
1438+ actual = ds .sel (cat = "foo" )
1439+ expected = ds .isel (cat = [0 , 2 ])
1440+ assert_identical (expected , actual )
1441+ # make sure the conversion to the array works
1442+ actual = ds .sel (cat = "foo" )["cat" ].values
1443+ assert (actual == np .array (["foo" , "foo" ])).all ()
1444+
1445+ ds = ds .set_index (index = ["cat" , "c" ])
1446+ actual = ds .unstack ("index" )
1447+ assert actual ["var" ].shape == (2 , 2 )
1448+
1449+ def test_categorical_reindex (self ):
1450+ cat = pd .CategoricalIndex (
1451+ ["foo" , "bar" , "baz" ],
1452+ categories = ["foo" , "bar" , "baz" , "qux" , "quux" , "corge" ],
1453+ )
1454+ ds = xr .Dataset (
1455+ {"var" : ("cat" , np .arange (3 ))},
1456+ coords = {"cat" : ("cat" , cat ), "c" : ("cat" , [0 , 1 , 2 ])},
1457+ )
1458+ actual = ds .reindex (cat = ["foo" ])["cat" ].values
1459+ assert (actual == np .array (["foo" ])).all ()
1460+
14111461 def test_sel_drop (self ):
14121462 data = Dataset ({"foo" : ("x" , [1 , 2 , 3 ])}, {"x" : [0 , 1 , 2 ]})
14131463 expected = Dataset ({"foo" : 1 })
@@ -3865,6 +3915,21 @@ def test_to_and_from_dataframe(self):
38653915 expected = pd .DataFrame ([[]], index = idx )
38663916 assert expected .equals (actual ), (expected , actual )
38673917
3918+ def test_from_dataframe_categorical (self ):
3919+ cat = pd .CategoricalDtype (
3920+ categories = ["foo" , "bar" , "baz" , "qux" , "quux" , "corge" ]
3921+ )
3922+ i1 = pd .Series (["foo" , "bar" , "foo" ], dtype = cat )
3923+ i2 = pd .Series (["bar" , "bar" , "baz" ], dtype = cat )
3924+
3925+ df = pd .DataFrame ({"i1" : i1 , "i2" : i2 , "values" : [1 , 2 , 3 ]})
3926+ ds = df .set_index ("i1" ).to_xarray ()
3927+ assert len (ds ["i1" ]) == 3
3928+
3929+ ds = df .set_index (["i1" , "i2" ]).to_xarray ()
3930+ assert len (ds ["i1" ]) == 2
3931+ assert len (ds ["i2" ]) == 2
3932+
38683933 @requires_sparse
38693934 def test_from_dataframe_sparse (self ):
38703935 import sparse
0 commit comments