Skip to content

Series.combine() with a scalar only works if function is compatible with (vec, scalar) operation #21248

Closed
@Dr-Irv

Description

@Dr-Irv

Code Sample, a copy-pastable example if possible

In [1]: import pandas as pd

In [2]: s = pd.Series([i*10 for i in range(5)])
   ...: s
   ...:
Out[2]:
0     0
1    10
2    20
3    30
4    40
dtype: int64

In [3]: s.combine(3, lambda x,y: x + y)
Out[3]:
0     3
1    13
2    23
3    33
4    43
dtype: int64

In [4]: s.combine(22, lambda x,y: min(x,y))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-b2ac1f5a4fa4> in <module>()
----> 1 s.combine(22, lambda x,y: min(x,y))

C:\Anaconda3\lib\site-packages\pandas\core\series.py in combine(self, other, func, fill_value)
   2240             new_index = self.index
   2241             with np.errstate(all='ignore'):
-> 2242                 new_values = func(self._values, other)
   2243             new_name = self.name
   2244         return self._constructor(new_values, index=new_index, name=new_name)

<ipython-input-4-b2ac1f5a4fa4> in <lambda>(x, y)
----> 1 s.combine(22, lambda x,y: min(x,y))

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

In [5]:  s.combine(pd.Series([22,22,22,22,22]), lambda x,y: min(x,y))
Out[5]:
0     0
1    10
2    20
3    22
4    22
dtype: int64

Problem description

In the case of using Series.combine() with a scalar argument, it only works if the corresponding function func supports func(Series, scalar). Implementation should always use an element-by-element implementation. The results of [3] and [5] are expected, but [4] should still work.

Expected Output

For [4], it should look like the output of [5] above.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.4.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.0
pytest: 3.3.2
pip: 9.0.1
setuptools: 38.4.0
Cython: 0.27.3
numpy: 1.14.0
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.6.6
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: 2.1.2
openpyxl: 2.4.10
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.1
pymysql: 0.7.11.None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    ExtensionArrayExtending pandas with custom dtypes or arrays.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions