|
4 | 4 | import pytest
|
5 | 5 |
|
6 | 6 | from datetime import datetime, timedelta
|
| 7 | +from collections import OrderedDict |
7 | 8 |
|
8 | 9 | from numpy import nan
|
9 | 10 | import numpy as np
|
@@ -79,17 +80,42 @@ def test_constructor(self):
|
79 | 80 | m = MultiIndex.from_arrays([[1, 2], [3, 4]])
|
80 | 81 | pytest.raises(NotImplementedError, Series, m)
|
81 | 82 |
|
82 |
| - def test_constructor_empty(self): |
| 83 | + @pytest.mark.parametrize('input_class', [list, dict, OrderedDict]) |
| 84 | + def test_constructor_empty(self, input_class): |
83 | 85 | empty = Series()
|
84 |
| - empty2 = Series([]) |
| 86 | + empty2 = Series(input_class()) |
85 | 87 |
|
86 |
| - # the are Index() and RangeIndex() which don't compare type equal |
| 88 | + # these are Index() and RangeIndex() which don't compare type equal |
87 | 89 | # but are just .equals
|
88 | 90 | assert_series_equal(empty, empty2, check_index_type=False)
|
89 | 91 |
|
90 |
| - empty = Series(index=lrange(10)) |
91 |
| - empty2 = Series(np.nan, index=lrange(10)) |
92 |
| - assert_series_equal(empty, empty2) |
| 92 | + # With explicit dtype: |
| 93 | + empty = Series(dtype='float64') |
| 94 | + empty2 = Series(input_class(), dtype='float64') |
| 95 | + assert_series_equal(empty, empty2, check_index_type=False) |
| 96 | + |
| 97 | + # GH 18515 : with dtype=category: |
| 98 | + empty = Series(dtype='category') |
| 99 | + empty2 = Series(input_class(), dtype='category') |
| 100 | + assert_series_equal(empty, empty2, check_index_type=False) |
| 101 | + |
| 102 | + if input_class is not list: |
| 103 | + # With index: |
| 104 | + empty = Series(index=lrange(10)) |
| 105 | + empty2 = Series(input_class(), index=lrange(10)) |
| 106 | + assert_series_equal(empty, empty2) |
| 107 | + |
| 108 | + # With index and dtype float64: |
| 109 | + empty = Series(np.nan, index=lrange(10)) |
| 110 | + empty2 = Series(input_class(), index=lrange(10), dtype='float64') |
| 111 | + assert_series_equal(empty, empty2) |
| 112 | + |
| 113 | + @pytest.mark.parametrize('input_arg', [np.nan, float('nan')]) |
| 114 | + def test_constructor_nan(self, input_arg): |
| 115 | + empty = Series(dtype='float64', index=lrange(10)) |
| 116 | + empty2 = Series(input_arg, index=lrange(10)) |
| 117 | + |
| 118 | + assert_series_equal(empty, empty2, check_index_type=False) |
93 | 119 |
|
94 | 120 | def test_constructor_series(self):
|
95 | 121 | index1 = ['d', 'b', 'a', 'c']
|
@@ -625,6 +651,21 @@ def test_constructor_dict(self):
|
625 | 651 | expected.iloc[1] = 1
|
626 | 652 | assert_series_equal(result, expected)
|
627 | 653 |
|
| 654 | + @pytest.mark.parametrize("value", [2, np.nan, None, float('nan')]) |
| 655 | + def test_constructor_dict_nan_key(self, value): |
| 656 | + # GH 18480 |
| 657 | + d = {1: 'a', value: 'b', float('nan'): 'c', 4: 'd'} |
| 658 | + result = Series(d).sort_values() |
| 659 | + expected = Series(['a', 'b', 'c', 'd'], index=[1, value, np.nan, 4]) |
| 660 | + assert_series_equal(result, expected) |
| 661 | + |
| 662 | + # MultiIndex: |
| 663 | + d = {(1, 1): 'a', (2, np.nan): 'b', (3, value): 'c'} |
| 664 | + result = Series(d).sort_values() |
| 665 | + expected = Series(['a', 'b', 'c'], |
| 666 | + index=Index([(1, 1), (2, np.nan), (3, value)])) |
| 667 | + assert_series_equal(result, expected) |
| 668 | + |
628 | 669 | def test_constructor_dict_datetime64_index(self):
|
629 | 670 | # GH 9456
|
630 | 671 |
|
@@ -658,8 +699,6 @@ def test_constructor_tuple_of_tuples(self):
|
658 | 699 | s = Series(data)
|
659 | 700 | assert tuple(s) == data
|
660 | 701 |
|
661 |
| - @pytest.mark.xfail(reason='GH 18480 (Series initialization from dict with ' |
662 |
| - 'NaN keys') |
663 | 702 | def test_constructor_dict_of_tuples(self):
|
664 | 703 | data = {(1, 2): 3,
|
665 | 704 | (None, 5): 6}
|
|
0 commit comments