Skip to content

Commit f789bb7

Browse files
committed
BUG: passing list of tuples to Series constructor failed, GH #270
1 parent b82a93f commit f789bb7

File tree

5 files changed

+29
-16
lines changed

5 files changed

+29
-16
lines changed

RELEASE.rst

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ feedback on the library.
146146

147147
**Bug fixes**
148148

149+
- `read_csv` / `read_table` fixes
150+
- Be less aggressive about converting float->int in cases of floating point
151+
representations of integers like 1.0, 2.0, etc.
152+
- "True"/"False" will not get correctly converted to boolean
153+
- Index name attribute will get set when specifying an index column
154+
- Passing column names should force `header=None` (GH #257)
155+
- Don't modify passed column names when `index_col` is not
156+
None (GH #258)
157+
- Can sniff CSV separator in zip file (since seek is not supported, was
158+
failing before)
149159
- Worked around matplotlib "bug" in which series[:, np.newaxis] fails. Should
150160
be reported upstream to matplotlib (GH #224)
151161
- DataFrame.iteritems was not returning Series with the name attribute
@@ -166,16 +176,7 @@ feedback on the library.
166176
- Implemented `MultiIndex.diff` (GH #260)
167177
- `Int64Index.take` and `MultiIndex.take` lost name field, fix downstream
168178
issue GH #262
169-
- `read_csv` / `read_table` fixes
170-
- Be less aggressive about converting float->int in cases of floating point
171-
representations of integers like 1.0, 2.0, etc.
172-
- "True"/"False" will not get correctly converted to boolean
173-
- Index name attribute will get set when specifying an index column
174-
- Passing column names should force `header=None` (GH #257)
175-
- Don't modify passed column names when `index_col` is not
176-
None (GH #258)
177-
- Can sniff CSV separator in zip file (since seek is not supported, was
178-
failing before)
179+
- Can pass list of tuples to `Series` (GH #270)
179180

180181
Thanks
181182
------

pandas/core/groupby.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,10 @@ def _aggregate_simple(self, func, *args, **kwargs):
760760
values = self.obj.values
761761
result = {}
762762
for k, v in self.primary.indices.iteritems():
763-
result[k] = func(values.take(v), *args, **kwargs)
763+
agged = func(values.take(v), *args, **kwargs)
764+
if isinstance(output, np.ndarray):
765+
raise Exception('Must produce aggregated value')
766+
result[k] = agged
764767

765768
return result
766769

@@ -771,6 +774,8 @@ def _aggregate_named(self, func, *args, **kwargs):
771774
grp = self.get_group(name)
772775
grp.name = name
773776
output = func(grp, *args, **kwargs)
777+
if isinstance(output, np.ndarray):
778+
raise Exception('Must produce aggregated value')
774779
result[name] = output
775780

776781
return result

pandas/core/series.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
import numpy as np
2020

2121
from pandas.core.common import (isnull, notnull, _is_bool_indexer,
22-
_default_index, _maybe_upcast)
22+
_default_index, _maybe_upcast,
23+
_asarray_tuplesafe)
2324
from pandas.core.daterange import DateRange
2425
from pandas.core.generic import PandasObject
2526
from pandas.core.index import Index, MultiIndex, _ensure_index
@@ -109,7 +110,6 @@ def __new__(cls, data, index=None, dtype=None, name=None, copy=False):
109110
index = Index(sorted(data.keys()))
110111
data = [data.get(idx, np.nan) for idx in index]
111112

112-
# Create array, do *not* copy data by default, infer type
113113
try:
114114
subarr = np.array(data, dtype=dtype, copy=copy)
115115
except ValueError:
@@ -139,9 +139,11 @@ def __new__(cls, data, index=None, dtype=None, name=None, copy=False):
139139
subarr.fill(value)
140140
else:
141141
return subarr.item()
142-
143142
elif subarr.ndim > 1:
144-
raise Exception('Data must be 1-dimensional')
143+
if isinstance(data, np.ndarray):
144+
raise Exception('Data must be 1-dimensional')
145+
else:
146+
subarr = _asarray_tuplesafe(data, dtype=dtype)
145147

146148
if index is None:
147149
index = _default_index(len(subarr))

pandas/tests/test_groupby.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def test_series_describe_multikey(self):
298298
def test_series_describe_single(self):
299299
ts = tm.makeTimeSeries()
300300
grouped = ts.groupby(lambda x: x.month)
301-
result = grouped.agg(lambda x: x.describe())
301+
result = grouped.apply(lambda x: x.describe())
302302
expected = grouped.describe()
303303
assert_frame_equal(result, expected)
304304

pandas/tests/test_series.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ def test_constructor_dict(self):
157157
expected = Series([1, 2, nan, 0], index=['b', 'c', 'd', 'a'])
158158
assert_series_equal(result, expected)
159159

160+
def test_constructor_tuples(self):
161+
data = [(1, 1), (2, 2), (2, 3)]
162+
s = Series(data)
163+
self.assertEqual(list(s), data)
164+
160165
def test_fromDict(self):
161166
data = {'a' : 0, 'b' : 1, 'c' : 2, 'd' : 3}
162167

0 commit comments

Comments
 (0)