Skip to content
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

pivot_table(aggfunc="count") with category column raise "ValueError: Cannot convert NA to integer" #9534

Closed
ruoyu0088 opened this issue Feb 23, 2015 · 2 comments
Labels
Bug Categorical Categorical Data Type Groupby Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@ruoyu0088
Copy link

Here is the test code, that return the right table:

import pandas as pd
data = {"C1":["A", "B", "C", "C"], "C2":["a", "a", "b", "b"], "V":[1, 2, 3, 4]}
df = pd.DataFrame(data)
df.pivot_table("V", index="C1", columns="C2", aggfunc="count")

when convert column to category, ValueError is raised:

df2 = df.copy()
df2["C1"] = df2["C1"].astype("category")
df2.pivot_table("V", index="C1", columns="C2", aggfunc="count")

groupby also raise the same error:

df2.groupby(["C1", "C2"]).count()

add dropna() to count() in groupby.py fix this problem:

    def count(self, axis=0):
        return self._count().dropna().astype('int64')
@jreback
Copy link
Contributor

jreback commented Feb 23, 2015

xref #8731

pls pd.show_versions()

pull-requests are welcome

@jreback jreback added Bug Groupby Reshaping Concat, Merge/Join, Stack/Unstack, Explode Categorical Categorical Data Type labels Feb 23, 2015
@jreback jreback modified the milestones: 0.16.0, 0.16.1 Feb 23, 2015
@ruoyu0088
Copy link
Author

Here is the output of show_versions:

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.9.final.0
python-bits: 32
OS: Windows
OS-release: 7
machine: x86
processor: x86 Family 6 Model 42 Stepping 7, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None

pandas: 0.15.2.dev
nose: 1.3.4
Cython: 0.21.2
numpy: 1.9.1
scipy: 0.15.0
statsmodels: 0.6.1
IPython: 3.0.0-dev
sphinx: 1.2.3
patsy: 0.3.0
dateutil: 2.3
pytz: 2014.10
bottleneck: None
tables: 3.1.1
numexpr: 2.4
matplotlib: 1.4.2
openpyxl: None
xlrd: 0.9.3
xlwt: None
xlsxwriter: 0.6.5
lxml: 3.4.1
bs4: 4.3.2
html5lib: None
httplib2: None
apiclient: None
rpy2: 2.5.4
sqlalchemy: 0.9.8
pymysql: None
psycopg2: None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type Groupby Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants