Skip to content

Commit 7e436ec

Browse files
committed
API: rename DataFrame.applymap -> DataFrame.map
1 parent 0b162f5 commit 7e436ec

File tree

19 files changed

+126
-70
lines changed

19 files changed

+126
-70
lines changed

doc/source/development/roadmap.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ Numba-accelerated operations
179179

180180
`Numba <https://numba.pydata.org>`__ is a JIT compiler for Python code. We'd like to provide
181181
ways for users to apply their own Numba-jitted functions where pandas accepts user-defined functions
182-
(for example, :meth:`Series.apply`, :meth:`DataFrame.apply`, :meth:`DataFrame.applymap`,
182+
(for example, :meth:`Series.apply`, :meth:`DataFrame.apply`, :meth:`DataFrame.map`,
183183
and in groupby and window contexts). This will improve the performance of
184184
user-defined-functions in these operations by staying within compiled code.
185185

doc/source/reference/frame.rst

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ Function application, GroupBy & window
116116
:toctree: api/
117117

118118
DataFrame.apply
119+
DataFrame.map
119120
DataFrame.applymap
120121
DataFrame.pipe
121122
DataFrame.agg

doc/source/user_guide/basics.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ on an entire ``DataFrame`` or ``Series``, row- or column-wise, or elementwise.
768768
1. `Tablewise Function Application`_: :meth:`~DataFrame.pipe`
769769
2. `Row or Column-wise Function Application`_: :meth:`~DataFrame.apply`
770770
3. `Aggregation API`_: :meth:`~DataFrame.agg` and :meth:`~DataFrame.transform`
771-
4. `Applying Elementwise Functions`_: :meth:`~DataFrame.applymap`
771+
4. `Applying Elementwise Functions`_: :meth:`~DataFrame.map`
772772

773773
.. _basics.pipe:
774774

@@ -1180,7 +1180,7 @@ Applying elementwise functions
11801180
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11811181

11821182
Since not all functions can be vectorized (accept NumPy arrays and return
1183-
another array or value), the methods :meth:`~DataFrame.applymap` on DataFrame
1183+
another array or value), the methods :meth:`~DataFrame.map` on DataFrame
11841184
and analogously :meth:`~Series.map` on Series accept any Python function taking
11851185
a single value and returning a single value. For example:
11861186

@@ -1197,7 +1197,7 @@ a single value and returning a single value. For example:
11971197
return len(str(x))
11981198
11991199
df4["one"].map(f)
1200-
df4.applymap(f)
1200+
df4.map(f)
12011201
12021202
:meth:`Series.map` has an additional feature; it can be used to easily
12031203
"link" or "map" values defined by a secondary series. This is closely related

doc/source/user_guide/cookbook.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ Ambiguity arises when an index consists of integers with a non-zero start or non
242242
New columns
243243
***********
244244

245-
`Efficiently and dynamically creating new columns using applymap
245+
`Efficiently and dynamically creating new columns using DataFrame.map (previously named applymap)
246246
<https://stackoverflow.com/questions/16575868/efficiently-creating-additional-columns-in-a-pandas-dataframe-using-map>`__
247247

248248
.. ipython:: python
@@ -254,7 +254,7 @@ New columns
254254
new_cols = [str(x) + "_cat" for x in source_cols]
255255
categories = {1: "Alpha", 2: "Beta", 3: "Charlie"}
256256
257-
df[new_cols] = df[source_cols].applymap(categories.get)
257+
df[new_cols] = df[source_cols].map(categories.get)
258258
df
259259
260260
`Keep other columns when using min() with groupby

doc/source/user_guide/visualization.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1709,7 +1709,7 @@ Colormaps can also be used other plot types, like bar charts:
17091709
17101710
.. ipython:: python
17111711
1712-
dd = pd.DataFrame(np.random.randn(10, 10)).applymap(abs)
1712+
dd = pd.DataFrame(np.random.randn(10, 10)).map(abs)
17131713
dd = dd.cumsum()
17141714
17151715
plt.figure();

doc/source/whatsnew/v2.1.0.rst

+5-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ enhancement1
2525
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2626

2727
When given a callable, :meth:`Series.map` applies the callable to all elements of the ``Series``.
28-
Similarly, :meth:`DataFrame.applymap` applies the callable to all elements of the ``DataFrame``,
28+
Similarly, :meth:`DataFrame.map` applies the callable to all elements of the ``DataFrame``,
2929
while :meth:`Index.map` applies the callable to all elements of the ``Index``.
3030

3131
Frequently, it is not desirable to apply the callable to nan-like values of the array and to avoid doing
@@ -59,10 +59,12 @@ and whether ``na_action="ignore"`` worked with a specific data type was quite un
5959
ser = pd.Series(["a", "b", np.nan], dtype="category")
6060
ser.map(str.upper, na_action="ignore")
6161
df = pd.DataFrame(ser)
62-
df.applymap(str.upper, na_action="ignore")
62+
df.map(str.upper, na_action="ignore")
6363
idx = pd.Index(ser)
6464
idx.map(str.upper, na_action="ignore")
6565
66+
Notice also that :meth:`DataFrame.applymap` in this version has been renamed to :meth:`DataFrame.map` (:issue:`52353`).
67+
6668
.. _whatsnew_210.enhancements.other:
6769

6870
Other enhancements
@@ -166,6 +168,7 @@ Deprecations
166168
- Deprecated logical operations (``|``, ``&``, ``^``) between pandas objects and dtype-less sequences (e.g. ``list``, ``tuple``), wrap a sequence in a :class:`Series` or numpy array before operating instead (:issue:`51521`)
167169
- Deprecated :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes`, use :meth:`DataFrame.transpose` or :meth:`Series.transpose` instead (:issue:`51946`)
168170
- Deprecated parameter ``convert_type`` in :meth:`Series.apply` (:issue:`52140`)
171+
- Deprecated :meth:`DataFrame.applymap`. Use the new :meth:`DataFrame.map` method instead (:issue:`52353`)
169172
-
170173

171174
.. ---------------------------------------------------------------------------

pandas/core/frame.py

+45-4
Original file line numberDiff line numberDiff line change
@@ -9710,7 +9710,7 @@ def apply(
97109710
97119711
See Also
97129712
--------
9713-
DataFrame.applymap: For elementwise operations.
9713+
DataFrame.map: For elementwise operations.
97149714
DataFrame.aggregate: Only perform aggregating type operations.
97159715
DataFrame.transform: Only perform transforming type operations.
97169716
@@ -9802,7 +9802,7 @@ def apply(
98029802
)
98039803
return op.apply().__finalize__(self, method="apply")
98049804

9805-
def applymap(
9805+
def map(
98069806
self, func: PythonFuncType, na_action: str | None = None, **kwargs
98079807
) -> DataFrame:
98089808
"""
@@ -9844,7 +9844,7 @@ def applymap(
98449844
0 1.000 2.120
98459845
1 3.356 4.567
98469846
9847-
>>> df.applymap(lambda x: len(str(x)))
9847+
>>> df.map(lambda x: len(str(x)))
98489848
0 1
98499849
0 3 4
98509850
1 5 5
@@ -9861,7 +9861,7 @@ def applymap(
98619861
Note that a vectorized version of `func` often exists, which will
98629862
be much faster. You could square each number elementwise.
98639863
9864-
>>> df.applymap(lambda x: x**2)
9864+
>>> df.map(lambda x: x**2)
98659865
0 1
98669866
0 1.000000 4.494400
98679867
1 11.262736 20.857489
@@ -9888,6 +9888,47 @@ def infer(x):
98889888

98899889
return self.apply(infer).__finalize__(self, "applymap")
98909890

9891+
def applymap(
9892+
self, func: PythonFuncType, na_action: str | None = None, **kwargs
9893+
) -> DataFrame:
9894+
"""
9895+
Apply a function to a {klass} elementwise.
9896+
9897+
.. deprecated:: 2.1.0
9898+
9899+
DataFrame.applymap has been deprecated. Use DataFrame.map instead.
9900+
9901+
This method applies a function that accepts and returns a scalar
9902+
to every element of a DataFrame.
9903+
9904+
Parameters
9905+
----------
9906+
func : callable
9907+
Python function, returns a single value from a single value.
9908+
na_action : {None, 'ignore'}, default None
9909+
If ‘ignore’, propagate NaN values, without passing them to func.
9910+
**kwargs
9911+
Additional keyword arguments to pass as keywords arguments to
9912+
`func`.
9913+
9914+
Returns
9915+
-------
9916+
DataFrame
9917+
Transformed DataFrame.
9918+
9919+
See Also
9920+
--------
9921+
DataFrame.apply : Apply a function along input axis of DataFrame.
9922+
DataFrame.map : Apply a function along input axis of DataFrame.
9923+
DataFrame.replace: Replace values given in `to_replace` with `value`.
9924+
"""
9925+
warnings.warn(
9926+
"DataFrame.applymap has been deprecated. Use DataFrame.map instead.",
9927+
FutureWarning,
9928+
stacklevel=find_stack_level(),
9929+
)
9930+
return self.map(func, na_action=na_action, **kwargs)
9931+
98919932
# ----------------------------------------------------------------------
98929933
# Merging / joining methods
98939934

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5938,7 +5938,7 @@ def pipe(
59385938
See Also
59395939
--------
59405940
DataFrame.apply : Apply a function along input axis of DataFrame.
5941-
DataFrame.applymap : Apply a function elementwise on a whole DataFrame.
5941+
DataFrame.map : Apply a function elementwise on a whole DataFrame.
59425942
Series.map : Apply a mapping correspondence on a
59435943
:class:`~pandas.Series`.
59445944

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4266,7 +4266,7 @@ def map(
42664266
Series.apply : For applying more complex functions on a Series.
42674267
Series.replace: Replace values given in `to_replace` with `value`.
42684268
DataFrame.apply : Apply a function row-/column-wise.
4269-
DataFrame.applymap : Apply a function elementwise on a whole DataFrame.
4269+
DataFrame.map : Apply a function elementwise on a whole DataFrame.
42704270
42714271
Notes
42724272
-----

pandas/core/shared_docs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@
606606
--------
607607
{klass}.fillna : Fill NA values.
608608
{klass}.where : Replace values based on boolean condition.
609-
DataFrame.applymap: Apply a function to a Dataframe elementwise.
609+
DataFrame.map: Apply a function to a Dataframe elementwise.
610610
Series.map: Map values of Series according to an input mapping or function.
611611
Series.str.replace : Simple string replacement.
612612

pandas/io/formats/style.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1813,7 +1813,7 @@ def _apply_index(
18131813
if method == "apply":
18141814
result = data.apply(func, axis=0, **kwargs)
18151815
elif method == "applymap":
1816-
result = data.applymap(func, **kwargs)
1816+
result = data.map(func, **kwargs)
18171817

18181818
self._update_ctx_header(result, axis)
18191819
return self
@@ -1938,7 +1938,7 @@ def _applymap(
19381938
if subset is None:
19391939
subset = IndexSlice[:]
19401940
subset = non_reducing_slice(subset)
1941-
result = self.data.loc[subset].applymap(func)
1941+
result = self.data.loc[subset].map(func)
19421942
self._update_ctx(result)
19431943
return self
19441944

pandas/io/json/_json.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def __init__(
365365
obj = obj.copy()
366366
timedeltas = obj.select_dtypes(include=["timedelta"]).columns
367367
if len(timedeltas):
368-
obj[timedeltas] = obj[timedeltas].applymap(lambda x: x.isoformat())
368+
obj[timedeltas] = obj[timedeltas].map(lambda x: x.isoformat())
369369
# Convert PeriodIndex to datetimes before serializing
370370
if isinstance(obj.index.dtype, PeriodDtype):
371371
obj.index = obj.index.to_timestamp()

0 commit comments

Comments
 (0)