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 aggregate changes data type #713

Closed
kieranholland opened this issue Jan 30, 2012 · 4 comments
Closed

pivot_table aggregate changes data type #713

kieranholland opened this issue Jan 30, 2012 · 4 comments
Milestone

Comments

@kieranholland
Copy link

IMHO the result should be integer type:

df = DataFrame({
    'x':('a', 'b'),
    'y':('A', 'B'),
    'i':(1, 2),
})
pivot_table(df, rows='x', cols='y')
y  A    B  
x          
a  1.0  NaN
b  NaN  2.0

Love pandas!

@adamklein
Copy link
Contributor

There are a few places that this happens: see issue #622. Problem is representation of NaN in integer does not exist in pandas. for now.

@wesm wesm closed this as completed Feb 1, 2012
@wesm
Copy link
Member

wesm commented Feb 1, 2012

if you replace the NaNs with some other value and cast to integer, that will work:

table.fillna(0).astype(int)

#622 is actually a separate issue re: richer handling of the NumPy type hierarchy

@kieranholland
Copy link
Author

Except with mixed types:

df = dataframe({
    'x':('a', 'b'),
    'y':('a', 'b'),
    'i':(1, 2),
    'j':(1.0, 2.0),
})
pt = pivot_table(df, rows='x', cols='y', values=['i', 'j'])
print(pt)

   i         j       
   a    b    a    b  
x                    
a  1.0  nan  1.0  nan
b  nan  2.0  nan  2.0

print(pt.fillna(0).astype(int))

   i     j   
   a  b  a  b
x            
a  1  0  1  0
b  0  2  0  2

Desired:

   i         j   
   a    b    a    b
x                    
a  1    nan  1.0  nan
b  nan  2    nan  2.0

@wesm
Copy link
Member

wesm commented Feb 4, 2012

Unfortunately there's nothing that can be done until NA support in NumPy improves (not clear if that will ever happen)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants