pivot_table changes data type of unrelated columns · Issue #7142 · pandas-dev/pandas (original) (raw)

When using the pivot_table command, the data type of a float64 column changes to object when I include a boolean column in the pivot table. This is unrelated to issue #713.

I'm using

python: 2.7.3.final.0
pandas: 0.13.1

For example, I've created the table below with two float64 columns a,b and one boolean column c

import pandas as pd import numpy as np

np.random.seed(2) x = pd.DataFrame(np.random.random((10,2)), columns=list('ab')) x['pivot_column'] = x.a > 0.5 x['c'] = x.b > 0.5

I wish to create a pivot table, splitting on the 'pivot_column'. When using the columns a and b (both float64), the resulting table is as expected:

p2 = x.reset_index().pivot_table(rows='index', cols='pivot_column', values=list('ab')) p2.info()

with output

Int64Index: 10 entries, 0 to 9
Data columns (total 4 columns):
(a, False)    6 non-null float64
(a, True)     4 non-null float64
(b, False)    6 non-null float64
(b, True)     4 non-null float64
dtypes: float64(4)<class 'pandas.core.frame.DataFrame'>

But, when I use the columns a,b,c the data type of a and b changes from float64 to object

p1 = x.reset_index().pivot_table(rows='index', cols='pivot_column', values=list('abc')) p1.info()

with output

Int64Index: 10 entries, 0 to 9
Data columns (total 6 columns):
(a, False)    6 non-null object
(a, True)     4 non-null object
(b, False)    6 non-null object
(b, True)     4 non-null object
(c, False)    6 non-null object
(c, True)     4 non-null object
dtypes: object(6)<class 'pandas.core.frame.DataFrame'>

Is this a bug or is there some internal logic I don't know about (or both)?

Using unstack works as expected.

P.s. Pandas is great. Thanks for creating it! D.s.