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

Support for mean in pandas DataFrame of ufloat #58

Closed
bgatessucks opened this issue Sep 5, 2016 · 5 comments
Closed

Support for mean in pandas DataFrame of ufloat #58

bgatessucks opened this issue Sep 5, 2016 · 5 comments

Comments

@bgatessucks
Copy link

Background:
I would like to use the uncertainties package to hold and manipulate the results of Monte Carlo simulations.
At a high level I find a pandas DataFrame to be the most convenient structure, therefore I tried to combine the two packages as in the simple example below.

Is there a way I can already apply mean to different slices of the DataFrame ?
If not, would you please consider adding support for it ?

import pandas as pd
import numpy as np

import uncertainties
from uncertainties import unumpy

value = pd.DataFrame(np.arange(12).reshape(3, 4), index=['r1', 'r2', 'r3'], columns=['c1', 'c2', 'c3', 'c4'])
err = pd.DataFrame(0.01 * np.arange(12).reshape(3, 4) + 0.005, index=['r1', 'r2', 'r3'], columns=['c1', 'c2', 'c3', 'c4'])

df = pd.DataFrame(unumpy.uarray(value.values, err.values), 
                                index=['r1', 'r2', 'r3'], 
                                columns=['c1', 'c2', 'c3', 'c4'])

df.sum(axis=0)  # This works
df.sum(axis=1)  # This works
df.loc[['r1', 'r3'], :].sum(axis=1)  # This works
df.iloc[0:2, 1:].sum(axis=1)  # This works

df.mean(axis=0)  # This does not work: it can be fudged with: df.apply(lambda x: x.sum() / x.shape[0])
df.mean(axis=1)  # This does not work: it can be fudged with: df.T.apply(lambda x: x.sum() / x.shape[0])
df.loc[['r1', 'r3'], :].mean(axis=1)  # This does not work
@lebigot
Copy link
Collaborator

lebigot commented Sep 5, 2016

What are the error messages?

@bgatessucks
Copy link
Author

df.mean(axis=0)

Series([], dtype: float64)

df.mean(axis=1)
r1   NaN
r2   NaN
r3   NaN
dtype: float64

@lebigot
Copy link
Collaborator

lebigot commented Sep 5, 2016

Pandas seems to get the data type wrong.

I don't see how the uncertainties module could affect Pandas on this issue.

I would suggest that you cross-post the issue in Pandas itself. If you do, can you post a link to it here?

@bgatessucks
Copy link
Author

bgatessucks commented Sep 6, 2016

Done:
pandas-dev/pandas#14162

@lebigot
Copy link
Collaborator

lebigot commented Sep 7, 2016

"Probably a Pandas bug": pandas-dev/pandas#14162 (comment). That's my feeling too, so closing this, as I don't see what can be done within uncertainties.

@lebigot lebigot closed this as completed Sep 7, 2016
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

2 participants