Skip to content

unexpected behavior of pd.core.algorithms._ensure_data() #22160

Closed
@realead

Description

@realead

Examples

Example 1:

import pandas.core.algorithms as algos
comps = ['ss', 42]
values = ['42']
algos.isin(comps, values)

results in array([False, True], dtype=bool)

Example 2:

import pandas.core.algorithms as algos
comps = ['ss', np.nan]
values = [np.nan]
algos.isin(comps, values)

results in array([False, False], dtype=bool)

Expected Output

The results should be [False, False] for example 1 and [False, True] for example 2.

Problem description

The problem can be tracked down to this line: https://github.com/pandas-dev/pandas/blob/master/pandas/core/algorithms.py#L137

    # we have failed, return object
    values = np.asarray(values)
   return ensure_object(values), 'object', 'object'

np.asarray(values) tries to do smart things (like converting 42 to a string), it should be probably avoided by passing dtype=np.object to it.

Output of pd.show_versions()

pandas 0.22.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    AlgosNon-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diffBugMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions