Skip to content

Commit e053982

Browse files
committed
API: rename DataFrame.applymap -> DataFrame.map
1 parent 4c07e07 commit e053982

File tree

19 files changed

+126
-70
lines changed

19 files changed

+126
-70
lines changed

doc/source/development/roadmap.rst

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ on an entire ``DataFrame`` or ``Series``, row- or column-wise, or elementwise.
758758
1. `Tablewise Function Application`_: :meth:`~DataFrame.pipe`
759759
2. `Row or Column-wise Function Application`_: :meth:`~DataFrame.apply`
760760
3. `Aggregation API`_: :meth:`~DataFrame.agg` and :meth:`~DataFrame.transform`
761-
4. `Applying Elementwise Functions`_: :meth:`~DataFrame.applymap`
761+
4. `Applying Elementwise Functions`_: :meth:`~DataFrame.map`
762762

763763
.. _basics.pipe:
764764

@@ -1170,7 +1170,7 @@ Applying elementwise functions
11701170
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11711171

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

@@ -1187,7 +1187,7 @@ a single value and returning a single value. For example:
11871187
return len(str(x))
11881188
11891189
df4["one"].map(f)
1190-
df4.applymap(f)
1190+
df4.map(f)
11911191
11921192
:meth:`Series.map` has an additional feature; it can be used to easily
11931193
"link" or "map" values defined by a secondary series. This is closely related

doc/source/user_guide/cookbook.rst

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 2 deletions
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 :class:`Series`.
28-
Similarly, :meth:`DataFrame.applymap` applies the callable to all elements of the :class:`DataFrame`,
28+
Similarly, :meth:`DataFrame.map` (:meth:`DataFrame.applymap`) applies the callable to all elements of the :class:`DataFrame`,
2929
while :meth:`Index.map` applies the callable to all elements of the :class:`Index`.
3030

3131
Frequently, it is not desirable to apply the callable to nan-like values of the array and to avoid doing
@@ -59,14 +59,16 @@ and ``na_action="ignore"`` did not work correctly for any ``ExtensionArray`` sub
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
6666
Also, note that :meth:`Categorical.map` implicitly has had its ``na_action`` set to ``"ignore"`` by default.
6767
This has been deprecated and will :meth:`Categorical.map` in the future change the default
6868
to ``na_action=None``, like for all the other array types.
6969

70+
Notice also that :meth:`DataFrame.applymap` has been renamed to :meth:`DataFrame.map` (:issue:`52353`).
71+
7072
.. _whatsnew_210.enhancements.other:
7173

7274
Other enhancements
@@ -172,6 +174,7 @@ Deprecations
172174
- Deprecated the methods :meth:`Series.bool` and :meth:`DataFrame.bool` (:issue:`51749`)
173175
- Deprecated :meth:`DataFrame.swapaxes` and :meth:`Series.swapaxes`, use :meth:`DataFrame.transpose` or :meth:`Series.transpose` instead (:issue:`51946`)
174176
- Deprecated parameter ``convert_type`` in :meth:`Series.apply` (:issue:`52140`)
177+
- Deprecated :meth:`DataFrame.applymap`. Use the new :meth:`DataFrame.map` method instead (:issue:`52353`)
175178
-
176179

177180

pandas/core/frame.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9711,7 +9711,7 @@ def apply(
97119711
97129712
See Also
97139713
--------
9714-
DataFrame.applymap: For elementwise operations.
9714+
DataFrame.map: For elementwise operations.
97159715
DataFrame.aggregate: Only perform aggregating type operations.
97169716
DataFrame.transform: Only perform transforming type operations.
97179717
@@ -9803,7 +9803,7 @@ def apply(
98039803
)
98049804
return op.apply().__finalize__(self, method="apply")
98059805

9806-
def applymap(
9806+
def map(
98079807
self, func: PythonFuncType, na_action: str | None = None, **kwargs
98089808
) -> DataFrame:
98099809
"""
@@ -9845,7 +9845,7 @@ def applymap(
98459845
0 1.000 2.120
98469846
1 3.356 4.567
98479847
9848-
>>> df.applymap(lambda x: len(str(x)))
9848+
>>> df.map(lambda x: len(str(x)))
98499849
0 1
98509850
0 3 4
98519851
1 5 5
@@ -9862,7 +9862,7 @@ def applymap(
98629862
Note that a vectorized version of `func` often exists, which will
98639863
be much faster. You could square each number elementwise.
98649864
9865-
>>> df.applymap(lambda x: x**2)
9865+
>>> df.map(lambda x: x**2)
98669866
0 1
98679867
0 1.000000 4.494400
98689868
1 11.262736 20.857489
@@ -9889,6 +9889,47 @@ def infer(x):
98899889

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

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

pandas/core/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5953,7 +5953,7 @@ def pipe(
59535953
See Also
59545954
--------
59555955
DataFrame.apply : Apply a function along input axis of DataFrame.
5956-
DataFrame.applymap : Apply a function elementwise on a whole DataFrame.
5956+
DataFrame.map : Apply a function elementwise on a whole DataFrame.
59575957
Series.map : Apply a mapping correspondence on a
59585958
:class:`~pandas.Series`.
59595959

pandas/core/series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4265,7 +4265,7 @@ def map(
42654265
Series.apply : For applying more complex functions on a Series.
42664266
Series.replace: Replace values given in `to_replace` with `value`.
42674267
DataFrame.apply : Apply a function row-/column-wise.
4268-
DataFrame.applymap : Apply a function elementwise on a whole DataFrame.
4268+
DataFrame.map : Apply a function elementwise on a whole DataFrame.
42694269
42704270
Notes
42714271
-----

pandas/core/shared_docs.py

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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)