Skip to content

BUG: Instantiating DataFrames using dicts with iterators/generators as values causes errors #26349

Closed
@topper-123

Description

@topper-123

Working on removing compat.lrange, I've found that Series will accept iterators and generators as input:

>>> pd.Series(reversed(range(3)))  # Iterator work for Series
0    0
1    1
2    2
dtype: int64
>>>  pd.Series((i for i in range(3)))  # Generators work for Series
0    0
1    1
2    2
dtype: int64

If iterators and generators work when instantiating Series, I'd expect that they'd work as dict-values for DataFrames also, but they don't:

>>> pd.DataFrame({'A': reversed(range(3))})  # iterator do not work
TypeError: object of type 'range_iterator' has no len()
>>> pd.DataFrame({'A': (i for i in range(3))})  # generator do not work
TypeError: object of type 'generator' has no len()
>>> pd.DataFrame({'A': range(3)})  # ranges work
   A
0  0
1  1
2  2

The docs say that list-likes will work as dict values, and because iterators and generators are iterables and list-like, they should work.

This issue is different than #26342, but the fix is still relatively straight forward.

I can can put up a PR one of the coming days,

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSeriesSeries data structure

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions