Repository navigation
Description
I am trying to perform binary logit and my data has some NaN. It looks like I should be able to use the "missing='drop'" option but I am getting an exception ("Exception: Data must be 1-dimensional"). I am using statsmodel version 0.6.1 (update: tried this with 0.9.0 and received same error).
import statsmodels.api as sm
...
logit = sm.Logit(y,x,missing='drop')
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.2.4\helpers\pydev_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "", line 1, in
File "C:\Anaconda\envs\geo\lib\site-packages\statsmodels\discrete\discrete_model.py", line 418, in init
super(BinaryModel, self).init(endog, exog, **kwargs)
File "C:\Anaconda\envs\geo\lib\site-packages\statsmodels\discrete\discrete_model.py", line 171, in init
super(DiscreteModel, self).init(endog, exog, **kwargs)
File "C:\Anaconda\envs\geo\lib\site-packages\statsmodels\base\model.py", line 212, in init
super(LikelihoodModel, self).init(endog, exog, **kwargs)
File "C:\Anaconda\envs\geo\lib\site-packages\statsmodels\base\model.py", line 64, in init
**kwargs)
File "C:\Anaconda\envs\geo\lib\site-packages\statsmodels\base\model.py", line 87, in _handle_data
data = handle_data(endog, exog, missing, hasconst, **kwargs)
File "C:\Anaconda\envs\geo\lib\site-packages\statsmodels\base\data.py", line 633, in handle_data
**kwargs)
File "C:\Anaconda\envs\geo\lib\site-packages\statsmodels\base\data.py", line 65, in init
**kwargs)
File "C:\Anaconda\envs\geo\lib\site-packages\statsmodels\base\data.py", line 261, in handle_missing
nan_mask = _nan_rows(*combined)
File "C:\Anaconda\envs\geo\lib\site-packages\statsmodels\base\data.py", line 47, in _nan_rows
return reduce(_nan_row_maybe_two_inputs, arrs).squeeze()
File "C:\Anaconda\envs\geo\lib\site-packages\statsmodels\base\data.py", line 46, in _nan_row_maybe_two_inputs
(x_is_boolean_array | _asarray_2d_null_rows(y)))
File "C:\Anaconda\envs\geo\lib\site-packages\pandas\core\ops.py", line 936, in wrapper
index=self.index)).finalize(self)
File "C:\Anaconda\envs\geo\lib\site-packages\pandas\core\series.py", line 248, in init
raise_cast_failure=True)
File "C:\Anaconda\envs\geo\lib\site-packages\pandas\core\series.py", line 3027, in _sanitize_array
raise Exception('Data must be 1-dimensional')
Exception: Data must be 1-dimensional
Activity
RonLek commentedon Jan 12, 2019
@jtamerius I tried with the Guerry dataset which also has a NaN in the Region column but didn't get any errors.
Package Versions:
I think you've done your imports wrong as pointed out here https://stackoverflow.com/questions/43192626/pandas-series-getting-data-must-be-1-dimensional-error
rgrasmus commentedon Apr 30, 2019
I'm having the same issue with my own dataset. It only occurs when missing='drop' is called. I tried it for a single column of the dataset that did not have any NaNs and the error is only raised when missing='drop' is called. The packages are imported properly (eg "import pandas as pd" and "import statsmodels.api as sm"). The package versions are statsmodels==0.9.0 and pandas==0.23.0
josef-pkt commentedon Apr 30, 2019
Can you make a reproducible example?
I have no guess what might be wrong based on the traceback and without data.
rgrasmus commentedon Apr 30, 2019
Ah, it looks like the error occurs when the endog is boolean data along with the above missing='drop'. Here is a minimal reproducible example:
josef-pkt commentedon Apr 30, 2019
So, if you convert the bool to int, then the same example works?
It's a bug, but I'm not familiar with the code behind this, why it would treat bool differently.
rgrasmus commentedon Apr 30, 2019
Yup, it worked fine on my dataset after casting the endog column to int.
josef-pkt commentedon Apr 30, 2019
The problem could be that pandas creates an object dtype for the bool with missing values and then numpy does not work in the usual way as for numeric dtypes.
At least there is a clear workaround.
Thanks @rgrasmus
BUG: Fix missing="drop" with a boolean endog