Skip to content

LabelEncoder with mixed typed labels feature or bug? #17294

Open

Description

@thomasjpfan

Is the following a bug or a feature?

from sklearn.preprocessing import LabelEncoder

LabelEncoder().fit_transform(['1', '2', 2])
# array([0, 1, 1])

LabelEncoder().fit_transform(np.array(['1', '2', 2]))
# array([0, 1, 1])

LabelEncoder().fit_transform(np.array(['1', '2', 2], dtype=object))
# TypeError: Encoders require their input to be uniformly strings or numbers.

This stems from how we only check for multiple types in _encode when dtype=object. Note this behavior is the same in 0.22, 0.23, and on master.

Activity

  1. changed the title [-]LabelEncoder with different labels feature or bug?[/-] [+]LabelEncoder with mixed typed labels feature or bug?[/+] on May 20, 2020
  2. amueller commented on May 20, 2020

    @amueller
    Member

    For the center we have no choice, the bottom one makes sense to me, you don't want to necessarily identify '1' with 1.
    You might argue that the top one is a bug, but I'm not sure if we want to replace the numpy casting logic.

    Btw, you run into this when concatenating dataframes from multiple csv files and a column is sometimes parsed as string and sometimes as integer. Great Fun!

  3. jnothman commented on May 21, 2020

    @jnothman
    Member
  4. jnothman commented on May 21, 2020

    @jnothman
    Member
  5. glemaitre commented on May 21, 2020

    @glemaitre
    Member

    If we want to make the stuff consistent I am really scared that we will stumble in the dtype nightmare and we might come with tricks-and-tips implementations on something which should already be solved otherwise I think.

    If we could maybe have better error messages with potential avenues for resolution could already be great.

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