question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: AttributeError: 'float' object has no attribute 'shape'

See original GitHub issue

Describe your issue.

I tried to plot QQ plot by using stats.probplot, but raised AttributeError.

image

Reproducing Code Example

stats.probplot(
    df['col'], 
    dist="norm", 
    plot=pylab
    )
pylab.show()

Error message

AttributeError: 'float' object has no attribute 'shape'

SciPy/NumPy/Python version information

1.7.3 1.21.6 sys.version_info(major=3, minor=7, micro=15, releaselevel=‘final’, serial=0)

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
WarrenWeckessercommented, Nov 20, 2022

Almost certainly, the data type of the data in the Pandas DataFrame create by bigquery is object. I can recreate the problem with a numpy array with data type object:

In [140]: from scipy.stats import probplot

In [141]: a = np.arange(20.0).astype(object)

In [142]: probplot(a, dist='norm')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
[... snip ...]
File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/lib/function_base.py:554, in average(a, axis, weights, returned, keepdims)
    550     avg = np.multiply(a, wgt,
    551                       dtype=result_dtype).sum(axis, **keepdims_kw) / scl
    553 if returned:
--> 554     if scl.shape != avg.shape:
    555         scl = np.broadcast_to(scl, avg.shape).copy()
    556     return avg, scl

AttributeError: 'float' object has no attribute 'shape'

Try passing df['col'].to_numpy(dtype=float) to probplot.

0reactions
bhaskoro-muthoharcommented, Nov 20, 2022

Almost certainly, the data type of the data in the Pandas DataFrame create by bigquery is object. I can recreate the problem with a numpy array with data type object:

In [140]: from scipy.stats import probplot

In [141]: a = np.arange(20.0).astype(object)

In [142]: probplot(a, dist='norm')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
[... snip ...]
File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/lib/function_base.py:554, in average(a, axis, weights, returned, keepdims)
    550     avg = np.multiply(a, wgt,
    551                       dtype=result_dtype).sum(axis, **keepdims_kw) / scl
    553 if returned:
--> 554     if scl.shape != avg.shape:
    555         scl = np.broadcast_to(scl, avg.shape).copy()
    556     return avg, scl

AttributeError: 'float' object has no attribute 'shape'

Try passing df['col'].to_numpy(dtype=float) to probplot.

Yess, it’s worked like a charm! Thanks!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy.cov() exception: 'float' object has no attribute 'shape'
The error is reproducible if the array is of dtype=object : ... AttributeError: 'float' object has no attribute 'shape'.
Read more >
[BUG] 'float' object has no attribute 'shape' in Data pipelines
I think the issue here is that the shape has been set to (1,) which is why it expects a numpy array. If...
Read more >
AttributeError: 'float' object has no attribute 'X' (Python)
The Python "AttributeError: 'float' object has no attribute" occurs when we try to access an attribute that doesn't exist on a floating-point ...
Read more >
Python – AttributeError: 'float' object has no attribute 'shape'
I'm trying to get the solution(W2) of a equation, which include np. cov, using python sympy. Solvers, but get a AttributeError: 'float' object...
Read more >
'float' object has no attribute 'shape' when using .corr-pandas
I get an error: 'float' object has no attribute 'shape'. If I import the stats library and try: corr, pval=stats.pearsonr(col1,col2).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found