BUG: Instantiating DataFrames using dicts with iterators/generators as values causes errors · Issue #26349 · pandas-dev/pandas (original) (raw)
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,