Context
I have several data groups (defined by 3 w / i columns) and would like to perform a linear fit and each group, and then add estimates (with lower + upper fit boundaries).
Problem
After the operation, I get an error message related to the forms of the final and original data frames
An example showing the problem:
from io import StringIO
import numpy
import pandas
def fake_model(group, formula):
modeled = group.assign(
fit=numpy.random.normal(size=group.shape[0]),
ci_lower=numpy.random.normal(size=group.shape[0]),
ci_upper=numpy.random.normal(size=group.shape[0])
)
return modeled
raw_csv = StringIO("""\
location,days,era,chemical,conc
MW-A,2415,modern,"Chem1",5.4
MW-A,7536,modern,"Chem1",0.21
MW-A,7741,modern,"Chem1",0.15
MW-A,2415,modern,"Chem2",33.0
MW-A,2446,modern,"Chem2",0.26
MW-A,3402,modern,"Chem2",0.18
MW-A,3626,modern,"Chem2",0.26
MW-A,7536,modern,"Chem2",0.32
MW-A,7741,modern,"Chem2",0.24
""")
data = pandas.read_csv(raw_csv)
modeled = (
data.groupby(by=['location', 'era', 'chemical'])
.apply(fake_model, formula='conc ~ days')
.reset_index(drop=True)
)
This causes a very long trace, the essence of which is as follows:
[snip]
C:\Miniconda3\envs\puente\lib\site-packages\pandas\core\internals.py in construction_error(tot_items, block_shape, axes, e)
3880 raise e
3881 raise ValueError("Shape of passed values is {0}, indices imply {1}".format(
-> 3882 passed,implied))
3883
3884
ValueError: Shape of passed values is (8, 9), indices imply (8, 6)
I understand that I added three columns, therefore, the form (8, 9) vs (8, 6).
I do not understand that if I inspect the dataframe subgroup in the slightest way, the error above does not occur:
def fake_model2(group, formula):
_ = group.name
return fake_model(group, formula)
modeled = (
data.groupby(by=['location', 'era', 'chemical'])
.apply(fake_model2, formula='conc ~ days')
.reset_index(drop=True)
)
print(modeled)
What produces:
location days era chemical conc ci_lower ci_upper fit
0 MW-A 2415 modern Chem1 5.40 -0.466833 -0.599039 -1.143867
1 MW-A 7536 modern Chem1 0.21 -1.790619 -0.532233 -1.356336
2 MW-A 7741 modern Chem1 0.15 1.892256 -0.405768 -0.718673
3 MW-A 2415 modern Chem2 33.00 0.428811 0.259244 -1.259238
4 MW-A 2446 modern Chem2 0.26 -1.616517 -0.955750 -0.727216
5 MW-A 3402 modern Chem2 0.18 -0.300749 0.341106 0.602332
6 MW-A 3626 modern Chem2 0.26 -0.232240 1.845240 1.340124
7 MW-A 7536 modern Chem2 0.32 -0.416087 -0.521973 -1.477748
8 MW-A 7741 modern Chem2 0.24 0.958202 0.634742 0.542667
Question
. ?