Skip to content

Commit 196775a

Browse files
authored
REF: Styler.applymap -> map (#52708)
* REF: Styler.applymap -> map * update tests to new names with deprecation warnings * whats new * excel tests fixed * Fix the UserGuide style.ipynb * Additional applymap change * add whatsnew back after merge --------- Co-authored-by: JHM Darbyshire (iMac) <attack68@users.noreply.github.com>
1 parent f10e3fc commit 196775a

File tree

12 files changed

+178
-112
lines changed

12 files changed

+178
-112
lines changed

doc/source/reference/style.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ Style application
3636
:toctree: api/
3737

3838
Styler.apply
39-
Styler.applymap
39+
Styler.map
4040
Styler.apply_index
41-
Styler.applymap_index
41+
Styler.map_index
4242
Styler.format
4343
Styler.format_index
4444
Styler.relabel_index

doc/source/user_guide/style.ipynb

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"cells": [
33
{
4-
"attachments": {},
54
"cell_type": "markdown",
65
"metadata": {},
76
"source": [
@@ -352,15 +351,15 @@
352351
"\n",
353352
"- 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",
354353
"- 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",
355-
"- 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",
354+
"- 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",
356355
"\n",
357356
"[table]: ../reference/api/pandas.io.formats.style.Styler.set_table_styles.rst\n",
358357
"[styler]: ../reference/api/pandas.io.formats.style.Styler.rst\n",
359358
"[td_class]: ../reference/api/pandas.io.formats.style.Styler.set_td_classes.rst\n",
360359
"[apply]: ../reference/api/pandas.io.formats.style.Styler.apply.rst\n",
361-
"[applymap]: ../reference/api/pandas.io.formats.style.Styler.applymap.rst\n",
360+
"[map]: ../reference/api/pandas.io.formats.style.Styler.map.rst\n",
362361
"[applyindex]: ../reference/api/pandas.io.formats.style.Styler.apply_index.rst\n",
363-
"[applymapindex]: ../reference/api/pandas.io.formats.style.Styler.applymap_index.rst\n",
362+
"[mapindex]: ../reference/api/pandas.io.formats.style.Styler.map_index.rst\n",
364363
"[dfapply]: ../reference/api/pandas.DataFrame.apply.rst\n",
365364
"[dfmap]: ../reference/api/pandas.DataFrame.map.rst"
366365
]
@@ -565,13 +564,13 @@
565564
"\n",
566565
"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",
567566
"\n",
568-
"- [.applymap()][applymap] (elementwise): accepts a function that takes a single value and returns a string with the CSS attribute-value pair.\n",
567+
"- [.map()][map] (elementwise): accepts a function that takes a single value and returns a string with the CSS attribute-value pair.\n",
569568
"- [.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",
570569
"\n",
571570
"This method is powerful for applying multiple, complex logic to data cells. We create a new DataFrame to demonstrate this.\n",
572571
"\n",
573572
"[apply]: ../reference/api/pandas.io.formats.style.Styler.apply.rst\n",
574-
"[applymap]: ../reference/api/pandas.io.formats.style.Styler.applymap.rst"
573+
"[map]: ../reference/api/pandas.io.formats.style.Styler.map.rst"
575574
]
576575
},
577576
{
@@ -589,7 +588,7 @@
589588
"cell_type": "markdown",
590589
"metadata": {},
591590
"source": [
592-
"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``."
591+
"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``."
593592
]
594593
},
595594
{
@@ -600,8 +599,8 @@
600599
"source": [
601600
"def style_negative(v, props=''):\n",
602601
" return props if v < 0 else None\n",
603-
"s2 = df2.style.applymap(style_negative, props='color:red;')\\\n",
604-
" .applymap(lambda v: 'opacity: 20%;' if (v < 0.3) and (v > -0.3) else None)\n",
602+
"s2 = df2.style.map(style_negative, props='color:red;')\\\n",
603+
" .map(lambda v: 'opacity: 20%;' if (v < 0.3) and (v > -0.3) else None)\n",
605604
"s2"
606605
]
607606
},
@@ -699,13 +698,13 @@
699698
"\n",
700699
"Similar application is achieved for headers by using:\n",
701700
" \n",
702-
"- [.applymap_index()][applymapindex] (elementwise): accepts a function that takes a single value and returns a string with the CSS attribute-value pair.\n",
701+
"- [.map_index()][mapindex] (elementwise): accepts a function that takes a single value and returns a string with the CSS attribute-value pair.\n",
703702
"- [.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",
704703
"\n",
705704
"You can select a `level` of a `MultiIndex` but currently no similar `subset` application is available for these methods.\n",
706705
"\n",
707706
"[applyindex]: ../reference/api/pandas.io.formats.style.Styler.apply_index.rst\n",
708-
"[applymapindex]: ../reference/api/pandas.io.formats.style.Styler.applymap_index.rst"
707+
"[mapindex]: ../reference/api/pandas.io.formats.style.Styler.map_index.rst"
709708
]
710709
},
711710
{
@@ -714,7 +713,7 @@
714713
"metadata": {},
715714
"outputs": [],
716715
"source": [
717-
"s2.applymap_index(lambda v: \"color:pink;\" if v>4 else \"color:darkblue;\", axis=0)\n",
716+
"s2.map_index(lambda v: \"color:pink;\" if v>4 else \"color:darkblue;\", axis=0)\n",
718717
"s2.apply_index(lambda s: np.where(s.isin([\"A\", \"B\"]), \"color:pink;\", \"color:darkblue;\"), axis=1)"
719718
]
720719
},
@@ -831,7 +830,7 @@
831830
"source": [
832831
"## Finer Control with Slicing\n",
833832
"\n",
834-
"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",
833+
"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",
835834
"\n",
836835
"The value passed to `subset` behaves similar to slicing a DataFrame;\n",
837836
"\n",
@@ -1034,7 +1033,7 @@
10341033
"outputs": [],
10351034
"source": [
10361035
"props = 'font-family: \"Times New Roman\", Times, serif; color: #e83e8c; font-size:1.3em;'\n",
1037-
"df4.style.applymap(lambda x: props, subset=[1])"
1036+
"df4.style.map(lambda x: props, subset=[1])"
10381037
]
10391038
},
10401039
{
@@ -1321,7 +1320,7 @@
13211320
"source": [
13221321
"### Set properties\n",
13231322
"\n",
1324-
"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."
1323+
"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."
13251324
]
13261325
},
13271326
{
@@ -1465,8 +1464,8 @@
14651464
"outputs": [],
14661465
"source": [
14671466
"style1 = df2.style\\\n",
1468-
" .applymap(style_negative, props='color:red;')\\\n",
1469-
" .applymap(lambda v: 'opacity: 20%;' if (v < 0.3) and (v > -0.3) else None)\\\n",
1467+
" .map(style_negative, props='color:red;')\\\n",
1468+
" .map(lambda v: 'opacity: 20%;' if (v < 0.3) and (v > -0.3) else None)\\\n",
14701469
" .set_table_styles([{\"selector\": \"th\", \"props\": \"color: blue;\"}])\\\n",
14711470
" .hide(axis=\"index\")\n",
14721471
"style1"
@@ -1683,7 +1682,7 @@
16831682
" - `number-format`\n",
16841683
" - `border-style` (for Excel-specific styles: \"hair\", \"mediumDashDot\", \"dashDotDot\", \"mediumDashDotDot\", \"dashDot\", \"slantDashDot\", or \"mediumDashed\")\n",
16851684
"\n",
1686-
"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."
1685+
"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."
16871686
]
16881687
},
16891688
{
@@ -1693,7 +1692,7 @@
16931692
"outputs": [],
16941693
"source": [
16951694
"df2.style.\\\n",
1696-
" applymap(style_negative, props='color:red;').\\\n",
1695+
" map(style_negative, props='color:red;').\\\n",
16971696
" highlight_max(axis=0).\\\n",
16981697
" to_excel('styled.xlsx', engine='openpyxl')"
16991698
]
@@ -1783,8 +1782,8 @@
17831782
"outputs": [],
17841783
"source": [
17851784
"df4 = pd.DataFrame([['text']])\n",
1786-
"df4.style.applymap(lambda x: 'color:green;')\\\n",
1787-
" .applymap(lambda x: 'color:red;')"
1785+
"df4.style.map(lambda x: 'color:green;')\\\n",
1786+
" .map(lambda x: 'color:red;')"
17881787
]
17891788
},
17901789
{
@@ -1793,8 +1792,8 @@
17931792
"metadata": {},
17941793
"outputs": [],
17951794
"source": [
1796-
"df4.style.applymap(lambda x: 'color:red;')\\\n",
1797-
" .applymap(lambda x: 'color:green;')"
1795+
"df4.style.map(lambda x: 'color:red;')\\\n",
1796+
" .map(lambda x: 'color:green;')"
17981797
]
17991798
},
18001799
{
@@ -1821,7 +1820,7 @@
18211820
"source": [
18221821
"df4.style.set_uuid('a_')\\\n",
18231822
" .set_table_styles([{'selector': 'td', 'props': 'color:red;'}])\\\n",
1824-
" .applymap(lambda x: 'color:green;')"
1823+
" .map(lambda x: 'color:green;')"
18251824
]
18261825
},
18271826
{
@@ -1840,7 +1839,7 @@
18401839
"df4.style.set_uuid('b_')\\\n",
18411840
" .set_table_styles([{'selector': 'td', 'props': 'color:red;'},\n",
18421841
" {'selector': '.cls-1', 'props': 'color:blue;'}])\\\n",
1843-
" .applymap(lambda x: 'color:green;')\\\n",
1842+
" .map(lambda x: 'color:green;')\\\n",
18441843
" .set_td_classes(pd.DataFrame([['cls-1']]))"
18451844
]
18461845
},
@@ -1861,7 +1860,7 @@
18611860
" .set_table_styles([{'selector': 'td', 'props': 'color:red;'},\n",
18621861
" {'selector': '.cls-1', 'props': 'color:blue;'},\n",
18631862
" {'selector': 'td.data', 'props': 'color:yellow;'}])\\\n",
1864-
" .applymap(lambda x: 'color:green;')\\\n",
1863+
" .map(lambda x: 'color:green;')\\\n",
18651864
" .set_td_classes(pd.DataFrame([['cls-1']]))"
18661865
]
18671866
},
@@ -1884,7 +1883,7 @@
18841883
" .set_table_styles([{'selector': 'td', 'props': 'color:red;'},\n",
18851884
" {'selector': '.cls-1', 'props': 'color:blue;'},\n",
18861885
" {'selector': 'td.data', 'props': 'color:yellow;'}])\\\n",
1887-
" .applymap(lambda x: 'color:green !important;')\\\n",
1886+
" .map(lambda x: 'color:green !important;')\\\n",
18881887
" .set_td_classes(pd.DataFrame([['cls-1']]))"
18891888
]
18901889
},

doc/source/whatsnew/v2.1.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ Deprecations
241241
- Deprecated :func:`is_interval_dtype`, check ``isinstance(dtype, pd.IntervalDtype)`` instead (:issue:`52607`)
242242
- Deprecated :func:`is_period_dtype`, check ``isinstance(dtype, pd.PeriodDtype)`` instead (:issue:`52642`)
243243
- Deprecated :func:`is_sparse`, check ``isinstance(dtype, pd.SparseDtype)`` instead (:issue:`52642`)
244+
- Deprecated :meth:`.Styler.applymap_index`. Use the new :meth:`.Styler.map_index` method instead (:issue:`52708`)
245+
- Deprecated :meth:`.Styler.applymap`. Use the new :meth:`.Styler.map` method instead (:issue:`52708`)
244246
- Deprecated :meth:`DataFrame.applymap`. Use the new :meth:`DataFrame.map` method instead (:issue:`52353`)
245247
- Deprecated :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes`, use :meth:`DataFrame.transpose` or :meth:`Series.transpose` instead (:issue:`51946`)
246248
- Deprecated ``freq`` parameter in :class:`PeriodArray` constructor, pass ``dtype`` instead (:issue:`52462`)

pandas/core/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3543,7 +3543,7 @@ def _to_latex_via_styler(
35433543
# bold_rows is not a direct kwarg of Styler.to_latex
35443544
render_kwargs = {} if render_kwargs is None else render_kwargs
35453545
if render_kwargs.pop("bold_rows"):
3546-
styler.applymap_index(lambda v: "textbf:--rwrap;")
3546+
styler.map_index(lambda v: "textbf:--rwrap;")
35473547

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

0 commit comments

Comments
 (0)