hist raises AttributeError: 'AxesSubplot' object has no attribute 'ndim' with a one row DataFrame #10214

Closed
scls19fr opened this Issue May 27, 2015 · 6 comments

Comments

Projects
None yet
3 participants
Contributor

scls19fr commented May 27, 2015

Hello,

this code works fine:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
bins = np.arange(80, 100 + 2, 1)
df = pd.DataFrame({"Name": ["AAA", "BBB"], "ByCol": [1, 2], "Mark": [85, 89]})
df

   ByCol  Mark Name
0      1    85  AAA
1      2    89  BBB

df["Mark"].hist(by=df["ByCol"], bins=bins)
plt.show()

but same code with a one row DataFrame such as

df = pd.DataFrame({"Name": ["AAA"], "ByCol": [1], "Mark": [85]})
df

   ByCol  Mark Name
0      1    85  AAA

raises:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-35-93bcd47d24f2> in <module>()
----> 1 df["Mark"].hist(by=df["Mark"], bins=bins)

//anaconda/lib/python2.7/site-packages/pandas/tools/plotting.pyc in hist_series(self, by, ax, grid, xlabelsize, xrot, ylabelsize, yrot, figsize, bins, **kwds)
   2847                             **kwds)
   2848
-> 2849     if axes.ndim == 1 and len(axes) == 1:
   2850         return axes[0]
   2851     return axes

AttributeError: 'AxesSubplot' object has no attribute 'ndim'

Kind regards

Member

sinhrks commented May 27, 2015

Thanks for the report. This looks an edge case when grouped_hist doesn't return np.array of AxesSubplot. PR is appreciated.

Contributor

scls19fr commented May 27, 2015

I don't know if that's an elegant way of doing but what is your opinion about changing

if axes.ndim == 1 and len(axes) == 1:
    return axes[0]
return axes

to

try:
    if axes.ndim == 1 and len(axes) == 1:
        return axes[0]
    else:
        return axes
except:
    return axes
Member

sinhrks commented May 27, 2015

try - except should work, but I prefer to check isinstance(np.ndarray) as there is no need to catch all exceptions.

NOTE: Other functions uses _flatten to avoid this kind of issue, but we cannot use it here because hist may return nested np.array.

https://github.com/pydata/pandas/blob/master/pandas/tools/plotting.py#L3346

Contributor

scls19fr commented May 27, 2015

I thought that using isinstance wasn't very "Pythonic" because of duck typing.

Member

sinhrks commented May 27, 2015

Yeah it is correct for objects intended so, such as Series and Index.

I thought np.array and AxesSubplot are separate class, thus making separate flow is clearer for others who read the source?

@jreback jreback modified the milestone: 0.17.0, 0.16.2 Jun 2, 2015

Contributor

jreback commented Jun 9, 2015

closed by #10298

jreback closed this Jun 9, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment