Skip to content

Cannot use categorical IntervalIndex as index when creating pivot_table #25814

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
peterpanmj opened this issue Mar 21, 2019 · 1 comment · Fixed by #26765
Closed

Cannot use categorical IntervalIndex as index when creating pivot_table #25814

peterpanmj opened this issue Mar 21, 2019 · 1 comment · Fixed by #26765
Labels
Categorical Categorical Data Type Interval Interval data type Regression Functionality that used to work in a prior pandas version Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@peterpanmj
Copy link
Contributor

Code Sample, a copy-pastable example if possible

df = pd.DataFrame({"A":[1, 1, 2, 2], "B":[1,2,3,4]})
df['C'] = pd.cut(df['A'],[0,2])
df.pivot_table(index='C', values='B', aggfunc=np.sum)

Problem description

Cannot use IntervalIndex as index when creating pivot_table. It works fine on 0.23.4.

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\interval.py in _get_prev_label(label)
75 else:
76 raise TypeError('cannot determine next label for type {typ!r}'
---> 77 .format(typ=type(label)))
78
79

TypeError: cannot determine next label for type <class 'str'>

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.7.1.final.0 python-bits: 64 OS: Windows OS-release: 10 machine: AMD64 processor: Intel64 Family 6 Model 142 Stepping 9, GenuineIntel byteorder: little LC_ALL: None LANG: zh_CN.GBK@cjknarrow LOCALE: None.None

pandas: 0.24.2
pytest: 4.0.2
pip: 18.1
setuptools: 40.6.3
Cython: 0.29.2
numpy: 1.15.4
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 7.2.0
sphinx: 1.8.2
patsy: 0.5.1
dateutil: 2.7.5
pytz: 2018.7
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.8
feather: None
matplotlib: 3.0.2
openpyxl: 2.5.12
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.2
lxml.etree: 4.2.5
bs4: 4.6.3
html5lib: 1.0.1
sqlalchemy: 1.2.15
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: Nonetput of pd.show_versions() here below this line]

@jschendel
Copy link
Member

Thanks for the report. I can confirm that this is broken on master:

In [1]: import numpy as np; import pandas as pd; pd.__version__
Out[1]: '0.25.0.dev0+294.gfbe252333'

In [2]: df = pd.DataFrame({"A":[1, 1, 2, 2], "B":[1,2,3,4]}) 
   ...: df['C'] = pd.cut(df['A'],[0,2]) 
   ...: df
Out[2]: 
   A  B       C
0  1  1  (0, 2]
1  1  2  (0, 2]
2  2  3  (0, 2]
3  2  4  (0, 2]

In [3]: df.pivot_table(index='C', values='B', aggfunc=np.sum)
---------------------------------------------------------------------------
TypeError: cannot determine next label for type <class 'str'>

Interestingly, this works when df['C'] is a plain IntervalArray:

In [4]: df['C'] = pd.arrays.IntervalArray([pd.Interval(0, 2)] * 4)
   ...: df
Out[4]: 
   A  B       C
0  1  1  (0, 2]
1  1  2  (0, 2]
2  2  3  (0, 2]
3  2  4  (0, 2]

In [5]: df.pivot_table(index='C', values='B', aggfunc=np.sum)
Out[5]: 
         B
C         
(0, 2]  10

I can also confirm that this worked on 0.23.4:

In [1]: import numpy as np; import pandas as pd; pd.__version__
Out[1]: '0.23.4'

In [2]: df = pd.DataFrame({"A":[1, 1, 2, 2], "B":[1,2,3,4]}) 
   ...: df['C'] = pd.cut(df['A'],[0,2]) 
   ...: df
Out[2]: 
   A  B       C
0  1  1  (0, 2]
1  1  2  (0, 2]
2  2  3  (0, 2]
3  2  4  (0, 2]

In [3]: df.pivot_table(index='C', values='B', aggfunc=np.sum)
Out[3]: 
         B
C         
(0, 2]  10

@jschendel jschendel added Reshaping Concat, Merge/Join, Stack/Unstack, Explode Regression Functionality that used to work in a prior pandas version Categorical Categorical Data Type Interval Interval data type labels Mar 21, 2019
@jschendel jschendel added this to the Contributions Welcome milestone Mar 21, 2019
peterpanmj added a commit to peterpanmj/pandas that referenced this issue Jun 6, 2019
peterpanmj added a commit to peterpanmj/pandas that referenced this issue Jun 10, 2019
@jreback jreback modified the milestones: Contributions Welcome, 0.25.0 Jun 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type Interval Interval data type Regression Functionality that used to work in a prior pandas version Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants