Skip to content

Commit 755119a

Browse files
authored
TST: added tests for to_json when called on complex data (GH41174) (#43258)
1 parent 0df5b0b commit 755119a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

pandas/tests/io/json/test_pandas.py

+38
Original file line numberDiff line numberDiff line change
@@ -1782,3 +1782,41 @@ def e(self):
17821782
# JSON keys should be all non-callable non-underscore attributes, see GH-42768
17831783
series = Series([_TestObject(a=1, b=2, _c=3, d=4)])
17841784
assert json.loads(series.to_json()) == {"0": {"a": 1, "b": 2, "d": 4}}
1785+
1786+
@pytest.mark.parametrize(
1787+
"data,expected",
1788+
[
1789+
(
1790+
Series({0: -6 + 8j, 1: 0 + 1j, 2: 9 - 5j}),
1791+
'{"0":{"imag":8.0,"real":-6.0},'
1792+
'"1":{"imag":1.0,"real":0.0},'
1793+
'"2":{"imag":-5.0,"real":9.0}}',
1794+
),
1795+
(
1796+
Series({0: -9.39 + 0.66j, 1: 3.95 + 9.32j, 2: 4.03 - 0.17j}),
1797+
'{"0":{"imag":0.66,"real":-9.39},'
1798+
'"1":{"imag":9.32,"real":3.95},'
1799+
'"2":{"imag":-0.17,"real":4.03}}',
1800+
),
1801+
(
1802+
DataFrame([[-2 + 3j, -1 - 0j], [4 - 3j, -0 - 10j]]),
1803+
'{"0":{"0":{"imag":3.0,"real":-2.0},'
1804+
'"1":{"imag":-3.0,"real":4.0}},'
1805+
'"1":{"0":{"imag":0.0,"real":-1.0},'
1806+
'"1":{"imag":-10.0,"real":0.0}}}',
1807+
),
1808+
(
1809+
DataFrame(
1810+
[[-0.28 + 0.34j, -1.08 - 0.39j], [0.41 - 0.34j, -0.78 - 1.35j]]
1811+
),
1812+
'{"0":{"0":{"imag":0.34,"real":-0.28},'
1813+
'"1":{"imag":-0.34,"real":0.41}},'
1814+
'"1":{"0":{"imag":-0.39,"real":-1.08},'
1815+
'"1":{"imag":-1.35,"real":-0.78}}}',
1816+
),
1817+
],
1818+
)
1819+
def test_complex_data_tojson(self, data, expected):
1820+
# GH41174
1821+
result = data.to_json()
1822+
assert result == expected

0 commit comments

Comments
 (0)