TST : Adding new test case for pivot_table() with Categorical data
State: closed

TST: add additional test cases for pivot_table with categorical data #21370

- [x] closes #21370
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry

Added a new test case for checking ```index/column/value``` workability as per the patch offered by PR #21252 

**Note** : the _civ means Column,Index and Value in the test name (wasn't being very creative here)

Labels:
name: Testing
description: pandas testing functions or related to the test suite
name: Categorical
description: Categorical Data Type

Comments:
Hello @uds5501! Thanks for updating the PR.



Cheers ! There are no PEP8 issues in this Pull Request. :beers: 

##### Comment last updated on June 10, 2018 at 20:48 Hours UTC
@uds5501 generally ensure that your code contributions follow the PEP8 coding standard before pushing to GitHub. You can find more info on how to do that in the contributing guide:

https://pandas.pydata.org/pandas-docs/stable/contributing.html#python-pep8
@WillAyd @jreback Fixed PEP8 issues, and yes @WillAyd  I will follow this advice from now on
@WillAyd  @jreback  ,  I have tried to improve readability in this commit. Please see if it need further improvement
@jschendel  Actually, I don't know how to recreate `columns`/`values` pivots. Help would be appreciated here


@WillAyd  I have tried to fix these according to your suggestions. look any good?
>@jschendel Actually, I don't know how to recreate `columns`/`values` pivots. Help would be appreciated here

Here's a slightly modified version of your setup to address my last comment of `'Col'` not being a `Categorical` with `NaN` values:
```python
In [2]: pd.__version__
Out[2]: '0.24.0.dev0+85.g4807905'

In [3]: idx = [np.nan, 'low', 'high', 'low', np.nan]
   ...: col = [np.nan, 'A', 'B', np.nan, 'A']
   ...: df = pd.DataFrame({
   ...:     "In": pd.Categorical(idx, categories=['low', 'high'], ordered=True),
   ...:     "Col": pd.Categorical(col, categories=['A', 'B'], ordered=True),
   ...:     "Val": range(1, 6)})

In [4]: df
Out[4]:
     In  Col  Val
0   NaN  NaN    1
1   low    A    2
2  high    B    3
3   low  NaN    4
4   NaN    A    5
```

Then doing both variations of `pivot_table` produces the correct output on master:
```python
In [5]: df.pivot_table(index="In", columns="Col", values="Val")
Out[5]:
Col     A    B
In
low   2.0  NaN
high  NaN  3.0

In [6]: df.pivot_table(columns="Col", values="Val")
Out[6]:
Col    A    B
Val  3.5  3.0
```
So you'll need to create `expected` to match the above.  Note since the columns are categorical you'll need to explicitly pass something like `columns=CategoricalIndex(...)` to the `DataFrame` constructor, like what you did with the `index` parameter in your current code (or convert the columns to categorical after creation, either works).

Just to verify, the code above does indeed replicate the buggy behavior on 0.23.0:
```python
In [2]: pd.__version__
Out[2]: '0.23.0'

In [3]: idx = [np.nan, 'low', 'high', 'low', np.nan]
   ...: col = [np.nan, 'A', 'B', np.nan, 'A']
   ...: df = pd.DataFrame({
   ...:     "In": pd.Categorical(idx, categories=['low', 'high'], ordered=True),
   ...:     "Col": pd.Categorical(col, categories=['A', 'B'], ordered=True),
   ...:     "Val": range(1, 6)})

In [4]: df.pivot_table(index="In", columns="Col", values="Val")
Out[4]:
Col  NaN    A
In
NaN  2.0  NaN
low  NaN  3.0

In [5]: df.pivot_table(columns="Col", values="Val")
Out[5]:
Col  NaN    A
Val  3.5  3.0
```
@jschendel  Thank you for the example! I will start working on it right away 😃 
doesn't this duplicate #21393 ?
@jreback No sir. the aim of this PR is to add new Test Cases which include `column/index/value` form of DataFrame 
@uds5501 ok pls rebase and fixup
closing as stale