|
16 | 16 |
|
17 | 17 | import pandas as pd
|
18 | 18 | from pandas import (
|
19 |
| - Categorical, DataFrame, Index, MultiIndex, Series, Timedelta, Timestamp, |
20 |
| - compat, date_range, isna) |
| 19 | + Categorical, DataFrame, Index, MultiIndex, RangeIndex, Series, Timedelta, |
| 20 | + Timestamp, compat, date_range, isna) |
21 | 21 | from pandas.tests.frame.common import TestData
|
22 | 22 | import pandas.util.testing as tm
|
23 | 23 |
|
|
28 | 28 |
|
29 | 29 | class TestDataFrameConstructors(TestData):
|
30 | 30 |
|
31 |
| - def test_constructor(self): |
32 |
| - df = DataFrame() |
33 |
| - assert len(df.index) == 0 |
| 31 | + @pytest.mark.parametrize('constructor', [ |
| 32 | + lambda: DataFrame(), |
| 33 | + lambda: DataFrame(None), |
| 34 | + lambda: DataFrame({}), |
| 35 | + lambda: DataFrame(()), |
| 36 | + lambda: DataFrame([]), |
| 37 | + lambda: DataFrame((x for x in [])), |
| 38 | + lambda: DataFrame(data=None), |
| 39 | + lambda: DataFrame(data={}), |
| 40 | + lambda: DataFrame(data=()), |
| 41 | + lambda: DataFrame(data=[]), |
| 42 | + lambda: DataFrame(data=(x for x in [])) |
| 43 | + ]) |
| 44 | + def test_empty_constructor(self, constructor): |
| 45 | + expected = DataFrame() |
| 46 | + result = constructor() |
| 47 | + assert len(result.index) == 0 |
| 48 | + assert len(result.columns) == 0 |
| 49 | + tm.assert_frame_equal(result, expected) |
34 | 50 |
|
35 |
| - df = DataFrame(data={}) |
36 |
| - assert len(df.index) == 0 |
| 51 | + @pytest.mark.parametrize('emptylike,expected_index,expected_columns', [ |
| 52 | + ([[]], RangeIndex(1), RangeIndex(0)), |
| 53 | + ([[], []], RangeIndex(2), RangeIndex(0)), |
| 54 | + ([(x for x in [])], RangeIndex(1), RangeIndex(0)) |
| 55 | + ]) |
| 56 | + def test_emptylike_constructor( |
| 57 | + self, emptylike, expected_index, expected_columns): |
| 58 | + expected = DataFrame(index=expected_index, columns=expected_columns) |
| 59 | + result = DataFrame(emptylike) |
| 60 | + tm.assert_frame_equal(result, expected) |
37 | 61 |
|
38 | 62 | def test_constructor_mixed(self):
|
39 | 63 | index, data = tm.getMixedTypeDict()
|
@@ -91,7 +115,7 @@ def test_constructor_dtype_list_data(self):
|
91 | 115 |
|
92 | 116 | def test_constructor_list_frames(self):
|
93 | 117 | # see gh-3243
|
94 |
| - result = DataFrame([DataFrame([])]) |
| 118 | + result = DataFrame([DataFrame()]) |
95 | 119 | assert result.shape == (1, 0)
|
96 | 120 |
|
97 | 121 | result = DataFrame([DataFrame(dict(A=lrange(5)))])
|
@@ -258,7 +282,7 @@ def test_constructor_dict(self):
|
258 | 282 | frame = DataFrame({}, index=idx)
|
259 | 283 | assert frame.index is idx
|
260 | 284 |
|
261 |
| - # empty with index and columns |
| 285 | + # empty dict with index and columns |
262 | 286 | idx = Index([0, 1, 2])
|
263 | 287 | frame = DataFrame({}, index=idx, columns=idx)
|
264 | 288 | assert frame.index is idx
|
@@ -1105,7 +1129,7 @@ def test_constructor_list_of_series(self):
|
1105 | 1129 | result2 = DataFrame(data, index=np.arange(6))
|
1106 | 1130 | tm.assert_frame_equal(result, result2)
|
1107 | 1131 |
|
1108 |
| - result = DataFrame([Series({})]) |
| 1132 | + result = DataFrame([Series()]) |
1109 | 1133 | expected = DataFrame(index=[0])
|
1110 | 1134 | tm.assert_frame_equal(result, expected)
|
1111 | 1135 |
|
|
0 commit comments