Skip to content

Commit f6e87cd

Browse files
committed
restore
1 parent b3040f3 commit f6e87cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+38568
-0
lines changed

pandas/tests/io/json/__init__.py

Whitespace-only changes.

pandas/tests/io/json/conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pytest
2+
3+
4+
@pytest.fixture(params=["split", "records", "index", "columns", "values"])
5+
def orient(request):
6+
"""
7+
Fixture for orients excluding the table format.
8+
"""
9+
return request.param
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{"a": 1, "b": 2}
2+
{"a": 3, "b": 4}
3+
{"a": 5, "b": 6}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"A":{"2000-01-03T00:00:00":1.56808523,"2000-01-04T00:00:00":-0.2550111,"2000-01-05T00:00:00":1.51493992,"2000-01-06T00:00:00":-0.02765498,"2000-01-07T00:00:00":0.05951614},"B":{"2000-01-03T00:00:00":0.65727391,"2000-01-04T00:00:00":-0.08072427,"2000-01-05T00:00:00":0.11805825,"2000-01-06T00:00:00":0.44679743,"2000-01-07T00:00:00":-2.69652057},"C":{"2000-01-03T00:00:00":1.81021139,"2000-01-04T00:00:00":-0.03202878,"2000-01-05T00:00:00":1.629455,"2000-01-06T00:00:00":0.33192641,"2000-01-07T00:00:00":1.28163262},"D":{"2000-01-03T00:00:00":-0.17251653,"2000-01-04T00:00:00":-0.17581665,"2000-01-05T00:00:00":-1.31506612,"2000-01-06T00:00:00":-0.27885413,"2000-01-07T00:00:00":0.34703478},"date":{"2000-01-03T00:00:00":"1992-01-06T18:21:32.120000","2000-01-04T00:00:00":"1992-01-06T18:21:32.120000","2000-01-05T00:00:00":"1992-01-06T18:21:32.120000","2000-01-06T00:00:00":"2013-01-01T00:00:00","2000-01-07T00:00:00":"1992-01-06T18:21:32.120000"}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"A":{"946857600000000000":1.56808523,"946944000000000000":-0.2550111,"947030400000000000":1.51493992,"947116800000000000":-0.02765498,"947203200000000000":0.05951614},"B":{"946857600000000000":0.65727391,"946944000000000000":-0.08072427,"947030400000000000":0.11805825,"947116800000000000":0.44679743,"947203200000000000":-2.69652057},"C":{"946857600000000000":1.81021139,"946944000000000000":-0.03202878,"947030400000000000":1.629455,"947116800000000000":0.33192641,"947203200000000000":1.28163262},"D":{"946857600000000000":-0.17251653,"946944000000000000":-0.17581665,"947030400000000000":-1.31506612,"947116800000000000":-0.27885413,"947203200000000000":0.34703478},"date":{"946857600000000000":694722092120000000,"946944000000000000":694722092120000000,"947030400000000000":694722092120000000,"947116800000000000":1356998400000000000,"947203200000000000":694722092120000000},"modified":{"946857600000000000":694722092120000000,"946944000000000000":null,"947030400000000000":694722092120000000,"947116800000000000":1356998400000000000,"947203200000000000":694722092120000000}}
436 Bytes
Binary file not shown.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
from io import BytesIO
2+
3+
import pytest
4+
5+
import pandas.util._test_decorators as td
6+
7+
import pandas as pd
8+
import pandas._testing as tm
9+
10+
import pandas.io.common as icom
11+
12+
13+
def test_compression_roundtrip(compression):
14+
df = pd.DataFrame(
15+
[[0.123456, 0.234567, 0.567567], [12.32112, 123123.2, 321321.2]],
16+
index=["A", "B"],
17+
columns=["X", "Y", "Z"],
18+
)
19+
20+
with tm.ensure_clean() as path:
21+
df.to_json(path, compression=compression)
22+
tm.assert_frame_equal(df, pd.read_json(path, compression=compression))
23+
24+
# explicitly ensure file was compressed.
25+
with tm.decompress_file(path, compression) as fh:
26+
result = fh.read().decode("utf8")
27+
tm.assert_frame_equal(df, pd.read_json(result))
28+
29+
30+
def test_read_zipped_json(datapath):
31+
uncompressed_path = datapath("io", "json", "data", "tsframe_v012.json")
32+
uncompressed_df = pd.read_json(uncompressed_path)
33+
34+
compressed_path = datapath("io", "json", "data", "tsframe_v012.json.zip")
35+
compressed_df = pd.read_json(compressed_path, compression="zip")
36+
37+
tm.assert_frame_equal(uncompressed_df, compressed_df)
38+
39+
40+
@td.skip_if_not_us_locale
41+
@pytest.mark.xfail(
42+
reason="CI race condition GH 45433, GH 44584",
43+
raises=FileNotFoundError,
44+
strict=False,
45+
)
46+
def test_with_s3_url(compression, s3_resource, s3so):
47+
# Bucket "pandas-test" created in tests/io/conftest.py
48+
49+
df = pd.read_json('{"a": [1, 2, 3], "b": [4, 5, 6]}')
50+
51+
with tm.ensure_clean() as path:
52+
df.to_json(path, compression=compression)
53+
with open(path, "rb") as f:
54+
s3_resource.Bucket("pandas-test").put_object(Key="test-1", Body=f)
55+
56+
roundtripped_df = pd.read_json(
57+
"s3://pandas-test/test-1", compression=compression, storage_options=s3so
58+
)
59+
tm.assert_frame_equal(df, roundtripped_df)
60+
61+
62+
def test_lines_with_compression(compression):
63+
64+
with tm.ensure_clean() as path:
65+
df = pd.read_json('{"a": [1, 2, 3], "b": [4, 5, 6]}')
66+
df.to_json(path, orient="records", lines=True, compression=compression)
67+
roundtripped_df = pd.read_json(path, lines=True, compression=compression)
68+
tm.assert_frame_equal(df, roundtripped_df)
69+
70+
71+
def test_chunksize_with_compression(compression):
72+
73+
with tm.ensure_clean() as path:
74+
df = pd.read_json('{"a": ["foo", "bar", "baz"], "b": [4, 5, 6]}')
75+
df.to_json(path, orient="records", lines=True, compression=compression)
76+
77+
with pd.read_json(
78+
path, lines=True, chunksize=1, compression=compression
79+
) as res:
80+
roundtripped_df = pd.concat(res)
81+
tm.assert_frame_equal(df, roundtripped_df)
82+
83+
84+
def test_write_unsupported_compression_type():
85+
df = pd.read_json('{"a": [1, 2, 3], "b": [4, 5, 6]}')
86+
with tm.ensure_clean() as path:
87+
msg = "Unrecognized compression type: unsupported"
88+
with pytest.raises(ValueError, match=msg):
89+
df.to_json(path, compression="unsupported")
90+
91+
92+
def test_read_unsupported_compression_type():
93+
with tm.ensure_clean() as path:
94+
msg = "Unrecognized compression type: unsupported"
95+
with pytest.raises(ValueError, match=msg):
96+
pd.read_json(path, compression="unsupported")
97+
98+
99+
@pytest.mark.parametrize("to_infer", [True, False])
100+
@pytest.mark.parametrize("read_infer", [True, False])
101+
def test_to_json_compression(compression_only, read_infer, to_infer):
102+
# see gh-15008
103+
compression = compression_only
104+
105+
# We'll complete file extension subsequently.
106+
filename = "test."
107+
filename += icom._compression_to_extension[compression]
108+
109+
df = pd.DataFrame({"A": [1]})
110+
111+
to_compression = "infer" if to_infer else compression
112+
read_compression = "infer" if read_infer else compression
113+
114+
with tm.ensure_clean(filename) as path:
115+
df.to_json(path, compression=to_compression)
116+
result = pd.read_json(path, compression=read_compression)
117+
tm.assert_frame_equal(result, df)
118+
119+
120+
def test_to_json_compression_mode(compression):
121+
# GH 39985 (read_json does not support user-provided binary files)
122+
expected = pd.DataFrame({"A": [1]})
123+
124+
with BytesIO() as buffer:
125+
expected.to_json(buffer, compression=compression)
126+
# df = pd.read_json(buffer, compression=compression)
127+
# tm.assert_frame_equal(expected, df)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Tests for the deprecated keyword arguments for `read_json`.
3+
"""
4+
5+
import pandas as pd
6+
import pandas._testing as tm
7+
8+
from pandas.io.json import read_json
9+
10+
11+
def test_deprecated_kwargs():
12+
df = pd.DataFrame({"A": [2, 4, 6], "B": [3, 6, 9]}, index=[0, 1, 2])
13+
buf = df.to_json(orient="split")
14+
with tm.assert_produces_warning(FutureWarning):
15+
tm.assert_frame_equal(df, read_json(buf, "split"))
16+
buf = df.to_json(orient="columns")
17+
with tm.assert_produces_warning(FutureWarning):
18+
tm.assert_frame_equal(df, read_json(buf, "columns"))
19+
buf = df.to_json(orient="index")
20+
with tm.assert_produces_warning(FutureWarning):
21+
tm.assert_frame_equal(df, read_json(buf, "index"))
22+
23+
24+
def test_good_kwargs():
25+
df = pd.DataFrame({"A": [2, 4, 6], "B": [3, 6, 9]}, index=[0, 1, 2])
26+
with tm.assert_produces_warning(None):
27+
tm.assert_frame_equal(df, read_json(df.to_json(orient="split"), orient="split"))
28+
tm.assert_frame_equal(
29+
df, read_json(df.to_json(orient="columns"), orient="columns")
30+
)
31+
tm.assert_frame_equal(df, read_json(df.to_json(orient="index"), orient="index"))

0 commit comments

Comments
 (0)