Skip to content

Using a CategoricalIndex with interval categories as column index breaks operations on a DataFrame #27415

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
pberkes opened this issue Jul 16, 2019 · 2 comments · Fixed by #27443
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@pberkes
Copy link

pberkes commented Jul 16, 2019

Code Sample, a copy-pastable example if possible

>>> a = pd.DataFrame([[1, 2]], columns=pd.CategoricalIndex(pd.interval_range(start=0.0, end=2.0)))
>>> a.T * 100

              0
(0.0, 1.0]  100
(1.0, 2.0]  200

>>> a * 100

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-166-9b495e0b7c52> in <module>()
----> 1 a * 100

~/anaconda3/envs/python3/lib/python3.6/site-packages/pandas/core/ops.py in f(self, other, axis, level, fill_value)
   2034 
   2035             assert np.ndim(other) == 0
-> 2036             return self._combine_const(other, op)
   2037 
   2038     f.__name__ = op_name

...

TypeError: Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe'

Problem description

When the columns index is a CategoricalIndex with interval categories, basic operations like multiplication by a scalar are broken.

The minimal example is a contrived case, but the situation can arise in common data analysis workflows:

  1. Use pd.cut to bin floating point values
  2. Perform a pivot_table operation with the binned values as columns

For instance:

df = pd.DataFrame({'values': [10.0, 21.0, 32.0, 43.0], 
                   'strings': ['A', 'B', 'C', 'D']})
df['intervals'] = pd.cut(df['values'], [0, 20, 40, 60])
p = df.pivot_table(index='strings', columns='intervals', values='values', aggfunc='sum')

A DataFrame with a column index of this kind is brokwn in other ways, for example it does not display as an HTML table in a Jupyter notebook, etc.

Expected Output

    (0.0, 1.0] (1.0, 2.0] 
0  100          200

Workaround

df.columns = df.columns.categories seems to make the DataFrame behave correctly.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.14.123-86.109.amzn1.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.24.2
pytest: 3.5.1
pip: 10.0.1
setuptools: 39.1.0
Cython: 0.28.4
numpy: 1.15.4
scipy: 1.1.0
pyarrow: 0.14.0
xarray: None
IPython: 6.4.0
sphinx: 1.7.4
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: 1.2.1
tables: 3.4.3
numexpr: 2.6.5
feather: None
matplotlib: 3.0.3
openpyxl: 2.5.3
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.4
lxml.etree: 4.2.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.11
pymysql: None
psycopg2: 2.7.5 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: 0.1.5
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

@jschendel
Copy link
Member

This looks to be fixed on 0.25.0:

In [1]: import pandas as pd; pd.__version__
Out[1]: '0.25.0rc0'

In [2]: a = pd.DataFrame([[1, 2]], columns=pd.CategoricalIndex(pd.interval_range(start=0.0, end=2.0)))

In [3]: a * 100
Out[3]: 
   (0.0, 1.0]  (1.0, 2.0]
0         100         200

Not sure exactly which PR fixed this, but I don't recall this exact issue coming up before, so could probably use some tests to ensure that there's not a regression.

@jschendel jschendel added good first issue Needs Tests Unit test(s) needed to prevent regressions labels Jul 16, 2019
@jschendel jschendel added this to the Contributions Welcome milestone Jul 16, 2019
@another-green
Copy link
Contributor

I will add the test for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants