Skip to content

Groupby doesn't accept level=[0] for Index. #13901

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

Closed
starhel opened this issue Aug 4, 2016 · 7 comments
Closed

Groupby doesn't accept level=[0] for Index. #13901

starhel opened this issue Aug 4, 2016 · 7 comments
Milestone

Comments

@starhel
Copy link

starhel commented Aug 4, 2016

Today I've found weird behaviour of level parameter of groupby. I need to groupby two dataframes by index and this code is working as long as I'm using Multiindex.

index1 = pandas.Index([1,2,3,4,5])
index2 = pandas.Index([1,2,3,4,5])
first = pandas.DataFrame({'a':[1,2,3,4,5]}, index=index1)
second = df2 = pandas.DataFrame({'a':[2,3,4,5,6]}, index=index2)
pandas.concat([first, second]).groupby(level=range(first.index.nlevels)).min()

Unfortunately when I'm using simple Index I get:

ValueError: level > 0 only valid with MultiIndex

In other way if I passed 0 instead of [0], everything is working properly. It's weird and cause that it's hard to write reusable code.

INSTALLED VERSIONS

commit: None
python: 2.7.12.final.0
python-bits: 64
OS: Linux
OS-release: 4.2.0-42-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF8

pandas: 0.18.1
nose: 1.3.7
pip: 8.1.2
setuptools: 20.3
Cython: None
numpy: 1.11.1
scipy: None
statsmodels: None
xarray: None
IPython: 4.2.0
sphinx: None
patsy: None
dateutil: 2.4.1
pytz: 2016.4
blosc: None
bottleneck: 1.1.0
tables: 3.2.2
numexpr: 2.6.0
matplotlib: 1.5.1
openpyxl: None
xlrd: None
xlwt: 1.1.2
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.13
pymysql: None
psycopg2: None
jinja2: None
boto: None
pandas_datareader: None

@sinhrks
Copy link
Member

sinhrks commented Aug 4, 2016

Using range as level may work but not intended. It doesn't work PY3 at least.

@jreback
Copy link
Contributor

jreback commented Aug 4, 2016

I suppose this is legit, as [0] de-facto is equivalent to a scalar.

@jreback jreback added this to the 1.0 milestone Aug 4, 2016
@sinhrks
Copy link
Member

sinhrks commented Aug 4, 2016

ok, then the fix should include the support of range in py3.

@sinhrks
Copy link
Member

sinhrks commented Aug 4, 2016

ah sorry you refer to one length list, not range, @jreback?

@jreback
Copy link
Contributor

jreback commented Aug 4, 2016

yes @sinhrks we don't accept non-list-likes anywhere (meaning range) for things like this. They must be a list/tuple/array (or scalar in this case)

@starhel
Copy link
Author

starhel commented Aug 4, 2016

@sinhrks Sorry for the confusion. In python2 range returns list and I totally forgot about the change in py3.

@agraboso
Copy link
Contributor

agraboso commented Aug 4, 2016

In Python 3 range is an iterable — equivalent to Python 2's xrange. It fits the is_list_like definition, so my PR does accept them.

@jorisvandenbossche jorisvandenbossche modified the milestones: 0.19.0, 1.0 Aug 15, 2016
@jorisvandenbossche jorisvandenbossche modified the milestones: 0.20.0, 0.19.0 Sep 1, 2016
@jreback jreback modified the milestones: 0.19.0, 0.20.0 Sep 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants