Skip to content

Commit 30ca3bd

Browse files
engine with no function and tests
1 parent b5e5519 commit 30ca3bd

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

pandas/core/series.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4340,13 +4340,16 @@ def map(
43404340
If 'ignore', propagate NaN values, without passing them to the
43414341
mapping correspondence.
43424342
engine : decorator, optional
4343-
Choose the execution engine to use. If not provided the function
4344-
will be executed by the regular Python interpreter.
4343+
Choose the execution engine to use to run the function. Only used for
4344+
functions. If ``map`` is called with a mapping or ``Series``, and
4345+
exception will be raised. If ``engine`` is not provided the function will
4346+
be executed by the regular Python interpreter.
43454347
4346-
Other options include JIT compilers such as Numba and Bodo, which in some
4347-
cases can speed up the execution. To use an executor you can provide
4348-
the decorators ``numba.jit``, ``numba.njit`` or ``bodo.jit``. You can
4349-
also provide the decorator with parameters, like ``numba.jit(nogit=True)``.
4348+
Options include JIT compilers such as Numba, Bodo or Blosc2, which in some
4349+
cases can speed up the execution. To use an executor you can provide the
4350+
decorators ``numba.jit``, ``numba.njit``, ``bodo.jit`` or ``blosc2.jit``.
4351+
You can also provide the decorator with parameters, like
4352+
``numba.jit(nogit=True)``.
43504353
43514354
Not all functions can be executed with all execution engines. In general,
43524355
JIT compilers will require type stability in the function (no variable
@@ -4423,8 +4426,12 @@ def map(
44234426
dtype: object
44244427
"""
44254428
if engine is not None:
4429+
if not callable(arg):
4430+
raise ValueError(
4431+
"The engine argument can only be specified when func is a function"
4432+
)
44264433
if not hasattr(engine, "__pandas_udf__"):
4427-
raise ValueError(f"Not a valid engine: {engine}")
4434+
raise ValueError(f"Not a valid engine: {engine!r}")
44284435
result = engine.__pandas_udf__.map(
44294436
data=self,
44304437
func=arg,

pandas/tests/series/methods/test_map.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,20 @@ def f(x):
3232
ser.map(f)
3333

3434

35-
def test_map_callable(datetime_series):
35+
def test_map_callable(datetime_series, engine):
3636
with np.errstate(all="ignore"):
37-
tm.assert_series_equal(datetime_series.map(np.sqrt), np.sqrt(datetime_series))
37+
tm.assert_series_equal(
38+
datetime_series.map(np.sqrt, engine=engine), np.sqrt(datetime_series)
39+
)
3840

3941
# map function element-wise
40-
tm.assert_series_equal(datetime_series.map(math.exp), np.exp(datetime_series))
42+
tm.assert_series_equal(
43+
datetime_series.map(math.exp, engine=engine), np.exp(datetime_series)
44+
)
4145

4246
# empty series
4347
s = Series(dtype=object, name="foo", index=Index([], name="bar"))
44-
rs = s.map(lambda x: x)
48+
rs = s.map(lambda x: x, engine=engine)
4549
tm.assert_series_equal(s, rs)
4650

4751
# check all metadata (GH 9322)
@@ -52,7 +56,7 @@ def test_map_callable(datetime_series):
5256

5357
# index but no data
5458
s = Series(index=[1, 2, 3], dtype=np.float64)
55-
rs = s.map(lambda x: x)
59+
rs = s.map(lambda x: x, engine=engine)
5660
tm.assert_series_equal(s, rs)
5761

5862

@@ -269,10 +273,10 @@ def test_map_decimal(string_series):
269273
assert isinstance(result.iloc[0], Decimal)
270274

271275

272-
def test_map_na_exclusion():
276+
def test_map_na_exclusion(engine):
273277
s = Series([1.5, np.nan, 3, np.nan, 5])
274278

275-
result = s.map(lambda x: x * 2, na_action="ignore")
279+
result = s.map(lambda x: x * 2, na_action="ignore", engine=engine)
276280
exp = s * 2
277281
tm.assert_series_equal(result, exp)
278282

@@ -604,3 +608,23 @@ def test_map_kwargs():
604608
result = Series([2, 4, 5]).map(lambda x, y: x + y, y=2)
605609
expected = Series([4, 6, 7])
606610
tm.assert_series_equal(result, expected)
611+
612+
613+
def test_map_engine_no_function():
614+
s = Series([1, 2])
615+
616+
with pytest.raises(ValueError, match="engine argument can only be specified"):
617+
s.map({}, engine="something")
618+
619+
with pytest.raises(ValueError, match="engine argument can only be specified"):
620+
s.map({1: 2}, engine="something")
621+
622+
with pytest.raises(ValueError, match="engine argument can only be specified"):
623+
s.map(Series([3, 4]), engine="something")
624+
625+
626+
def test_map_engine_not_executor():
627+
s = Series([1, 2])
628+
629+
with pytest.raises(ValueError, match="Not a valid engine: 'something'"):
630+
s.map(lambda x: x, engine="something")

0 commit comments

Comments
 (0)