Skip to content

ENH: get something like a "list-like" type out of infer_dtype when a series with all lists is passed  #56482

Open
@marto1

Description

@marto1

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

Hi,

I was trying to use infer_dtype to fish for dataframe columns that contain only list like values, but I get only "mixed" in that case. To demonstrate the issue:

>>> import pandas as pd
>>> from pandas.api.types import infer_dtype, is_list_like

>>> s = pd.Series([[1,2], None, [5,6]])
>>> non_list = pd.Series(["blabla", None, 234.0])
>>> infer_dtype(s, skipna=True)
'mixed'
>>> all(s.dropna().apply(is_list_like))
True
>>> infer_dtype(non_list, skipna=True)
'mixed'
>>> all(non_list.dropna().apply(is_list_like))
False

So for both list-like and truly mixed series I get "mixed". Something like "list-like" in that case would be much better.

Feature Description

I'm guessing a patch to infer_dtype or an entirely new method, e.g. a is_list_like() that takes a series and checks all values.

Alternative Solutions

One could do:

import pandas as pd
from pandas.api.types import infer_dtype, is_list_like

s = pd.Series([[1,2], None, [5,6]])
all(s.dropna().apply(is_list_like))

but this scans the entire series which is not great for big series/df columns.

Additional Context

No response

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