Fix for DataFrame.hist() with by- and weights-keyword
State: closed

will make the following work

``` python
import numpy as np
import pandas as pd
d = {'one' : ['A', 'A', 'B', 'B', 'C'],
     'two' : [4., 3., 2., 1., np.nan],
     'three' : [10., 8., np.nan, 5., 7.]}     
df = pd.DataFrame(d)
df.hist('two', by='one', weights='three', bins=range(0, 10))
# or
df.hist('two', by=df.one.values, weights='three', bins=range(0, 10))
```


Labels:
name: Visualization
description: plotting

Comments:
Because of my poor git skills, this is a continuation for
https://github.com/pydata/pandas/pull/11028

upgraded to work with the newest pandas release

I am at loss writing a test for this. @jreback I could not find the place you recommended. The place where weights are tested right now seems to be test_generic.py?

However I am unsure of how to construct a test the best way, I have never written one, so pointers are very welcome!

Note, I added the column 'weighst' to the supplied DataFrame if an array was supplied instead of the column name... Can this be done in a nicer way? I need it in the groupby logic.

if a column is named 'weights' it will be overwritten... :(

@Twizzledrizzle no worries about the git snafu, we've all been there. People are usually in the [chat](https://gitter.im/pydata/pandas) if you need help walking through git.

I think Jeff was talking about the logic in `DataFrame.sample` in `pandas/core/frame.py`. I haven't had a chance to look at your code to see if it's similar enough to refactor into a common function.

For the tests you can look to see how similar tests are done in `pandas/tests/test_graphics.py`. Basically you call the function your testing and assert that something about the result equals an expected value that you hardcode in the test.

I'll take a look later, but it won't be until next week sometime.

Thanks @TomAugspurger I will take a look.

A lot of commits right now, finding things while working through the travis errors... I hope the server does not crash!

Yeah, we'll come up with a better way to solve that weights column.

FYI you can run the tests locally, will be quicker than waiting on Travis. Make sure you have nose installed (`pip install nose` or `conda install nose`) and then run the tests with `nosetests test_graphics` from the `pandas/tests` directory.

Run specific tests with `nosetests test_graphics:TestClass.test_function` (I think)

I have python 2.7 and every tests seems to work fine. Why could python 3.4 fail on 

```
nosetests test_data:TestGoogle.test_get_multi1
```

Does not seem to have anything to do with my pull request?

need tests

@Twizzledrizzle ignore that goggle multi failure....its an unreliable test on travis

Made the code more readable now I think, I hope it looks better.

Regarding tests, are we talking tests like this? In test_graphics_others.py? (Looks like some other tests are done one the hist functionality there).

ax = _check_plot_works(df.hist, column='two', by='one', weights='three', bins=range(0, 10))

Oh also, as a side, the new function df.plot.hist() will miss a lot of this functionality.

df.plot.hist() seem to have problems using the by- keyword. Also missing column keyword. I tried working with the code, but it was more complex than the one I have been editing. I could not get my head around making the by-keyword work. 

Should I open a discussion around this perhaps in a separate thread?

@TomAugspurger status of this?

I am awaiting with interest what will happen with

https://github.com/pydata/pandas/pull/8018

before putting to much time in this pull request. 

@TomAugspurger status of this

@TomAugspurger status?

I think waiting on #8018, which will conflict with the case where `by` is not None here.

status?

@TomAugspurger can this be independent #8018 ?


@TomAugspurger ?
closing as stale