Skip to content

DataFrame with bool type not cast correctly. #880

Closed

Description

@jseabold

From the mailing list. Replication

sm.Logit(np.random.randint(2, size=50), pandas.DataFrame(sm.add_constant(np.random.randint(2, size=50).astype(bool)))).fit()

Activity

  1. jseabold commented on Jun 20, 2013

    @jseabold
    MemberAuthor

    X has dtype of object.

    I wonder if this worked with older versions of pandas.

  2. jburroni commented on Jun 20, 2013

    @jburroni

    this is not a step to reproduce. Sorry

    df = pd.DataFrame(data= random.random(100), columns=['rand'])
    df.boolean = df.rand > 0.5
    sm.Logit(df.rand, sm.add_constant(df['boolean'], prepend=True)).fit()
    
  3. josef-pkt commented on Jun 20, 2013

    @josef-pkt
    Member

    I'm using pandas 0.7.3 and get the same casting

    >>> np.asarray(zdf)
    array([[0.0, True],
           [2.0, False]], dtype=object)
    >>> np.asarray(zdf, float)
    array([[ 0.,  1.],
           [ 2.,  0.]])
    
    casting to at least float
    >>> np.asarray(zdf, np.find_common_type(tuple(zdf.dtypes) + (float,), []))
    array([[ 0.,  1.],
           [ 2.,  0.]])
    >>> np.find_common_type(tuple(zdf.dtypes) + (float,), [])
    dtype('float64')
    

    for numpy >=1.6

    np.result_type(arr, np.float64) can be used to find common dtype

  4. josef-pkt commented on Jun 20, 2013

    @josef-pkt
    Member

    one possibility with pandas dataframes:

    >>> common_dtype = np.find_common_type(list(xdf.dtypes), [])
    >>> if common_dtype is np.dtype('object'): raise ValueError('dtype cannot be converted to numeric')
    else: x = np.asarray(xdf, dtype=common_dtype)
    

    this doesn't require a minimum numeric dtype, maybe we want at least float32 if someone wants to calculate in single precision.

  5. added this to the 0.6 milestone on May 28, 2014
  6. jseabold commented on May 28, 2014

    @jseabold
    MemberAuthor

    Bumping this. Contacted off-list about the same issue. I guess we need to cast object dtypes or maybe we should just raise?

    X = np.random.random((40,2))
    df = pd.DataFrame(X)
    df[2] = np.random.randint(2, size=40).astype('object')
    df['constant'] = 1
    
    y = pd.Series(np.random.randint(2, size=40))
    
    sm.Logit(y, df).fit()
    
  7. jseabold commented on May 28, 2014

    @jseabold
    MemberAuthor

    Really we should have comprehensive handling of dtypes, including single precision, etc. Might be a decent amount of work though. We'd have to check.

  8. josef-pkt commented on Jun 3, 2014

    @josef-pkt
    Member

    asked on the mailinglist: automatic conversion of dates in exog

  9. added 2 commits that reference this issue on Sep 26, 2014
    eb55b15
    00f6558
  10. added a commit that references this issue on Oct 23, 2014
    7e3fe95
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions