5
5
import numpy as np
6
6
import pytest
7
7
8
- import pandas as pd
9
8
from pandas import (
9
+ Categorical ,
10
10
DataFrame ,
11
+ IndexSlice ,
11
12
MultiIndex ,
13
+ Series ,
14
+ option_context ,
12
15
)
13
16
import pandas ._testing as tm
14
17
@@ -137,16 +140,16 @@ def test_mi_styler_sparsify_index(mi_styler, sparse_index, exp_rows):
137
140
138
141
139
142
def test_mi_styler_sparsify_options (mi_styler ):
140
- with pd . option_context ("styler.sparse.index" , False ):
143
+ with option_context ("styler.sparse.index" , False ):
141
144
html1 = mi_styler .to_html ()
142
- with pd . option_context ("styler.sparse.index" , True ):
145
+ with option_context ("styler.sparse.index" , True ):
143
146
html2 = mi_styler .to_html ()
144
147
145
148
assert html1 != html2
146
149
147
- with pd . option_context ("styler.sparse.columns" , False ):
150
+ with option_context ("styler.sparse.columns" , False ):
148
151
html1 = mi_styler .to_html ()
149
- with pd . option_context ("styler.sparse.columns" , True ):
152
+ with option_context ("styler.sparse.columns" , True ):
150
153
html2 = mi_styler .to_html ()
151
154
152
155
assert html1 != html2
@@ -180,7 +183,7 @@ def test_trimming_maximum(rn, cn, max_els, max_rows, max_cols, exp_rn, exp_cn):
180
183
def test_render_trimming_rows (option , val ):
181
184
# test auto and specific trimming of rows
182
185
df = DataFrame (np .arange (120 ).reshape (60 , 2 ))
183
- with pd . option_context (option , val ):
186
+ with option_context (option , val ):
184
187
ctx = df .style ._translate (True , True )
185
188
assert len (ctx ["head" ][0 ]) == 3 # index + 2 data cols
186
189
assert len (ctx ["body" ]) == 4 # 3 data rows + trimming row
@@ -197,7 +200,7 @@ def test_render_trimming_rows(option, val):
197
200
def test_render_trimming_cols (option , val ):
198
201
# test auto and specific trimming of cols
199
202
df = DataFrame (np .arange (30 ).reshape (3 , 10 ))
200
- with pd . option_context (option , val ):
203
+ with option_context (option , val ):
201
204
ctx = df .style ._translate (True , True )
202
205
assert len (ctx ["head" ][0 ]) == 4 # index + 2 data cols + trimming col
203
206
assert len (ctx ["body" ]) == 3 # 3 data rows
@@ -207,7 +210,7 @@ def test_render_trimming_cols(option, val):
207
210
def test_render_trimming_mi ():
208
211
midx = MultiIndex .from_product ([[1 , 2 ], [1 , 2 , 3 ]])
209
212
df = DataFrame (np .arange (36 ).reshape (6 , 6 ), columns = midx , index = midx )
210
- with pd . option_context ("styler.render.max_elements" , 4 ):
213
+ with option_context ("styler.render.max_elements" , 4 ):
211
214
ctx = df .style ._translate (True , True )
212
215
213
216
assert len (ctx ["body" ][0 ]) == 5 # 2 indexes + 2 data cols + trimming row
@@ -446,16 +449,14 @@ def setup_method(self, method):
446
449
self .g = lambda x : x
447
450
448
451
def h (x , foo = "bar" ):
449
- return pd . Series (f"color: { foo } " , index = x .index , name = x .name )
452
+ return Series (f"color: { foo } " , index = x .index , name = x .name )
450
453
451
454
self .h = h
452
455
self .styler = Styler (self .df )
453
456
self .attrs = DataFrame ({"A" : ["color: red" , "color: blue" ]})
454
457
self .dataframes = [
455
458
self .df ,
456
- DataFrame (
457
- {"f" : [1.0 , 2.0 ], "o" : ["a" , "b" ], "c" : pd .Categorical (["a" , "b" ])}
458
- ),
459
+ DataFrame ({"f" : [1.0 , 2.0 ], "o" : ["a" , "b" ], "c" : Categorical (["a" , "b" ])}),
459
460
]
460
461
self .blank_value = " "
461
462
@@ -465,7 +466,7 @@ def test_init_non_pandas(self):
465
466
Styler ([1 , 2 , 3 ])
466
467
467
468
def test_init_series (self ):
468
- result = Styler (pd . Series ([1 , 2 ]))
469
+ result = Styler (Series ([1 , 2 ]))
469
470
assert result .data .ndim == 2
470
471
471
472
def test_repr_html_ok (self ):
@@ -475,7 +476,7 @@ def test_repr_html_mathjax(self):
475
476
# gh-19824 / 41395
476
477
assert "tex2jax_ignore" not in self .styler ._repr_html_ ()
477
478
478
- with pd . option_context ("styler.html.mathjax" , False ):
479
+ with option_context ("styler.html.mathjax" , False ):
479
480
assert "tex2jax_ignore" in self .styler ._repr_html_ ()
480
481
481
482
def test_update_ctx (self ):
@@ -494,7 +495,7 @@ def test_update_ctx_flatten_multi_and_trailing_semi(self):
494
495
495
496
def test_render (self ):
496
497
df = DataFrame ({"A" : [0 , 1 ]})
497
- style = lambda x : pd . Series (["color: red" , "color: blue" ], name = x .name )
498
+ style = lambda x : Series (["color: red" , "color: blue" ], name = x .name )
498
499
s = Styler (df , uuid = "AB" ).apply (style )
499
500
s .to_html ()
500
501
# it worked?
@@ -520,7 +521,7 @@ def test_render_empty_dfs(self):
520
521
521
522
def test_render_double (self ):
522
523
df = DataFrame ({"A" : [0 , 1 ]})
523
- style = lambda x : pd . Series (
524
+ style = lambda x : Series (
524
525
["color: red; border: 1px" , "color: blue; border: 2px" ], name = x .name
525
526
)
526
527
s = Styler (df , uuid = "AB" ).apply (style )
@@ -540,7 +541,7 @@ def test_set_properties(self):
540
541
def test_set_properties_subset (self ):
541
542
df = DataFrame ({"A" : [0 , 1 ]})
542
543
result = (
543
- df .style .set_properties (subset = pd . IndexSlice [0 , "A" ], color = "white" )
544
+ df .style .set_properties (subset = IndexSlice [0 , "A" ], color = "white" )
544
545
._compute ()
545
546
.ctx
546
547
)
@@ -614,13 +615,13 @@ def test_apply_series_return(self, axis):
614
615
df = DataFrame ([[1 , 2 ], [3 , 4 ]], index = ["X" , "Y" ], columns = ["X" , "Y" ])
615
616
616
617
# test Series return where len(Series) < df.index or df.columns but labels OK
617
- func = lambda s : pd . Series (["color: red;" ], index = ["Y" ])
618
+ func = lambda s : Series (["color: red;" ], index = ["Y" ])
618
619
result = df .style .apply (func , axis = axis )._compute ().ctx
619
620
assert result [(1 , 1 )] == [("color" , "red" )]
620
621
assert result [(1 - axis , axis )] == [("color" , "red" )]
621
622
622
623
# test Series return where labels align but different order
623
- func = lambda s : pd . Series (["color: red;" , "color: blue;" ], index = ["Y" , "X" ])
624
+ func = lambda s : Series (["color: red;" , "color: blue;" ], index = ["Y" , "X" ])
624
625
result = df .style .apply (func , axis = axis )._compute ().ctx
625
626
assert result [(0 , 0 )] == [("color" , "blue" )]
626
627
assert result [(1 , 1 )] == [("color" , "red" )]
@@ -645,11 +646,11 @@ def test_apply_dataframe_return(self, index, columns):
645
646
@pytest .mark .parametrize (
646
647
"slice_" ,
647
648
[
648
- pd . IndexSlice [:],
649
- pd . IndexSlice [:, ["A" ]],
650
- pd . IndexSlice [[1 ], :],
651
- pd . IndexSlice [[1 ], ["A" ]],
652
- pd . IndexSlice [:2 , ["A" , "B" ]],
649
+ IndexSlice [:],
650
+ IndexSlice [:, ["A" ]],
651
+ IndexSlice [[1 ], :],
652
+ IndexSlice [[1 ], ["A" ]],
653
+ IndexSlice [:2 , ["A" , "B" ]],
653
654
],
654
655
)
655
656
@pytest .mark .parametrize ("axis" , [0 , 1 ])
@@ -670,11 +671,11 @@ def test_apply_subset(self, slice_, axis):
670
671
@pytest .mark .parametrize (
671
672
"slice_" ,
672
673
[
673
- pd . IndexSlice [:],
674
- pd . IndexSlice [:, ["A" ]],
675
- pd . IndexSlice [[1 ], :],
676
- pd . IndexSlice [[1 ], ["A" ]],
677
- pd . IndexSlice [:2 , ["A" , "B" ]],
674
+ IndexSlice [:],
675
+ IndexSlice [:, ["A" ]],
676
+ IndexSlice [[1 ], :],
677
+ IndexSlice [[1 ], ["A" ]],
678
+ IndexSlice [:2 , ["A" , "B" ]],
678
679
],
679
680
)
680
681
def test_applymap_subset (self , slice_ ):
@@ -692,14 +693,14 @@ def test_applymap_subset(self, slice_):
692
693
@pytest .mark .parametrize (
693
694
"slice_" ,
694
695
[
695
- pd . IndexSlice [:, pd . IndexSlice ["x" , "A" ]],
696
- pd . IndexSlice [:, pd . IndexSlice [:, "A" ]],
697
- pd . IndexSlice [:, pd . IndexSlice [:, ["A" , "C" ]]], # missing col element
698
- pd . IndexSlice [pd . IndexSlice ["a" , 1 ], :],
699
- pd . IndexSlice [pd . IndexSlice [:, 1 ], :],
700
- pd . IndexSlice [pd . IndexSlice [:, [1 , 3 ]], :], # missing row element
701
- pd . IndexSlice [:, ("x" , "A" )],
702
- pd . IndexSlice [("a" , 1 ), :],
696
+ IndexSlice [:, IndexSlice ["x" , "A" ]],
697
+ IndexSlice [:, IndexSlice [:, "A" ]],
698
+ IndexSlice [:, IndexSlice [:, ["A" , "C" ]]], # missing col element
699
+ IndexSlice [IndexSlice ["a" , 1 ], :],
700
+ IndexSlice [IndexSlice [:, 1 ], :],
701
+ IndexSlice [IndexSlice [:, [1 , 3 ]], :], # missing row element
702
+ IndexSlice [:, ("x" , "A" )],
703
+ IndexSlice [("a" , 1 ), :],
703
704
],
704
705
)
705
706
def test_applymap_subset_multiindex (self , slice_ ):
@@ -737,7 +738,7 @@ def test_applymap_subset_multiindex_code(self):
737
738
df = DataFrame (
738
739
[[1 , - 1 , 1 , 1 ], [- 1 , 1 , 1 , 1 ]], index = ["hello" , "world" ], columns = columns
739
740
)
740
- pct_subset = pd . IndexSlice [:, pd . IndexSlice [:, "%" :"%" ]]
741
+ pct_subset = IndexSlice [:, IndexSlice [:, "%" :"%" ]]
741
742
742
743
def color_negative_red (val ):
743
744
color = "red" if val < 0 else "black"
@@ -913,13 +914,13 @@ def test_bad_apply_shape(self):
913
914
df .style ._apply (lambda x : ["" , "" , "" , "" ])
914
915
915
916
with pytest .raises (ValueError , match = msg .format ("index" )):
916
- df .style ._apply (lambda x : pd . Series (["a:v;" , "" ], index = ["A" , "C" ]), axis = 0 )
917
+ df .style ._apply (lambda x : Series (["a:v;" , "" ], index = ["A" , "C" ]), axis = 0 )
917
918
918
919
with pytest .raises (ValueError , match = msg .format ("columns" )):
919
920
df .style ._apply (lambda x : ["" , "" , "" ], axis = 1 )
920
921
921
922
with pytest .raises (ValueError , match = msg .format ("columns" )):
922
- df .style ._apply (lambda x : pd . Series (["a:v;" , "" ], index = ["X" , "Z" ]), axis = 1 )
923
+ df .style ._apply (lambda x : Series (["a:v;" , "" ], index = ["X" , "Z" ]), axis = 1 )
923
924
924
925
msg = "returned ndarray with wrong shape"
925
926
with pytest .raises (ValueError , match = msg ):
@@ -1315,18 +1316,18 @@ def test_uuid_len_raises(self, len_):
1315
1316
@pytest .mark .parametrize (
1316
1317
"slc" ,
1317
1318
[
1318
- pd . IndexSlice [:, :],
1319
- pd . IndexSlice [:, 1 ],
1320
- pd . IndexSlice [1 , :],
1321
- pd . IndexSlice [[1 ], [1 ]],
1322
- pd . IndexSlice [1 , [1 ]],
1323
- pd . IndexSlice [[1 ], 1 ],
1324
- pd . IndexSlice [1 ],
1325
- pd . IndexSlice [1 , 1 ],
1319
+ IndexSlice [:, :],
1320
+ IndexSlice [:, 1 ],
1321
+ IndexSlice [1 , :],
1322
+ IndexSlice [[1 ], [1 ]],
1323
+ IndexSlice [1 , [1 ]],
1324
+ IndexSlice [[1 ], 1 ],
1325
+ IndexSlice [1 ],
1326
+ IndexSlice [1 , 1 ],
1326
1327
slice (None , None , None ),
1327
1328
[0 , 1 ],
1328
1329
np .array ([0 , 1 ]),
1329
- pd . Series ([0 , 1 ]),
1330
+ Series ([0 , 1 ]),
1330
1331
],
1331
1332
)
1332
1333
def test_non_reducing_slice (self , slc ):
@@ -1335,13 +1336,13 @@ def test_non_reducing_slice(self, slc):
1335
1336
tslice_ = non_reducing_slice (slc )
1336
1337
assert isinstance (df .loc [tslice_ ], DataFrame )
1337
1338
1338
- @pytest .mark .parametrize ("box" , [list , pd . Series , np .array ])
1339
+ @pytest .mark .parametrize ("box" , [list , Series , np .array ])
1339
1340
def test_list_slice (self , box ):
1340
1341
# like dataframe getitem
1341
1342
subset = box (["A" ])
1342
1343
1343
1344
df = DataFrame ({"A" : [1 , 2 ], "B" : [3 , 4 ]}, index = ["A" , "B" ])
1344
- expected = pd . IndexSlice [:, ["A" ]]
1345
+ expected = IndexSlice [:, ["A" ]]
1345
1346
1346
1347
result = non_reducing_slice (subset )
1347
1348
tm .assert_frame_equal (df .loc [result ], df .loc [expected ])
@@ -1355,7 +1356,7 @@ def test_non_reducing_slice_on_multiindex(self):
1355
1356
("b" , "d" ): [4 , 1 ],
1356
1357
}
1357
1358
df = DataFrame (dic , index = [0 , 1 ])
1358
- idx = pd . IndexSlice
1359
+ idx = IndexSlice
1359
1360
slice_ = idx [:, idx ["b" , "d" ]]
1360
1361
tslice_ = non_reducing_slice (slice_ )
1361
1362
@@ -1366,27 +1367,27 @@ def test_non_reducing_slice_on_multiindex(self):
1366
1367
@pytest .mark .parametrize (
1367
1368
"slice_" ,
1368
1369
[
1369
- pd . IndexSlice [:, :],
1370
+ IndexSlice [:, :],
1370
1371
# check cols
1371
- pd . IndexSlice [:, pd . IndexSlice [["a" ]]], # inferred deeper need list
1372
- pd . IndexSlice [:, pd . IndexSlice [["a" ], ["c" ]]], # inferred deeper need list
1373
- pd . IndexSlice [:, pd . IndexSlice ["a" , "c" , :]],
1374
- pd . IndexSlice [:, pd . IndexSlice ["a" , :, "e" ]],
1375
- pd . IndexSlice [:, pd . IndexSlice [:, "c" , "e" ]],
1376
- pd . IndexSlice [:, pd . IndexSlice ["a" , ["c" , "d" ], :]], # check list
1377
- pd . IndexSlice [:, pd . IndexSlice ["a" , ["c" , "d" , "-" ], :]], # allow missing
1378
- pd . IndexSlice [:, pd . IndexSlice ["a" , ["c" , "d" , "-" ], "e" ]], # no slice
1372
+ IndexSlice [:, IndexSlice [["a" ]]], # inferred deeper need list
1373
+ IndexSlice [:, IndexSlice [["a" ], ["c" ]]], # inferred deeper need list
1374
+ IndexSlice [:, IndexSlice ["a" , "c" , :]],
1375
+ IndexSlice [:, IndexSlice ["a" , :, "e" ]],
1376
+ IndexSlice [:, IndexSlice [:, "c" , "e" ]],
1377
+ IndexSlice [:, IndexSlice ["a" , ["c" , "d" ], :]], # check list
1378
+ IndexSlice [:, IndexSlice ["a" , ["c" , "d" , "-" ], :]], # allow missing
1379
+ IndexSlice [:, IndexSlice ["a" , ["c" , "d" , "-" ], "e" ]], # no slice
1379
1380
# check rows
1380
- pd . IndexSlice [pd . IndexSlice [["U" ]], :], # inferred deeper need list
1381
- pd . IndexSlice [pd . IndexSlice [["U" ], ["W" ]], :], # inferred deeper need list
1382
- pd . IndexSlice [pd . IndexSlice ["U" , "W" , :], :],
1383
- pd . IndexSlice [pd . IndexSlice ["U" , :, "Y" ], :],
1384
- pd . IndexSlice [pd . IndexSlice [:, "W" , "Y" ], :],
1385
- pd . IndexSlice [pd . IndexSlice [:, "W" , ["Y" , "Z" ]], :], # check list
1386
- pd . IndexSlice [pd . IndexSlice [:, "W" , ["Y" , "Z" , "-" ]], :], # allow missing
1387
- pd . IndexSlice [pd . IndexSlice ["U" , "W" , ["Y" , "Z" , "-" ]], :], # no slice
1381
+ IndexSlice [IndexSlice [["U" ]], :], # inferred deeper need list
1382
+ IndexSlice [IndexSlice [["U" ], ["W" ]], :], # inferred deeper need list
1383
+ IndexSlice [IndexSlice ["U" , "W" , :], :],
1384
+ IndexSlice [IndexSlice ["U" , :, "Y" ], :],
1385
+ IndexSlice [IndexSlice [:, "W" , "Y" ], :],
1386
+ IndexSlice [IndexSlice [:, "W" , ["Y" , "Z" ]], :], # check list
1387
+ IndexSlice [IndexSlice [:, "W" , ["Y" , "Z" , "-" ]], :], # allow missing
1388
+ IndexSlice [IndexSlice ["U" , "W" , ["Y" , "Z" , "-" ]], :], # no slice
1388
1389
# check simultaneous
1389
- pd . IndexSlice [pd . IndexSlice [:, "W" , "Y" ], pd . IndexSlice ["a" , "c" , :]],
1390
+ IndexSlice [IndexSlice [:, "W" , "Y" ], IndexSlice ["a" , "c" , :]],
1390
1391
],
1391
1392
)
1392
1393
def test_non_reducing_multi_slice_on_multiindex (self , slice_ ):
@@ -1505,7 +1506,7 @@ def test_get_level_lengths_mi_hidden():
1505
1506
def test_row_trimming_hide_index ():
1506
1507
# gh 43703
1507
1508
df = DataFrame ([[1 ], [2 ], [3 ], [4 ], [5 ]])
1508
- with pd . option_context ("styler.render.max_rows" , 2 ):
1509
+ with option_context ("styler.render.max_rows" , 2 ):
1509
1510
ctx = df .style .hide ([0 , 1 ], axis = "index" )._translate (True , True )
1510
1511
assert len (ctx ["body" ]) == 3
1511
1512
for r , val in enumerate (["3" , "4" , "..." ]):
@@ -1516,7 +1517,7 @@ def test_row_trimming_hide_index_mi():
1516
1517
# gh 44247
1517
1518
df = DataFrame ([[1 ], [2 ], [3 ], [4 ], [5 ]])
1518
1519
df .index = MultiIndex .from_product ([[0 ], [0 , 1 , 2 , 3 , 4 ]])
1519
- with pd . option_context ("styler.render.max_rows" , 2 ):
1520
+ with option_context ("styler.render.max_rows" , 2 ):
1520
1521
ctx = df .style .hide ([(0 , 0 ), (0 , 1 )], axis = "index" )._translate (True , True )
1521
1522
assert len (ctx ["body" ]) == 3
1522
1523
@@ -1538,7 +1539,7 @@ def test_row_trimming_hide_index_mi():
1538
1539
def test_col_trimming_hide_columns ():
1539
1540
# gh 44272
1540
1541
df = DataFrame ([[1 , 2 , 3 , 4 , 5 ]])
1541
- with pd . option_context ("styler.render.max_columns" , 2 ):
1542
+ with option_context ("styler.render.max_columns" , 2 ):
1542
1543
ctx = df .style .hide ([0 , 1 ], axis = "columns" )._translate (True , True )
1543
1544
1544
1545
assert len (ctx ["head" ][0 ]) == 6 # blank, [0, 1 (hidden)], [2 ,3 (visible)], + trim
0 commit comments