Skip to content

REF: Styler.applymap -> map #52708

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 9 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions doc/source/reference/style.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ Style application
:toctree: api/

Styler.apply
Styler.applymap
Styler.map
Styler.apply_index
Styler.applymap_index
Styler.map_index
Styler.format
Styler.format_index
Styler.relabel_index
Expand Down
53 changes: 26 additions & 27 deletions doc/source/user_guide/style.ipynb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -352,15 +351,15 @@
"\n",
"- Using [.set_table_styles()][table] to control broader areas of the table with specified internal CSS. Although table styles allow the flexibility to add CSS selectors and properties controlling all individual parts of the table, they are unwieldy for individual cell specifications. Also, note that table styles cannot be exported to Excel. \n",
"- Using [.set_td_classes()][td_class] to directly link either external CSS classes to your data cells or link the internal CSS classes created by [.set_table_styles()][table]. See [here](#Setting-Classes-and-Linking-to-External-CSS). These cannot be used on column header rows or indexes, and also won't export to Excel. \n",
"- Using the [.apply()][apply] and [.applymap()][applymap] functions to add direct internal CSS to specific data cells. See [here](#Styler-Functions). As of v1.4.0 there are also methods that work directly on column header rows or indexes; [.apply_index()][applyindex] and [.applymap_index()][applymapindex]. Note that only these methods add styles that will export to Excel. These methods work in a similar way to [DataFrame.apply()][dfapply] and [DataFrame.map()][dfmap].\n",
"- Using the [.apply()][apply] and [.map()][map] functions to add direct internal CSS to specific data cells. See [here](#Styler-Functions). As of v1.4.0 there are also methods that work directly on column header rows or indexes; [.apply_index()][applyindex] and [.map_index()][mapindex]. Note that only these methods add styles that will export to Excel. These methods work in a similar way to [DataFrame.apply()][dfapply] and [DataFrame.map()][dfmap].\n",
"\n",
"[table]: ../reference/api/pandas.io.formats.style.Styler.set_table_styles.rst\n",
"[styler]: ../reference/api/pandas.io.formats.style.Styler.rst\n",
"[td_class]: ../reference/api/pandas.io.formats.style.Styler.set_td_classes.rst\n",
"[apply]: ../reference/api/pandas.io.formats.style.Styler.apply.rst\n",
"[applymap]: ../reference/api/pandas.io.formats.style.Styler.applymap.rst\n",
"[map]: ../reference/api/pandas.io.formats.style.Styler.map.rst\n",
"[applyindex]: ../reference/api/pandas.io.formats.style.Styler.apply_index.rst\n",
"[applymapindex]: ../reference/api/pandas.io.formats.style.Styler.applymap_index.rst\n",
"[mapindex]: ../reference/api/pandas.io.formats.style.Styler.map_index.rst\n",
"[dfapply]: ../reference/api/pandas.DataFrame.apply.rst\n",
"[dfmap]: ../reference/api/pandas.DataFrame.map.rst"
]
Expand Down Expand Up @@ -565,13 +564,13 @@
"\n",
"We use the following methods to pass your style functions. Both of those methods take a function (and some other keyword arguments) and apply it to the DataFrame in a certain way, rendering CSS styles.\n",
"\n",
"- [.applymap()][applymap] (elementwise): accepts a function that takes a single value and returns a string with the CSS attribute-value pair.\n",
"- [.map()][map] (elementwise): accepts a function that takes a single value and returns a string with the CSS attribute-value pair.\n",
"- [.apply()][apply] (column-/row-/table-wise): accepts a function that takes a Series or DataFrame and returns a Series, DataFrame, or numpy array with an identical shape where each element is a string with a CSS attribute-value pair. This method passes each column or row of your DataFrame one-at-a-time or the entire table at once, depending on the `axis` keyword argument. For columnwise use `axis=0`, rowwise use `axis=1`, and for the entire table at once use `axis=None`.\n",
"\n",
"This method is powerful for applying multiple, complex logic to data cells. We create a new DataFrame to demonstrate this.\n",
"\n",
"[apply]: ../reference/api/pandas.io.formats.style.Styler.apply.rst\n",
"[applymap]: ../reference/api/pandas.io.formats.style.Styler.applymap.rst"
"[map]: ../reference/api/pandas.io.formats.style.Styler.map.rst"
]
},
{
Expand All @@ -589,7 +588,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"For example we can build a function that colors text if it is negative, and chain this with a function that partially fades cells of negligible value. Since this looks at each element in turn we use ``applymap``."
"For example we can build a function that colors text if it is negative, and chain this with a function that partially fades cells of negligible value. Since this looks at each element in turn we use ``map``."
]
},
{
Expand All @@ -600,8 +599,8 @@
"source": [
"def style_negative(v, props=''):\n",
" return props if v < 0 else None\n",
"s2 = df2.style.applymap(style_negative, props='color:red;')\\\n",
" .applymap(lambda v: 'opacity: 20%;' if (v < 0.3) and (v > -0.3) else None)\n",
"s2 = df2.style.map(style_negative, props='color:red;')\\\n",
" .map(lambda v: 'opacity: 20%;' if (v < 0.3) and (v > -0.3) else None)\n",
"s2"
]
},
Expand Down Expand Up @@ -699,13 +698,13 @@
"\n",
"Similar application is achieved for headers by using:\n",
" \n",
"- [.applymap_index()][applymapindex] (elementwise): accepts a function that takes a single value and returns a string with the CSS attribute-value pair.\n",
"- [.map_index()][mapindex] (elementwise): accepts a function that takes a single value and returns a string with the CSS attribute-value pair.\n",
"- [.apply_index()][applyindex] (level-wise): accepts a function that takes a Series and returns a Series, or numpy array with an identical shape where each element is a string with a CSS attribute-value pair. This method passes each level of your Index one-at-a-time. To style the index use `axis=0` and to style the column headers use `axis=1`.\n",
"\n",
"You can select a `level` of a `MultiIndex` but currently no similar `subset` application is available for these methods.\n",
"\n",
"[applyindex]: ../reference/api/pandas.io.formats.style.Styler.apply_index.rst\n",
"[applymapindex]: ../reference/api/pandas.io.formats.style.Styler.applymap_index.rst"
"[mapindex]: ../reference/api/pandas.io.formats.style.Styler.map_index.rst"
]
},
{
Expand All @@ -714,7 +713,7 @@
"metadata": {},
"outputs": [],
"source": [
"s2.applymap_index(lambda v: \"color:pink;\" if v>4 else \"color:darkblue;\", axis=0)\n",
"s2.map_index(lambda v: \"color:pink;\" if v>4 else \"color:darkblue;\", axis=0)\n",
"s2.apply_index(lambda s: np.where(s.isin([\"A\", \"B\"]), \"color:pink;\", \"color:darkblue;\"), axis=1)"
]
},
Expand Down Expand Up @@ -831,7 +830,7 @@
"source": [
"## Finer Control with Slicing\n",
"\n",
"The examples we have shown so far for the `Styler.apply` and `Styler.applymap` functions have not demonstrated the use of the ``subset`` argument. This is a useful argument which permits a lot of flexibility: it allows you to apply styles to specific rows or columns, without having to code that logic into your `style` function.\n",
"The examples we have shown so far for the `Styler.apply` and `Styler.map` functions have not demonstrated the use of the ``subset`` argument. This is a useful argument which permits a lot of flexibility: it allows you to apply styles to specific rows or columns, without having to code that logic into your `style` function.\n",
"\n",
"The value passed to `subset` behaves similar to slicing a DataFrame;\n",
"\n",
Expand Down Expand Up @@ -1034,7 +1033,7 @@
"outputs": [],
"source": [
"props = 'font-family: \"Times New Roman\", Times, serif; color: #e83e8c; font-size:1.3em;'\n",
"df4.style.applymap(lambda x: props, subset=[1])"
"df4.style.map(lambda x: props, subset=[1])"
]
},
{
Expand Down Expand Up @@ -1321,7 +1320,7 @@
"source": [
"### Set properties\n",
"\n",
"Use `Styler.set_properties` when the style doesn't actually depend on the values. This is just a simple wrapper for `.applymap` where the function returns the same properties for all cells."
"Use `Styler.set_properties` when the style doesn't actually depend on the values. This is just a simple wrapper for `.map` where the function returns the same properties for all cells."
]
},
{
Expand Down Expand Up @@ -1465,8 +1464,8 @@
"outputs": [],
"source": [
"style1 = df2.style\\\n",
" .applymap(style_negative, props='color:red;')\\\n",
" .applymap(lambda v: 'opacity: 20%;' if (v < 0.3) and (v > -0.3) else None)\\\n",
" .map(style_negative, props='color:red;')\\\n",
" .map(lambda v: 'opacity: 20%;' if (v < 0.3) and (v > -0.3) else None)\\\n",
" .set_table_styles([{\"selector\": \"th\", \"props\": \"color: blue;\"}])\\\n",
" .hide(axis=\"index\")\n",
"style1"
Expand Down Expand Up @@ -1683,7 +1682,7 @@
" - `number-format`\n",
" - `border-style` (for Excel-specific styles: \"hair\", \"mediumDashDot\", \"dashDotDot\", \"mediumDashDotDot\", \"dashDot\", \"slantDashDot\", or \"mediumDashed\")\n",
"\n",
"Table level styles, and data cell CSS-classes are not included in the export to Excel: individual cells must have their properties mapped by the `Styler.apply` and/or `Styler.applymap` methods."
"Table level styles, and data cell CSS-classes are not included in the export to Excel: individual cells must have their properties mapped by the `Styler.apply` and/or `Styler.map` methods."
]
},
{
Expand All @@ -1693,7 +1692,7 @@
"outputs": [],
"source": [
"df2.style.\\\n",
" applymap(style_negative, props='color:red;').\\\n",
" map(style_negative, props='color:red;').\\\n",
" highlight_max(axis=0).\\\n",
" to_excel('styled.xlsx', engine='openpyxl')"
]
Expand Down Expand Up @@ -1783,8 +1782,8 @@
"outputs": [],
"source": [
"df4 = pd.DataFrame([['text']])\n",
"df4.style.applymap(lambda x: 'color:green;')\\\n",
" .applymap(lambda x: 'color:red;')"
"df4.style.map(lambda x: 'color:green;')\\\n",
" .map(lambda x: 'color:red;')"
]
},
{
Expand All @@ -1793,8 +1792,8 @@
"metadata": {},
"outputs": [],
"source": [
"df4.style.applymap(lambda x: 'color:red;')\\\n",
" .applymap(lambda x: 'color:green;')"
"df4.style.map(lambda x: 'color:red;')\\\n",
" .map(lambda x: 'color:green;')"
]
},
{
Expand All @@ -1821,7 +1820,7 @@
"source": [
"df4.style.set_uuid('a_')\\\n",
" .set_table_styles([{'selector': 'td', 'props': 'color:red;'}])\\\n",
" .applymap(lambda x: 'color:green;')"
" .map(lambda x: 'color:green;')"
]
},
{
Expand All @@ -1840,7 +1839,7 @@
"df4.style.set_uuid('b_')\\\n",
" .set_table_styles([{'selector': 'td', 'props': 'color:red;'},\n",
" {'selector': '.cls-1', 'props': 'color:blue;'}])\\\n",
" .applymap(lambda x: 'color:green;')\\\n",
" .map(lambda x: 'color:green;')\\\n",
" .set_td_classes(pd.DataFrame([['cls-1']]))"
]
},
Expand All @@ -1861,7 +1860,7 @@
" .set_table_styles([{'selector': 'td', 'props': 'color:red;'},\n",
" {'selector': '.cls-1', 'props': 'color:blue;'},\n",
" {'selector': 'td.data', 'props': 'color:yellow;'}])\\\n",
" .applymap(lambda x: 'color:green;')\\\n",
" .map(lambda x: 'color:green;')\\\n",
" .set_td_classes(pd.DataFrame([['cls-1']]))"
]
},
Expand All @@ -1884,7 +1883,7 @@
" .set_table_styles([{'selector': 'td', 'props': 'color:red;'},\n",
" {'selector': '.cls-1', 'props': 'color:blue;'},\n",
" {'selector': 'td.data', 'props': 'color:yellow;'}])\\\n",
" .applymap(lambda x: 'color:green !important;')\\\n",
" .map(lambda x: 'color:green !important;')\\\n",
" .set_td_classes(pd.DataFrame([['cls-1']]))"
]
},
Expand Down
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ Deprecations
- Deprecated :func:`is_interval_dtype`, check ``isinstance(dtype, pd.IntervalDtype)`` instead (:issue:`52607`)
- Deprecated :func:`is_period_dtype`, check ``isinstance(dtype, pd.PeriodDtype)`` instead (:issue:`52642`)
- Deprecated :func:`is_sparse`, check ``isinstance(dtype, pd.SparseDtype)`` instead (:issue:`52642`)
- Deprecated :meth:`.Styler.applymap_index`. Use the new :meth:`.Styler.map_index` method instead (:issue:`52708`)
- Deprecated :meth:`.Styler.applymap`. Use the new :meth:`.Styler.map` method instead (:issue:`52708`)
- Deprecated :meth:`DataFrame.applymap`. Use the new :meth:`DataFrame.map` method instead (:issue:`52353`)
- Deprecated :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes`, use :meth:`DataFrame.transpose` or :meth:`Series.transpose` instead (:issue:`51946`)
- Deprecated ``freq`` parameter in :class:`PeriodArray` constructor, pass ``dtype`` instead (:issue:`52462`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3543,7 +3543,7 @@ def _to_latex_via_styler(
# bold_rows is not a direct kwarg of Styler.to_latex
render_kwargs = {} if render_kwargs is None else render_kwargs
if render_kwargs.pop("bold_rows"):
styler.applymap_index(lambda v: "textbf:--rwrap;")
styler.map_index(lambda v: "textbf:--rwrap;")

return styler.to_latex(buf=buf, **render_kwargs)

Expand Down
Loading