Skip to content

Commit 745a496

Browse files
committed
ENH: handle generators in Series constructor close pandas-dev#1679
1 parent 188ddd7 commit 745a496

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

RELEASE.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pandas 0.8.2
3535

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

3940
**API Changes**
4041

pandas/core/series.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from itertools import izip
99
import operator
1010
from distutils.version import LooseVersion
11+
import types
1112

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

327330
if dtype is not None:
328331
dtype = np.dtype(dtype)

pandas/tests/test_series.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,18 @@ def test_constructor_series(self):
270270

271271
assert_series_equal(s2, s1.sort_index())
272272

273+
def test_constructor_generator(self):
274+
gen = (i for i in range(10))
275+
276+
result = Series(gen)
277+
exp = Series(range(10))
278+
assert_series_equal(result, exp)
279+
280+
gen = (i for i in range(10))
281+
result = Series(gen, index=range(10, 20))
282+
exp.index = range(10, 20)
283+
assert_series_equal(result, exp)
284+
273285
def test_constructor_maskedarray(self):
274286
data = ma.masked_all((3,), dtype=float)
275287
result = Series(data)

0 commit comments

Comments
 (0)