|
5 | 5 |
|
6 | 6 | from datetime import datetime, timedelta
|
7 | 7 | from collections import OrderedDict
|
| 8 | +import types |
8 | 9 |
|
9 | 10 | from numpy import nan
|
10 | 11 | import numpy as np
|
|
22 | 23 | from pandas._libs import lib
|
23 | 24 | from pandas._libs.tslib import iNaT
|
24 | 25 |
|
| 26 | +from pandas import compat |
25 | 27 | from pandas.compat import lrange, range, zip, long
|
26 | 28 | from pandas.util.testing import assert_series_equal
|
27 | 29 | import pandas.util.testing as tm
|
@@ -393,6 +395,32 @@ def test_constructor_default_index(self):
|
393 | 395 | s = Series([0, 1, 2])
|
394 | 396 | tm.assert_index_equal(s.index, pd.Index(np.arange(3)))
|
395 | 397 |
|
| 398 | + @pytest.mark.parametrize('input', [[1, 2, 3], |
| 399 | + (1, 2, 3), |
| 400 | + list(range(3)), |
| 401 | + pd.Categorical(['a', 'b', 'a']), |
| 402 | + (i for i in range(3)), |
| 403 | + map(lambda x: x, range(3))]) |
| 404 | + def test_constructor_index_mismatch(self, input): |
| 405 | + # GH 19342 |
| 406 | + # test that construction of a Series with an index of different length |
| 407 | + # raises an error |
| 408 | + idx = np.arange(4) |
| 409 | + if compat.PY2: |
| 410 | + typs = types.GeneratorType |
| 411 | + else: |
| 412 | + typs = (map, types.GeneratorType) |
| 413 | + |
| 414 | + if isinstance(input, typs): |
| 415 | + input_len = len(list(input)) |
| 416 | + else: |
| 417 | + input_len = len(input) |
| 418 | + |
| 419 | + msg = ('Length of passed values is {val}, index implies {ind}' |
| 420 | + .format(val=input_len, ind=len(idx))) |
| 421 | + with pytest.raises(ValueError, message=msg): |
| 422 | + Series(input, index=idx) |
| 423 | + |
396 | 424 | def test_constructor_corner(self):
|
397 | 425 | df = tm.makeTimeDataFrame()
|
398 | 426 | objs = [df, df]
|
|
0 commit comments