Updating generic.py error message
State: closed

This is my first attempt at a PR. I just made a little change to allow an error message. Would appreciate some feedback. 
closes https://github.com/pydata/pandas/issues/8015


Labels:
name: Error Reporting
description: Incorrect or improved errors from pandas

Comments:
Thanks for the PR!

Strange, for some reason Travis is not triggered to run the test suite. 

Hi Joris,
I just realized I was a bit tired yesterday when I wrote all of this without  triggering the travis test suite. 
I will add that and update above.

Updated based on your previous comments Joris. Let me know what you think.

going to need a specific test to validate this new behavior (eg need to hit and validate each of the branches)

model after assertRaisesRegex usage

Any suggestion what I test for - just by each argument? 

someting like (error message should match, this was off the cuff)

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

so should have 4 tests to cover
put in tests/test_groupby.py (their might be some existing tests, put with those)

Let me know if this works or not. I had some travis CI problems. Submitting a new build

The updated code is added included the tests. It passes the Travis CI build

see git help here:https://github.com/pydata/pandas/wiki/Using-Git

just rebase the offending commit away and force push

any update?

hmm, it seems you have lost the actual change you made. The tests are there though.

Sorting it out now :)

On Thu, Oct 30, 2014 at 12:17 AM, jreback notifications@github.com wrote:

> hmm, it seems you have lost the actual change you made. The tests are
> there though.
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/pydata/pandas/pull/8618#issuecomment-61021868.

## 

Peadar Coyle
Skype: springcoilarch
www.twitter.com/springcoil
peadarcoyle.wordpress.com

@hayd Hey Andy, I was having a look at this - andd got some feedback (bad tests the first time I submitted). I'm trying to figure out a way to test for df.groupby() as we all agree that should return an error. But I keep running into the errors above in my tests. Any suggestions?

BTW, @springcoil, if you reply by mail, can you try to remove the older messages in the reply text? (as they clutter your responses now a bit) or otherwise reply on github

I got the following test fail - the code is updated with this ======================================================================

## 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 1602, in **exit**
    raise AssertionError("{0} not raised.".format(name))
AssertionError: TypeError not raised.

@springcoil It is a little bit confusing. Your second to last commit was on the right track, I only had one more comment (this one https://github.com/pydata/pandas/pull/8618#discussion_r19601627), but now you changed it all again in the last commit, which doesn't seem right to me.

The error on test_fails_on_no_datetime_index you post above, with wich code you got this?

It is no problem that there are still failing tests when you push, if you are not really sure why it is failing. That way we can look at it. 
So can you update to your second to last commit? And then just adapt for my comment, then it should be looking good!

I got those errors again on the latest code I submitted. I removed the tests for the moment because they passed. Any suggestions?

@sprincoil see my suggestion above: https://github.com/pydata/pandas/pull/8618#discussion_r19595636. 
You don't need to remove the tests if they are passing, just leave them in (as they have to be in the end, and if you change your code you want to know if they still pass)

Ok I've no idea why my tests fail - can someone help. My understanding is that the tests contained should pass. I've included the tests in the latest push. Any suggestions on what I am missing here?

## ERROR: test_cumcount_empty (pandas.tests.test_groupby.TestGroupBy)

Traceback (most recent call last):
  File "/home/peadarcoyle/Code/pandas/pandas/tests/test_groupby.py", line 3703, in test_cumcount_empty
    ge = DataFrame().groupby()
  File "/home/peadarcoyle/Code/pandas/pandas/core/generic.py", line 2872, in groupby
    raise TypeError('You have to specify at least one of "by" and "level"')
TypeError: You have to specify at least one of "by" and "level"

# 

## ERROR: test_groupby_args (pandas.tests.test_groupby.TestGroupBy)

Traceback (most recent call last):
  File "/home/peadarcoyle/Code/pandas/pandas/tests/test_groupby.py", line 2011, in test_groupby_args
    self.assertRaisesRegexp(TypeError, k, "You have to supply one of 'by' and 'level'")
  File "/home/peadarcoyle/Code/pandas/pandas/util/testing.py", line 184, in assertRaisesRegexp
    assertRaisesRegexp(_exception, _regexp, _callable, _args, *_kwargs)
  File "/home/peadarcoyle/Code/pandas/pandas/util/testing.py", line 1579, in assertRaisesRegexp
    manager = _AssertRaisesContextmanager(exception=_exception, regexp=_regexp)
  File "/home/peadarcoyle/Code/pandas/pandas/util/testing.py", line 1592, in __init__
    regexp = re.compile(regexp)
  File "/home/peadarcoyle/.virtualenvs/Ipython/lib/python2.7/re.py", line 190, in compile
    return _compile(pattern, flags)
  File "/home/peadarcoyle/.virtualenvs/Ipython/lib/python2.7/re.py", line 240, in _compile
    raise TypeError, "first argument must be string or compiled pattern"
TypeError: first argument must be string or compiled pattern

The first (test_cumcount_empty ) should probably change to:

```
dfe = DataFrame()
ge = dfe.groupby(dfe.index)
```

_I don't think you can do this in one line (not sure why) as `DataFrame().groupby([])`._

@springcoil @hayd for the first failing test (`test_cumcount_empty`, https://github.com/pydata/pandas/blob/master/pandas/tests/test_groupby.py#L3691). I think a one liner would be:

```
ge = DataFrame().groupby(level=0)
```

@hayd @jreback I think it is no problem to change the test here? As I don't see any reason that a `groupby` without arguments should work on an empty DataFrame (`DataFrame().groupby()`), but fails on a non-empty (`DataFrame(randn(3,3)).groupby()`)

yes thouse tests should be `DataFrame().groupby(level=0)` 

empty `.groupby()` should always raise

@springcoil 

you will need to add a test for `DataFrame().groupby()`; it should raise
why it doesn't you might have to dig a bit

pls rebase and lmk status

@springcoil ?

Sorry @jreback just rebasing and submitting now. 

Still getting some test failures - but none seem related my own code... I'm a bit confused. Does anyone have any advice?

You have included a lot of other commits, maybe something there is causing the errors. Can you do on your branch:

```
git fetch upstream
git rebase upstream/master
git push -f origin PR8015
```

Deleting this branch - starting again - with a new one, this might make it easier.
See https://github.com/pydata/pandas/pull/8950