|
9 | 9 | import pytest
|
10 | 10 | import pytz
|
11 | 11 |
|
12 |
| -from pandas.compat import is_platform_little_endian |
| 12 | +from pandas.compat import PY37, is_platform_little_endian |
13 | 13 | from pandas.compat.numpy import _is_numpy_dev
|
14 | 14 |
|
15 | 15 | from pandas.core.dtypes.common import is_integer_dtype
|
@@ -1364,6 +1364,46 @@ def test_constructor_list_of_namedtuples(self):
|
1364 | 1364 | result = DataFrame(tuples, columns=["y", "z"])
|
1365 | 1365 | tm.assert_frame_equal(result, expected)
|
1366 | 1366 |
|
| 1367 | + @pytest.mark.skipif(not PY37, reason="Requires Python >= 3.7") |
| 1368 | + def test_constructor_list_of_dataclasses(self): |
| 1369 | + # GH21910 |
| 1370 | + from dataclasses import make_dataclass |
| 1371 | + |
| 1372 | + Point = make_dataclass("Point", [("x", int), ("y", int)]) |
| 1373 | + |
| 1374 | + datas = [Point(0, 3), Point(1, 3)] |
| 1375 | + expected = DataFrame({"x": [0, 1], "y": [3, 3]}) |
| 1376 | + result = DataFrame(datas) |
| 1377 | + tm.assert_frame_equal(result, expected) |
| 1378 | + |
| 1379 | + @pytest.mark.skipif(not PY37, reason="Requires Python >= 3.7") |
| 1380 | + def test_constructor_list_of_dataclasses_with_varying_types(self): |
| 1381 | + # GH21910 |
| 1382 | + from dataclasses import make_dataclass |
| 1383 | + |
| 1384 | + # varying types |
| 1385 | + Point = make_dataclass("Point", [("x", int), ("y", int)]) |
| 1386 | + HLine = make_dataclass("HLine", [("x0", int), ("x1", int), ("y", int)]) |
| 1387 | + |
| 1388 | + datas = [Point(0, 3), HLine(1, 3, 3)] |
| 1389 | + |
| 1390 | + expected = DataFrame( |
| 1391 | + {"x": [0, np.nan], "y": [3, 3], "x0": [np.nan, 1], "x1": [np.nan, 3]} |
| 1392 | + ) |
| 1393 | + result = DataFrame(datas) |
| 1394 | + tm.assert_frame_equal(result, expected) |
| 1395 | + |
| 1396 | + @pytest.mark.skipif(not PY37, reason="Requires Python >= 3.7") |
| 1397 | + def test_constructor_list_of_dataclasses_error_thrown(self): |
| 1398 | + # GH21910 |
| 1399 | + from dataclasses import make_dataclass |
| 1400 | + |
| 1401 | + Point = make_dataclass("Point", [("x", int), ("y", int)]) |
| 1402 | + |
| 1403 | + # expect TypeError |
| 1404 | + with pytest.raises(TypeError): |
| 1405 | + DataFrame([Point(0, 0), {"x": 1, "y": 0}]) |
| 1406 | + |
1367 | 1407 | def test_constructor_list_of_dict_order(self):
|
1368 | 1408 | # GH10056
|
1369 | 1409 | data = [
|
|
0 commit comments