Skip to content

BUG: apply for series of numpy arrays falis with np functions #33492

Open
@Aroksak

Description

@Aroksak
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd
import numpy as np

df = pd.DataFrame({"col": [np.array([.1, .2], dtype=float)]})
np.sin(df['col'].iloc[0])   # This works fine
df["col"].apply(np.sin)     # This breaks

Raises:

AttributeError: 'numpy.ndarray' object has no attribute 'sin'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "~/main.py", line 6, in <module>
    df["col"].apply(np.sin)
  File "~/anaconda3/envs/pandas-issue/lib/python3.7/site-packages/pandas/core/series.py", line 3840, in apply
    return f(self)
  File "~/anaconda3/envs/pandas-issue/lib/python3.7/site-packages/pandas/core/series.py", line 679, in __array_ufunc__
    result = getattr(ufunc, method)(*inputs, **kwargs)
TypeError: loop of ufunc does not support argument 0 of type numpy.ndarray which has no callable sin method

Problem description

When I have a pd.Series where each row is a numerical numpy array, I can't apply numpy functions to them, I tried np.sin, np.cos, np.exp, but I think it's a general problem. I can apply them to individual rows manually and it works fine.

Moreover, If I import tqdm with tqdm.pandas and use progress_apply instead of apply it also works just fine!

Expected Output

0    [0.09983341664682815, 0.19866933079506122]
Name: col, dtype: object

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.3.0-46-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.3
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.3.post20200325
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    ApplyApply, Aggregate, Transform, MapBugNested DataData where the values are collections (lists, sets, dicts, objects, etc.).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions