Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
589098c
Updating tests file Cleaning up
springcoil Oct 27, 2014
2e3c35b
Updating generic file to include change
springcoil Oct 30, 2014
f41ef3d
Pushing code for review
springcoil Oct 30, 2014
168b83a
updating after tests fail
springcoil Oct 30, 2014
6cc47ca
Updating due to errors
springcoil Oct 31, 2014
968d68f
Updating including test fails
springcoil Nov 1, 2014
6bf83c5
BUG CSV: fix problem with trailing whitespace in skipped rows, issues…
Nov 7, 2014
e5fe75e
Merge pull request #8752 from selasley/trailing_spaces_fix
jreback Nov 27, 2014
0ef5c07
BUG: DatetimeIndex with time as key
behzadnouri Nov 27, 2014
1e5d25a
Fix unrecognized 'Z' UTC designator
Nov 16, 2014
9fd99d7
Merge pull request #8832 from broessli/z-utc
jreback Nov 27, 2014
c8e36d4
Doc change for Issue #8805
villasv Nov 27, 2014
a3e478d
Merge pull request #8912 from vvbchaves/master
jreback Nov 28, 2014
3b2089b
DOC: specify return type in to_datetime
jorisvandenbossche Nov 29, 2014
cfcda5f
CLN: move import to top of file
aevri Nov 29, 2014
99a7318
Merge pull request #8922 from aevri/refactor/move_import
jorisvandenbossche Nov 29, 2014
f21539b
BUG: Option context applies on __enter__
hkleynhans Nov 29, 2014
26129f5
Merge pull request #8921 from jorisvandenbossche/docstring-to_datetime
jorisvandenbossche Nov 29, 2014
e9ee47c
Merge pull request #8925 from hkleynhans/fix/8514_option_context_with
jorisvandenbossche Nov 29, 2014
fef4b09
BUG: fix doctests in pandas.core.common
rupertthompson Nov 29, 2014
e2f8f0a
Merge pull request #8907 from behzadnouri/time-slice
jreback Nov 29, 2014
2e59e42
Merge pull request #8931 from rupertthompson/fix/core_common_doctests
jorisvandenbossche Nov 30, 2014
e759d99
TST: 32-bit construction fix re GH8907
jreback Nov 30, 2014
8290a4d
Merge pull request #8937 from JanSchulz/cat_unique2
jreback Nov 30, 2014
bcaf7fd
Updating tests file Cleaning up
springcoil Oct 27, 2014
d55f582
Updating generic file to include change
springcoil Oct 30, 2014
b1168b5
Pushing code for review
springcoil Oct 30, 2014
ca70dbe
updating after tests fail
springcoil Oct 30, 2014
11c53a7
Updating due to errors
springcoil Oct 31, 2014
baa8cd6
Updating including test fails
springcoil Nov 1, 2014
ab1c90f
Updating test files
springcoil Nov 30, 2014
e40215a
Adding generic.py etc
springcoil Nov 30, 2014
49bd373
Refactoring slightly
springcoil Nov 30, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2868,9 +2868,17 @@ def groupby(self, by=None, axis=0, level=None, as_index=True, sort=True,
"""

from pandas.core.groupby import groupby
axis = self._get_axis_number(axis)
return groupby(self, by, axis=axis, level=level, as_index=as_index,
sort=sort, group_keys=group_keys, squeeze=squeeze)
if axis is not None:
axis = self._get_axis_number(axis)
return groupby(self, by, axis=axis, level=level, as_index=as_index,
sort=sort, group_keys=group_keys, squeeze=squeeze)
elif level is not None:
raise TypeError('You have to specify one of "by" or "level"')
elif by is not None:
raise TypeError('You have to specify one of "by" or "level"')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what reason are the two elifs included? Because if level is not None (it means that level is specified), is should not raise, no? (but you do raise here)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right I shouldn't raise for not none I should just raise for None.

else:
raise TypeError('You have to specify one of "by" or "level"')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand the logic here. You first test for axis is not None, which will almost always be the case (as axis has a default value of 0), and only afterwards you test if level and/or by are not None. But I think you will never get there since if axis is not None will give True?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any suggestions on improving this logic?
Obviously I want to check if levels are not none, Do I just remove the if
axis is not None: branch and proceed with the rest?
I'll try it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not fully sure, but something like just checking that at least level or by is specified:

if level is None and by is None:
    raise TypeError('You have to specify at least one of "by" or "level"')

and then just proceed with how it was original?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that and then I get broken tests. Any suggestions?

FAIL: test_fails_on_no_datetime_index (pandas.tseries.tests.test_resample.TestTimeGrouper)

Traceback (most recent call last):
File "/home/peadarcoyle/Code/pandas/pandas/tseries/tests/test_resample.py", line 1385, in test_fails_on_no_datetime_index
df.groupby(TimeGrouper('D'))
File "/home/peadarcoyle/Code/pandas/pandas/util/testing.py", line 1604, in exit
return self.handle_success(exc_type, exc_value, traceback)
File "/home/peadarcoyle/Code/pandas/pandas/util/testing.py", line 1617, in handle_success
raise_with_traceback(e, traceback)
File "/home/peadarcoyle/Code/pandas/pandas/tseries/tests/test_resample.py", line 1385, in test_fails_on_no_datetime_index
df.groupby(TimeGrouper('D'))
File "/home/peadarcoyle/Code/pandas/pandas/core/generic.py", line 2876, in groupby
raise TypeError('You have to specify one of "by" or "level"')
AssertionError: "axis must be a DatetimeIndex, but got an instance of 'Int64Index'" does not match "You have to specify one of "by" or "level""

FAIL: test_insert_error_msmgs (pandas.tests.test_frame.TestDataFrame)

Traceback (most recent call last):
File "/home/peadarcoyle/Code/pandas/pandas/tests/test_frame.py", line 2661, in test_insert_error_msmgs
df['gr'] = df.groupby(['b', 'c']).count()
File "/home/peadarcoyle/Code/pandas/pandas/util/testing.py", line 1604, in exit
return self.handle_success(exc_type, exc_value, traceback)
File "/home/peadarcoyle/Code/pandas/pandas/util/testing.py", line 1617, in handle_success
raise_with_traceback(e, traceback)
File "/home/peadarcoyle/Code/pandas/pandas/tests/test_frame.py", line 2661, in test_insert_error_msmgs
df['gr'] = df.groupby(['b', 'c']).count()
File "/home/peadarcoyle/Code/pandas/pandas/core/generic.py", line 2876, in groupby
raise TypeError('You have to specify one of "by" or "level"')
AssertionError: "incompatible index of inserted column with frame index" does not match "You have to specify one of "by" or "level""

FAIL: test_filter_enforces_scalarness (pandas.tests.test_groupby.TestGroupBy)

Traceback (most recent call last):
File "/home/peadarcoyle/Code/pandas/pandas/tests/test_groupby.py", line 4251, in test_filter_enforces_scalarness
df.groupby('c').filter(lambda g: g['a'] == 'best')
File "/home/peadarcoyle/Code/pandas/pandas/util/testing.py", line 1604, in exit
return self.handle_success(exc_type, exc_value, traceback)
File "/home/peadarcoyle/Code/pandas/pandas/util/testing.py", line 1617, in handle_success
raise_with_traceback(e, traceback)
File "/home/peadarcoyle/Code/pandas/pandas/tests/test_groupby.py", line 4251, in test_filter_enforces_scalarness
df.groupby('c').filter(lambda g: g['a'] == 'best')
File "/home/peadarcoyle/Code/pandas/pandas/core/generic.py", line 2876, in groupby
raise TypeError('You have to specify one of "by" or "level"')
AssertionError: "filter function returned a.*" does not match "You have to specify one of "by" or "level""

FAIL: test_filter_non_bool_raises (pandas.tests.test_groupby.TestGroupBy)

Traceback (most recent call last):
File "/home/peadarcoyle/Code/pandas/pandas/tests/test_groupby.py", line 4264, in test_filter_non_bool_raises
df.groupby('a').filter(lambda g: g.c.mean())
File "/home/peadarcoyle/Code/pandas/pandas/util/testing.py", line 1604, in exit
return self.handle_success(exc_type, exc_value, traceback)
File "/home/peadarcoyle/Code/pandas/pandas/util/testing.py", line 1617, in handle_success
raise_with_traceback(e, traceback)
File "/home/peadarcoyle/Code/pandas/pandas/tests/test_groupby.py", line 4264, in test_filter_non_bool_raises
df.groupby('a').filter(lambda g: g.c.mean())
File "/home/peadarcoyle/Code/pandas/pandas/core/generic.py", line 2876, in groupby
raise TypeError('You have to specify one of "by" or "level"')
AssertionError: "filter function returned a.*" does not match "You have to specify one of "by" or "level""

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, difficult to say without seeing the code. Can you just push your latest attempt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nosetests fail but sure :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be all pushed now :)



def asfreq(self, freq, method=None, how=None, normalize=False):
"""
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1961,6 +1961,9 @@ def test_groupby_level(self):
# raise exception for non-MultiIndex
self.assertRaises(ValueError, self.df.groupby, level=1)




def test_groupby_level_index_names(self):
## GH4014 this used to raise ValueError since 'exp'>1 (in py2)
df = DataFrame({'exp' : ['A']*3 + ['B']*3, 'var1' : lrange(6),}).set_index('exp')
Expand Down Expand Up @@ -1999,6 +2002,27 @@ def test_groupby_level_apply(self):
result = frame['A'].groupby(level=0).count()
self.assertEqual(result.index.name, 'first')


#PR8618 and issue 8015
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you align this with def ..

def test_groupby_args(self):
frame = self.mframe
def g():
frame.groupby(level=None).count()
self.assertRaisesRegexp(TypeError, g, "You have to supply one of 'by' or 'level'")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assert should not be in the function definition, as in this way it is not called:

def g():
    frame.groupby(..)
self.assert ...


def k():
frame.groupby(by=None).count()
self.assertRaisesRegexp(TypeError, k, "You have to supply one of 'by' or 'level'")

def j():
frame.groupby()
self.assertRaisesRegexp(TypeError, j, "You have to supply one of 'by' or 'level'")

def i():
frame.groupby(axes=None)
self.assertRaisesRegexp(TypeError, i, "You have to supply one of 'by' or 'level'")


def test_groupby_level_mapper(self):
frame = self.mframe
deleveled = frame.reset_index()
Expand Down