Skip to content

missing='drop' returning "Exception: Data must be 1-dimensional" error for sm.Logit #5445

Closed

Description

@jtamerius

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

  1. RonLek commented on Jan 12, 2019

    @RonLek
    Contributor

    @jtamerius I tried with the Guerry dataset which also has a NaN in the Region column but didn't get any errors.

    Package Versions:

    statsmodels==0.9.0
    pandas==0.23.4
    

    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

  2. rgrasmus commented on Apr 30, 2019

    @rgrasmus

    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

  3. josef-pkt commented on Apr 30, 2019

    @josef-pkt
    Member

    Can you make a reproducible example?

    I have no guess what might be wrong based on the traceback and without data.

  4. rgrasmus commented on Apr 30, 2019

    @rgrasmus

    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:

    import pandas as pd
    import numpy as np
    import statsmodels.api as sm
    
    x = np.linspace(0,1)
    y = 5*x-3
    Data = np.stack([x,y]).T
    myDf = pd.DataFrame(data=Data,columns=['x','y'])
    myDf['Bool_X'] = myDf['x']>0.5
    mod = sm.Logit(myDf['Bool_X'], myDf['y'], missing='drop')
  5. josef-pkt commented on Apr 30, 2019

    @josef-pkt
    Member

    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.

  6. rgrasmus commented on Apr 30, 2019

    @rgrasmus

    Yup, it worked fine on my dataset after casting the endog column to int.

  7. josef-pkt commented on Apr 30, 2019

    @josef-pkt
    Member

    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

  8. added a commit that references this issue on Oct 11, 2026
    ce056fd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions