8000 ENH: make `Styler` compatible with non-unique indexes by attack68 · Pull Request #41269 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

ENH: make Styler compatible with non-unique indexes #41269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1cc569f
ENH: make `Styler.format` compatible with non-unique indexes (with Te…
attack68 May 2, 2021
6982554
ENH: error when using a non-unique subset with .apply and .applymap (…
attack68 May 2, 2021
a3694db
ENH: error when using a non-unique subset with .apply and .applymap: …
attack68 May 2, 2021
5c6669c
ENH: make table_styles work with non-unique + TST: refactor to own file
attack68 May 2, 2021
732c7d5
ENH: error catching
attack68 May 2, 2021
4c99130
ENH: error catching
attack68 May 2, 2021
57e8bef
Merge remote-tracking branch 'upstream/master' into styler_non_unique
attack68 May 3, 2021
a7a2966
ENH: deal with tooltips and raise (inc Tests)
attack68 May 3, 2021
19fb7f9
ENH: deal with tooltips and raise (inc Tests)
attack68 May 3, 2021
4ce559e
ENH: deal with tset_td_classes and raise (inc Tests)
attack68 May 3, 2021
7f28111
ENH: tests for hide_columns
attack68 May 3, 2021
5043c01
ENH: remove style ValueError
attack68 May 3, 2021
9fc6cd3
Merge remote-tracking branch 'upstream/master' into styler_non_unique
attack68 May 4, 2021
09764ba
whats new
attack68 May 4, 2021
9451aae
Merge remote-tracking branch 'upstream/master' into styler_non_unique
attack68 May 5, 2021
4faeb29
prohibit apply and applymap in non-unique case
attack68 May 5, 2021
aed0536
Merge remote-tracking branch 'upstream/master' into styler_non_unique
attack68 May 6, 2021
3a8f11e
move outside loop
attack68 May 6, 2021
51233be
create conditional for performance
attack68 May 6, 2021
8454c5e
create conditional for performance
attack68 May 6, 2021
c3b7af8
take indexing out of loops
attack68 May 6, 2021
20cd19f
take indexing out of loops
attack68 May 6, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
Prev Previous commit
Next Next commit
prohibit apply and applymap in non-unique case
  • Loading branch information
attack68 committed May 5, 2021
commit 4faeb29e1bc2085231bfa6915760fee9898335fc
29 changes: 11 additions & 18 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,26 +472,19 @@ def _update_ctx(self, attrs: DataFrame) -> None:
Whitespace shouldn't matter and the final trailing ';' shouldn't
matter.
"""
err = KeyError(
"`Styler.apply` and `.applymap` are not compatible with subset slices "
"containing non-unique index or column keys."
)
if not self.index.is_unique or not self.columns.is_unique:
raise KeyError(
"`Styler.apply` and `.applymap` are not compatible "
"with non-unique index or columns."
)

for cn in attrs.columns:
try:
for rn, c in attrs[[cn]].itertuples():
if not c:
continue
css_list = maybe_convert_css_to_tuples(c)
i, j = self.index.get_loc(rn), self.columns.get_loc(cn)
self.ctx[(i, j)].extend(css_list)
except ValueError as ve:
if "Styles supplied as string must follow CSS rule formats" in str(ve):
raise ve # error is from maybe_convert_css_to_tuples()
else:
raise err # 'too many values to unpack': caught non-unique column
except TypeError:
raise err # 'unhashable type: slice' caught a non-unique index
for rn, c in attrs[[cn]].itertuples():
if not c:
continue
css_list = maybe_convert_css_to_tuples(c)
i, j = self.index.get_loc(rn), self.columns.get_loc(cn)
self.ctx[(i, j)].extend(css_list)

def _copy(self, deepcopy: bool = False) -> Styler:
styler = Styler(
Expand Down
16 changes: 1 addition & 15 deletions pandas/tests/io/formats/style/test_non_unique.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,7 @@ def test_apply_applymap_non_unique_raises(df, func):
op = lambda v: "color: red;"

with pytest.raises(KeyError, match="`Styler.apply` and `.applymap` are not"):
# slice is non-unique on columns
getattr(df.style, func)(op, subset=("i", "d"))._compute()

with pytest.raises(KeyError, match="`Styler.apply` and `.applymap` are not"):
# slice is non-unique on rows
getattr(df.style, func)(op, subset=("j", "c"))._compute()

# unique subset OK
getattr(df.style, func)(op, subset=("i", "c"))._compute()
getattr(df.style, func)(op)._compute()


def test_table_styles_dict_non_unique_index(styler):
Expand All @@ -83,12 +75,6 @@ def test_table_styles_dict_non_unique_columns(styler):
]


def test_maybe_convert_css_raises(styler):
# test _update_ctx() detects the right ValueError where non-unique columns present
with pytest.raises(ValueError, match="Styles supplied as string must follow CSS"):
styler.applymap(lambda x: "bad-css;")._compute()


def test_tooltips_non_unique_raises(styler):
# ttips has unique keys
ttips = DataFrame([["1", "2"], ["3", "4"]], columns=["c", "d"], index=["a", "b"])
Expand Down
0