Skip to content

Commit ba79824

Browse files
authored
remove pd. (#45229)
Co-authored-by: JHM Darbyshire (iMac) <[email protected]>
1 parent fa3dfdb commit ba79824

File tree

1 file changed

+74
-73
lines changed

1 file changed

+74
-73
lines changed

pandas/tests/io/formats/style/test_style.py

+74-73
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
import numpy as np
66
import pytest
77

8-
import pandas as pd
98
from pandas import (
9+
Categorical,
1010
DataFrame,
11+
IndexSlice,
1112
MultiIndex,
13+
Series,
14+
option_context,
1215
)
1316
import pandas._testing as tm
1417

@@ -137,16 +140,16 @@ def test_mi_styler_sparsify_index(mi_styler, sparse_index, exp_rows):
137140

138141

139142
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):
141144
html1 = mi_styler.to_html()
142-
with pd.option_context("styler.sparse.index", True):
145+
with option_context("styler.sparse.index", True):
143146
html2 = mi_styler.to_html()
144147

145148
assert html1 != html2
146149

147-
with pd.option_context("styler.sparse.columns", False):
150+
with option_context("styler.sparse.columns", False):
148151
html1 = mi_styler.to_html()
149-
with pd.option_context("styler.sparse.columns", True):
152+
with option_context("styler.sparse.columns", True):
150153
html2 = mi_styler.to_html()
151154

152155
assert html1 != html2
@@ -180,7 +183,7 @@ def test_trimming_maximum(rn, cn, max_els, max_rows, max_cols, exp_rn, exp_cn):
180183
def test_render_trimming_rows(option, val):
181184
# test auto and specific trimming of rows
182185
df = DataFrame(np.arange(120).reshape(60, 2))
183-
with pd.option_context(option, val):
186+
with option_context(option, val):
184187
ctx = df.style._translate(True, True)
185188
assert len(ctx["head"][0]) == 3 # index + 2 data cols
186189
assert len(ctx["body"]) == 4 # 3 data rows + trimming row
@@ -197,7 +200,7 @@ def test_render_trimming_rows(option, val):
197200
def test_render_trimming_cols(option, val):
198201
# test auto and specific trimming of cols
199202
df = DataFrame(np.arange(30).reshape(3, 10))
200-
with pd.option_context(option, val):
203+
with option_context(option, val):
201204
ctx = df.style._translate(True, True)
202205
assert len(ctx["head"][0]) == 4 # index + 2 data cols + trimming col
203206
assert len(ctx["body"]) == 3 # 3 data rows
@@ -207,7 +210,7 @@ def test_render_trimming_cols(option, val):
207210
def test_render_trimming_mi():
208211
midx = MultiIndex.from_product([[1, 2], [1, 2, 3]])
209212
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):
211214
ctx = df.style._translate(True, True)
212215

213216
assert len(ctx["body"][0]) == 5 # 2 indexes + 2 data cols + trimming row
@@ -446,16 +449,14 @@ def setup_method(self, method):
446449
self.g = lambda x: x
447450

448451
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)
450453

451454
self.h = h
452455
self.styler = Styler(self.df)
453456
self.attrs = DataFrame({"A": ["color: red", "color: blue"]})
454457
self.dataframes = [
455458
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"])}),
459460
]
460461
self.blank_value = "&nbsp;"
461462

@@ -465,7 +466,7 @@ def test_init_non_pandas(self):
465466
Styler([1, 2, 3])
466467

467468
def test_init_series(self):
468-
result = Styler(pd.Series([1, 2]))
469+
result = Styler(Series([1, 2]))
469470
assert result.data.ndim == 2
470471

471472
def test_repr_html_ok(self):
@@ -475,7 +476,7 @@ def test_repr_html_mathjax(self):
475476
# gh-19824 / 41395
476477
assert "tex2jax_ignore" not in self.styler._repr_html_()
477478

478-
with pd.option_context("styler.html.mathjax", False):
479+
with option_context("styler.html.mathjax", False):
479480
assert "tex2jax_ignore" in self.styler._repr_html_()
480481

481482
def test_update_ctx(self):
@@ -494,7 +495,7 @@ def test_update_ctx_flatten_multi_and_trailing_semi(self):
494495

495496
def test_render(self):
496497
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)
498499
s = Styler(df, uuid="AB").apply(style)
499500
s.to_html()
500501
# it worked?
@@ -520,7 +521,7 @@ def test_render_empty_dfs(self):
520521

521522
def test_render_double(self):
522523
df = DataFrame({"A": [0, 1]})
523-
style = lambda x: pd.Series(
524+
style = lambda x: Series(
524525
["color: red; border: 1px", "color: blue; border: 2px"], name=x.name
525526
)
526527
s = Styler(df, uuid="AB").apply(style)
@@ -540,7 +541,7 @@ def test_set_properties(self):
540541
def test_set_properties_subset(self):
541542
df = DataFrame({"A": [0, 1]})
542543
result = (
543-
df.style.set_properties(subset=pd.IndexSlice[0, "A"], color="white")
544+
df.style.set_properties(subset=IndexSlice[0, "A"], color="white")
544545
._compute()
545546
.ctx
546547
)
@@ -614,13 +615,13 @@ def test_apply_series_return(self, axis):
614615
df = DataFrame([[1, 2], [3, 4]], index=["X", "Y"], columns=["X", "Y"])
615616

616617
# 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"])
618619
result = df.style.apply(func, axis=axis)._compute().ctx
619620
assert result[(1, 1)] == [("color", "red")]
620621
assert result[(1 - axis, axis)] == [("color", "red")]
621622

622623
# 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"])
624625
result = df.style.apply(func, axis=axis)._compute().ctx
625626
assert result[(0, 0)] == [("color", "blue")]
626627
assert result[(1, 1)] == [("color", "red")]
@@ -645,11 +646,11 @@ def test_apply_dataframe_return(self, index, columns):
645646
@pytest.mark.parametrize(
646647
"slice_",
647648
[
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"]],
653654
],
654655
)
655656
@pytest.mark.parametrize("axis", [0, 1])
@@ -670,11 +671,11 @@ def test_apply_subset(self, slice_, axis):
670671
@pytest.mark.parametrize(
671672
"slice_",
672673
[
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"]],
678679
],
679680
)
680681
def test_applymap_subset(self, slice_):
@@ -692,14 +693,14 @@ def test_applymap_subset(self, slice_):
692693
@pytest.mark.parametrize(
693694
"slice_",
694695
[
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), :],
703704
],
704705
)
705706
def test_applymap_subset_multiindex(self, slice_):
@@ -737,7 +738,7 @@ def test_applymap_subset_multiindex_code(self):
737738
df = DataFrame(
738739
[[1, -1, 1, 1], [-1, 1, 1, 1]], index=["hello", "world"], columns=columns
739740
)
740-
pct_subset = pd.IndexSlice[:, pd.IndexSlice[:, "%":"%"]]
741+
pct_subset = IndexSlice[:, IndexSlice[:, "%":"%"]]
741742

742743
def color_negative_red(val):
743744
color = "red" if val < 0 else "black"
@@ -913,13 +914,13 @@ def test_bad_apply_shape(self):
913914
df.style._apply(lambda x: ["", "", "", ""])
914915

915916
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)
917918

918919
with pytest.raises(ValueError, match=msg.format("columns")):
919920
df.style._apply(lambda x: ["", "", ""], axis=1)
920921

921922
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)
923924

924925
msg = "returned ndarray with wrong shape"
925926
with pytest.raises(ValueError, match=msg):
@@ -1315,18 +1316,18 @@ def test_uuid_len_raises(self, len_):
13151316
@pytest.mark.parametrize(
13161317
"slc",
13171318
[
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],
13261327
slice(None, None, None),
13271328
[0, 1],
13281329
np.array([0, 1]),
1329-
pd.Series([0, 1]),
1330+
Series([0, 1]),
13301331
],
13311332
)
13321333
def test_non_reducing_slice(self, slc):
@@ -1335,13 +1336,13 @@ def test_non_reducing_slice(self, slc):
13351336
tslice_ = non_reducing_slice(slc)
13361337
assert isinstance(df.loc[tslice_], DataFrame)
13371338

1338-
@pytest.mark.parametrize("box", [list, pd.Series, np.array])
1339+
@pytest.mark.parametrize("box", [list, Series, np.array])
13391340
def test_list_slice(self, box):
13401341
# like dataframe getitem
13411342
subset = box(["A"])
13421343

13431344
df = DataFrame({"A": [1, 2], "B": [3, 4]}, index=["A", "B"])
1344-
expected = pd.IndexSlice[:, ["A"]]
1345+
expected = IndexSlice[:, ["A"]]
13451346

13461347
result = non_reducing_slice(subset)
13471348
tm.assert_frame_equal(df.loc[result], df.loc[expected])
@@ -1355,7 +1356,7 @@ def test_non_reducing_slice_on_multiindex(self):
13551356
("b", "d"): [4, 1],
13561357
}
13571358
df = DataFrame(dic, index=[0, 1])
1358-
idx = pd.IndexSlice
1359+
idx = IndexSlice
13591360
slice_ = idx[:, idx["b", "d"]]
13601361
tslice_ = non_reducing_slice(slice_)
13611362

@@ -1366,27 +1367,27 @@ def test_non_reducing_slice_on_multiindex(self):
13661367
@pytest.mark.parametrize(
13671368
"slice_",
13681369
[
1369-
pd.IndexSlice[:, :],
1370+
IndexSlice[:, :],
13701371
# 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
13791380
# 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
13881389
# check simultaneous
1389-
pd.IndexSlice[pd.IndexSlice[:, "W", "Y"], pd.IndexSlice["a", "c", :]],
1390+
IndexSlice[IndexSlice[:, "W", "Y"], IndexSlice["a", "c", :]],
13901391
],
13911392
)
13921393
def test_non_reducing_multi_slice_on_multiindex(self, slice_):
@@ -1505,7 +1506,7 @@ def test_get_level_lengths_mi_hidden():
15051506
def test_row_trimming_hide_index():
15061507
# gh 43703
15071508
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):
15091510
ctx = df.style.hide([0, 1], axis="index")._translate(True, True)
15101511
assert len(ctx["body"]) == 3
15111512
for r, val in enumerate(["3", "4", "..."]):
@@ -1516,7 +1517,7 @@ def test_row_trimming_hide_index_mi():
15161517
# gh 44247
15171518
df = DataFrame([[1], [2], [3], [4], [5]])
15181519
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):
15201521
ctx = df.style.hide([(0, 0), (0, 1)], axis="index")._translate(True, True)
15211522
assert len(ctx["body"]) == 3
15221523

@@ -1538,7 +1539,7 @@ def test_row_trimming_hide_index_mi():
15381539
def test_col_trimming_hide_columns():
15391540
# gh 44272
15401541
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):
15421543
ctx = df.style.hide([0, 1], axis="columns")._translate(True, True)
15431544

15441545
assert len(ctx["head"][0]) == 6 # blank, [0, 1 (hidden)], [2 ,3 (visible)], + trim

0 commit comments

Comments
 (0)