dmsul / econtools

Econometrics and data manipulation functions.
http://www.danielmsullivan.com/econtools
BSD 3-Clause "New" or "Revised" License
111 stars 24 forks source link

KeyError while using mt.reg and setting fe_name #6

Open jhuang2023 opened 2 months ago

jhuang2023 commented 2 months ago

Hi, I am facing an issue and cannot find a solution online.

result = mt.reg(mydata, # DataFrame to use 'car_-1_1', # Outcome fin_var + lang_cols, # Indep. Variables fe_name= 'permno', # Fixed-effects cluster= 'fyearqrt' # Cluster )

I confirm that 'permno' is in the columns, and I also try to set the value type to string. But I still get the error:

`KeyError Traceback (most recent call last) Cell In[17], line 1 ----> 1 result = mt.reg(mydata, # DataFrame to use 2 'car_-1_1', # Outcome 3 fin_var + lang_cols, # Indep. Variables
4 fe_name= 'permno', # Fixed-effects 5 #cluster= 'fyearqrt' # Cluster 6 )

File c:\Users\KK\Anaconda3\envs\py38\lib\site-packages\econtools\metrics\api.py:82, in reg(df, y_name, x_name, fe_name, a_name, nosingles, vce_type, cluster, shac, addcons, nocons, awt_name, save_mem, check_colinear) 71 fe_name = _a_name_deprecation_handling(a_name, fe_name) 73 RegWorker = Regression( 74 df, y_name, x_name, 75 fe_name=fe_name, nosingles=nosingles, addcons=addcons, nocons=nocons, (...) 79 check_colinear=check_colinear, 80 ) ---> 82 results = RegWorker.main() 83 return results

File c:\Users\KK\Anaconda3\envs\py38\lib\site-packages\econtools\metrics\core.py:49, in RegBase.main(self) 48 def main(self): ---> 49 self.set_sample() 50 self.estimate() 51 self.get_vce()

File c:\Users\KK\Anaconda3\envs\py38\lib\site-packages\econtools\metrics\core.py:77, in RegBase.set_sample(self) 75 # Demean or add constant 76 if self.fe_name is not None: ---> 77 self._demean_sample() 78 elif self.addcons: 79 _cons = np.ones(self.y.shape[0])

File c:\Users\KK\Anaconda3\envs\py38\lib\site-packages\econtools\metrics\core.py:94, in RegBase._demean_sample(self) 92 self.y_raw = self.y.copy() 93 for var in self.vars_in_reg: ---> 94 self.dict[var] = _demean(self.A, self.dict[var])

File c:\Users\KK\Anaconda3\envs\py38\lib\site-packages\econtools\metrics\core.py:252, in _demean(A, df) 250 group_name = A.name 251 mean = df.groupby(A).mean() --> 252 large_mean = force_df(A).join(mean, on=group_name).drop(group_name, 253 axis=1) 254 if df.ndim == 1: 255 large_mean = large_mean.squeeze()

File c:\Users\KK\Anaconda3\envs\py38\lib\site-packages\pandas\core\frame.py:9729, in DataFrame.join(self, other, on, how, lsuffix, rsuffix, sort, validate) 9566 def join( 9567 self, 9568 other: DataFrame | Series | Iterable[DataFrame | Series], (...) 9574 validate: str | None = None, 9575 ) -> DataFrame: 9576 """ 9577 Join columns of another DataFrame. 9578 (...) 9727 5 K1 A5 B1 9728 """ -> 9729 return self._join_compat( 9730 other, 9731 on=on, 9732 how=how, 9733 lsuffix=lsuffix, 9734 rsuffix=rsuffix, 9735 sort=sort, 9736 validate=validate, 9737 )

File c:\Users\KK\Anaconda3\envs\py38\lib\site-packages\pandas\core\frame.py:9768, in DataFrame._join_compat(self, other, on, how, lsuffix, rsuffix, sort, validate) 9758 if how == "cross": 9759 return merge( 9760 self, 9761 other, (...) 9766 validate=validate, 9767 ) -> 9768 return merge( 9769 self, 9770 other, 9771 left_on=on, 9772 how=how, 9773 left_index=on is None, 9774 right_index=True, 9775 suffixes=(lsuffix, rsuffix), 9776 sort=sort, 9777 validate=validate, 9778 ) 9779 else: 9780 if on is not None:

File c:\Users\KK\Anaconda3\envs\py38\lib\site-packages\pandas\core\reshape\merge.py:148, in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate) 131 @Substitution("\nleft : DataFrame or named Series") 132 @Appender(_merge_doc, indents=0) ... -> 1778 raise KeyError(key) 1780 # Check for duplicates 1781 if values.ndim > 1:

KeyError: 'permno'`

I also find another person having the same issue: https://stackoverflow.com/questions/76452835/keyerror-while-using-mt-reg-econtools