Skip to content

Pivot_table with categorical data gives ValueError #10028

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
jorisvandenbossche opened this issue Apr 30, 2015 · 1 comment
Closed

Pivot_table with categorical data gives ValueError #10028

jorisvandenbossche opened this issue Apr 30, 2015 · 1 comment
Labels
Bug Categorical Categorical Data Type Duplicate Report Duplicate issue or pull request Groupby

Comments

@jorisvandenbossche
Copy link
Member

This errors for me on master as well: http://stackoverflow.com/questions/29945342/pandas-error-with-pivot-table-and-categorical-data

  category1   category2  numeric
0      grey    complete        1
1      grey    complete        2
2       NaN  incomplete        3
3      blue    complete        4
In [13]: df = pd.read_clipboard()

# with plain strings it works

In [16]: pd.pivot_table(df, index = 'category1', columns = 'category2', values = 'numeric', aggfunc = 'count')
Out[16]:
category2  complete
category1
blue              1
grey              2

In [17]: df['category1'] = df['category1'].astype('category')

In [18]: df['category2'] = df['category2'].astype('category')

In [19]: pd.pivot_table(df, index = 'category1', columns = 'category2', values =
 'numeric', aggfunc = 'count')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

...

c:\users\vdbosscj\scipy\pandas-joris\pandas\core\common.py in _astype_nansafe(ar
r, dtype, copy)
   2726
   2727         if np.isnan(arr).any():
-> 2728             raise ValueError('Cannot convert NA to integer')
   2729     elif arr.dtype == np.object_ and np.issubdtype(dtype.type, np.intege
r):
   2730         # work around NumPy brokenness, #1987

ValueError: Cannot convert NA to integer
@jreback
Copy link
Contributor

jreback commented Apr 30, 2015

dupe of #9534

@jreback jreback closed this as completed Apr 30, 2015
@jreback jreback added Bug Groupby Duplicate Report Duplicate issue or pull request Categorical Categorical Data Type labels Apr 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type Duplicate Report Duplicate issue or pull request Groupby
Projects
None yet
Development

No branches or pull requests

2 participants