Skip to content

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

Closed
@pberkes

Description

@pberkes

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions