Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 of 3 tasks
Aroksak opened this issue Apr 12, 2020 · 2 comments
Open
2 of 3 tasks

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

Aroksak opened this issue Apr 12, 2020 · 2 comments
Labels
Apply Apply, Aggregate, Transform Bug Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).

Comments

@Aroksak
Copy link

Aroksak commented Apr 12, 2020

  • 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

@Aroksak Aroksak added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 12, 2020
@dsaxton
Copy link
Member

dsaxton commented Apr 12, 2020

Hmm, given that this works either by wrapping the ufunc in a lambda or using map I think it likely qualifies as a bug (in the meantime you should be able to use either of these workarounds):

[ins] In [1]: ser = pd.Series([np.array([1, 2])])

[ins] In [2]: ser.map(np.sin)
Out[2]:
0    [0.8414709848078965, 0.9092974268256817]
dtype: object

[ins] In [3]: ser.apply(lambda x: np.sin(x))
Out[3]:
0    [0.8414709848078965, 0.9092974268256817]
dtype: object

@dsaxton dsaxton added Apply Apply, Aggregate, Transform Numeric Operations Arithmetic, Comparison, and Logical operations and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 12, 2020
@sathyz
Copy link
Contributor

sathyz commented Apr 13, 2020

I wouldn't say, the failure is for all numpy functions,

>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame({"col": [np.array([.1, .2], dtype=float), np.array([.3]) ]})
>>> df.col.apply(np.max)
0    0.2
1    0.3
Name: col, dtype: float64
>>>

I suppose, pandas is calling vectorised version of NumPy calls, which too raises the same exception.

>>> np.sin(df.col)
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 "<stdin>", line 1, in <module>
  File "/Users/skmohan 1/Workspace/github.com/pandas/pandas/core/series.py", line 732, 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
>>>

mproszewska added a commit to mproszewska/pandas that referenced this issue Apr 24, 2020
@jbrockmendel jbrockmendel added the Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.). label Sep 23, 2020
@mroeschke mroeschke removed the Numeric Operations Arithmetic, Comparison, and Logical operations label Jul 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform Bug Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).
Projects
None yet
5 participants