|
2 | 2 | from decimal import Decimal
|
3 | 3 |
|
4 | 4 | import isodate
|
| 5 | +import numpy as np |
5 | 6 | import pandas as pd
|
6 | 7 | import pytz
|
7 | 8 | from dateutil.tz import tzoffset, tzutc
|
|
14 | 15 |
|
15 | 16 |
|
16 | 17 | def test_pandas_parser():
|
17 |
| - dataframe = pd.DataFrame(data={"id": [1, 2], "name": ["english", "中国人"]}) |
| 18 | + test_cases = [ |
| 19 | + { |
| 20 | + "name": "Integer type only dataframe, cf issue 1678", |
| 21 | + "df_data": {"int": [1]}, |
| 22 | + "expected_header": ["int"], |
| 23 | + "expected_rows": [{"int": 1}], |
| 24 | + }, |
| 25 | + { |
| 26 | + "name": "Boolean type only dataframe, cf issue 1678", |
| 27 | + "df_data": {"bool": [True]}, |
| 28 | + "expected_header": ["bool"], |
| 29 | + "expected_rows": [{"bool": True}], |
| 30 | + }, |
| 31 | + { |
| 32 | + "name": "Mixed types dataframe, chinese characters", |
| 33 | + "df_data": {"id": [1, 2], "name": ["english", "中国人"]}, |
| 34 | + "expected_header": ["id", "name"], |
| 35 | + "expected_rows": [ |
| 36 | + {"id": 1, "name": "english"}, |
| 37 | + {"id": 2, "name": "中国人"}, |
| 38 | + ], |
| 39 | + }, |
| 40 | + ] |
| 41 | + for tc in test_cases: |
| 42 | + dataframe = pd.DataFrame(data=tc["df_data"]) |
| 43 | + |
| 44 | + with TableResource(data=dataframe) as resource: |
| 45 | + assert resource.header == tc["expected_header"], tc["name"] |
| 46 | + assert resource.read_rows() == tc["expected_rows"], tc["name"] |
| 47 | + |
| 48 | + |
| 49 | +def test_pandas_parser_with_nan(): |
| 50 | + dataframe = pd.DataFrame(data={"x": [np.nan]}) |
| 51 | + |
18 | 52 | with TableResource(data=dataframe) as resource:
|
19 |
| - assert resource.header == ["id", "name"] |
20 |
| - assert resource.read_rows() == [ |
21 |
| - {"id": 1, "name": "english"}, |
22 |
| - {"id": 2, "name": "中国人"}, |
23 |
| - ] |
| 53 | + test_name = 'np.nan converted to Decimal("NaN")' |
| 54 | + row = resource.read_rows()[0] |
| 55 | + assert row["x"].is_nan(), test_name |
24 | 56 |
|
25 | 57 |
|
26 | 58 | def test_pandas_parser_from_dataframe_with_primary_key_having_datetime():
|
|
0 commit comments