Skip to content

Commit 867b288

Browse files
committed
ENH: make Series work with map objects the same way as generators
1 parent e5fe75e commit 867b288

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

doc/source/whatsnew/v0.15.2.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,30 @@ Enhancements
6969
- ``Timedelta`` arithmetic returns ``NotImplemented`` in unknown cases, allowing extensions by custom classes (:issue:`8813`).
7070
- ``Timedelta`` now supports arithemtic with ``numpy.ndarray`` objects of the appropriate dtype (numpy 1.8 or newer only) (:issue:`8884`).
7171
- Added ``Timedelta.to_timedelta64`` method to the public API (:issue:`8884`).
72+
- ``Series`` now works with map objects the same way as generators (:issue:`8909`).
73+
74+
previous behaviour:
75+
76+
.. code-block:: python
77+
78+
In [1]: pd.Series(map(lambda x: x, range(3)), index=range(10, 13))
79+
Out[1]:
80+
10 <map object at 0x7f817181d7f0>
81+
11 <map object at 0x7f817181d7f0>
82+
12 <map object at 0x7f817181d7f0>
83+
dtype: object
84+
85+
current behavior:
86+
87+
.. ipython:: python
88+
89+
In [2]: pd.Series(map(lambda x: x, range(3)), index=range(10, 13))
90+
Out[2]:
91+
10 0
92+
11 1
93+
12 2
94+
dtype: int64
95+
7296

7397
.. _whatsnew_0152.performance:
7498

pandas/core/series.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
183183
raise ValueError("cannot specify a dtype with a Categorical")
184184
if name is None:
185185
name = data.name
186-
elif isinstance(data, types.GeneratorType):
186+
elif (isinstance(data, types.GeneratorType) or
187+
(compat.PY3 and isinstance(data, map))):
187188
data = list(data)
188189
elif isinstance(data, (set, frozenset)):
189190
raise TypeError("{0!r} type is unordered"

pandas/tests/test_series.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,19 @@ def test_constructor_generator(self):
632632
exp.index = lrange(10, 20)
633633
assert_series_equal(result, exp)
634634

635+
def test_constructor_map(self):
636+
# GH8909
637+
m = map(lambda x: x, range(10))
638+
639+
result = Series(m)
640+
exp = Series(lrange(10))
641+
assert_series_equal(result, exp)
642+
643+
m = map(lambda x: x, range(10))
644+
result = Series(m, index=lrange(10, 20))
645+
exp.index = lrange(10, 20)
646+
assert_series_equal(result, exp)
647+
635648
def test_constructor_categorical(self):
636649
cat = pd.Categorical([0, 1, 2, 0, 1, 2], ['a', 'b', 'c'], fastpath=True)
637650
cat.name = 'foo'

0 commit comments

Comments
 (0)