Skip to content

Commit

Permalink
ENH: handle generators in Series constructor close #1679
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Aug 12, 2012
1 parent 188ddd7 commit 745a496
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pandas 0.8.2

- Add ``flags`` option for ``re.compile`` in some Series.str methods (#1659)
- Parsing of UTC date strings in read_* functions (#1693)
- Handle generator input to Series (#1679)

**API Changes**

Expand Down
3 changes: 3 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from itertools import izip
import operator
from distutils.version import LooseVersion
import types

from numpy import nan, ndarray
import numpy as np
Expand Down Expand Up @@ -323,6 +324,8 @@ def __new__(cls, data=None, index=None, dtype=None, name=None,
data = lib.fast_multiget(data, index.values, default=np.nan)
except TypeError:
data = [data.get(i, nan) for i in index]
elif isinstance(data, types.GeneratorType):
data = list(data)

if dtype is not None:
dtype = np.dtype(dtype)
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,18 @@ def test_constructor_series(self):

assert_series_equal(s2, s1.sort_index())

def test_constructor_generator(self):
gen = (i for i in range(10))

result = Series(gen)
exp = Series(range(10))
assert_series_equal(result, exp)

gen = (i for i in range(10))
result = Series(gen, index=range(10, 20))
exp.index = range(10, 20)
assert_series_equal(result, exp)

def test_constructor_maskedarray(self):
data = ma.masked_all((3,), dtype=float)
result = Series(data)
Expand Down

0 comments on commit 745a496

Please sign in to comment.