Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- parrot/share/terminfo/d/d216+ +0 -0
- parrot/share/terminfo/d/d216-unix +0 -0
- parrot/share/terminfo/d/d410-dg +0 -0
- parrot/share/terminfo/d/d411-7b-w +0 -0
- parrot/share/terminfo/d/d412-dg +0 -0
- parrot/share/terminfo/d/d413-unix-25 +0 -0
- parrot/share/terminfo/d/d461-w +0 -0
- parrot/share/terminfo/d/d463-dg +0 -0
- parrot/share/terminfo/d/d464-unix-w +0 -0
- parrot/share/terminfo/d/d577-dg +0 -0
- parrot/share/terminfo/d/d578-7b +0 -0
- parrot/share/terminfo/d/darwin-100x37-m +0 -0
- parrot/share/terminfo/d/darwin-160x64 +0 -0
- parrot/share/terminfo/d/dec-vt340 +0 -0
- parrot/share/terminfo/d/decansi +0 -0
- parrot/share/terminfo/d/decpro +0 -0
- parrot/share/terminfo/d/decwriter +0 -0
- parrot/share/terminfo/d/dg+color +0 -0
- parrot/share/terminfo/d/dg-generic +0 -0
- parrot/share/terminfo/d/dg200 +0 -0
- parrot/share/terminfo/d/dgkeys+8b +0 -0
- parrot/share/terminfo/d/dgunix+fixed +0 -0
- parrot/share/terminfo/d/diablo1720 +0 -0
- parrot/share/terminfo/d/diablo630 +0 -0
- parrot/share/terminfo/d/dku7102-sna +0 -0
- parrot/share/terminfo/d/dku7202 +0 -0
- parrot/share/terminfo/d/dmd1 +0 -0
- parrot/share/terminfo/d/dp3360 +0 -0
- parrot/share/terminfo/d/dt110 +0 -0
- parrot/share/terminfo/d/dw1 +0 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/conftest.py +242 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/generate_legacy_storage_files.py +342 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/json/__init__.py +0 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/__init__.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/conftest.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/test_compression.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/test_deprecated_kwargs.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/test_json_table_schema.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/test_json_table_schema_ext_dtype.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/test_normalize.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/test_pandas.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/test_readlines.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/test_ujson.cpython-310.pyc +0 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/json/conftest.py +9 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/json/test_compression.py +130 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/json/test_deprecated_kwargs.py +21 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/json/test_json_table_schema.py +873 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/json/test_json_table_schema_ext_dtype.py +317 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/json/test_normalize.py +907 -0
- vllm/lib/python3.10/site-packages/pandas/tests/io/json/test_pandas.py +2202 -0
parrot/share/terminfo/d/d216+
ADDED
|
Binary file (1.17 kB). View file
|
|
|
parrot/share/terminfo/d/d216-unix
ADDED
|
Binary file (1.17 kB). View file
|
|
|
parrot/share/terminfo/d/d410-dg
ADDED
|
Binary file (1.18 kB). View file
|
|
|
parrot/share/terminfo/d/d411-7b-w
ADDED
|
Binary file (1.61 kB). View file
|
|
|
parrot/share/terminfo/d/d412-dg
ADDED
|
Binary file (1.2 kB). View file
|
|
|
parrot/share/terminfo/d/d413-unix-25
ADDED
|
Binary file (1.36 kB). View file
|
|
|
parrot/share/terminfo/d/d461-w
ADDED
|
Binary file (1.54 kB). View file
|
|
|
parrot/share/terminfo/d/d463-dg
ADDED
|
Binary file (1.2 kB). View file
|
|
|
parrot/share/terminfo/d/d464-unix-w
ADDED
|
Binary file (1.34 kB). View file
|
|
|
parrot/share/terminfo/d/d577-dg
ADDED
|
Binary file (1.16 kB). View file
|
|
|
parrot/share/terminfo/d/d578-7b
ADDED
|
Binary file (1.53 kB). View file
|
|
|
parrot/share/terminfo/d/darwin-100x37-m
ADDED
|
Binary file (987 Bytes). View file
|
|
|
parrot/share/terminfo/d/darwin-160x64
ADDED
|
Binary file (1.22 kB). View file
|
|
|
parrot/share/terminfo/d/dec-vt340
ADDED
|
Binary file (1.31 kB). View file
|
|
|
parrot/share/terminfo/d/decansi
ADDED
|
Binary file (1.52 kB). View file
|
|
|
parrot/share/terminfo/d/decpro
ADDED
|
Binary file (543 Bytes). View file
|
|
|
parrot/share/terminfo/d/decwriter
ADDED
|
Binary file (354 Bytes). View file
|
|
|
parrot/share/terminfo/d/dg+color
ADDED
|
Binary file (1.4 kB). View file
|
|
|
parrot/share/terminfo/d/dg-generic
ADDED
|
Binary file (792 Bytes). View file
|
|
|
parrot/share/terminfo/d/dg200
ADDED
|
Binary file (434 Bytes). View file
|
|
|
parrot/share/terminfo/d/dgkeys+8b
ADDED
|
Binary file (1.03 kB). View file
|
|
|
parrot/share/terminfo/d/dgunix+fixed
ADDED
|
Binary file (1.05 kB). View file
|
|
|
parrot/share/terminfo/d/diablo1720
ADDED
|
Binary file (391 Bytes). View file
|
|
|
parrot/share/terminfo/d/diablo630
ADDED
|
Binary file (429 Bytes). View file
|
|
|
parrot/share/terminfo/d/dku7102-sna
ADDED
|
Binary file (1.44 kB). View file
|
|
|
parrot/share/terminfo/d/dku7202
ADDED
|
Binary file (1.5 kB). View file
|
|
|
parrot/share/terminfo/d/dmd1
ADDED
|
Binary file (547 Bytes). View file
|
|
|
parrot/share/terminfo/d/dp3360
ADDED
|
Binary file (371 Bytes). View file
|
|
|
parrot/share/terminfo/d/dt110
ADDED
|
Binary file (710 Bytes). View file
|
|
|
parrot/share/terminfo/d/dw1
ADDED
|
Binary file (338 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/pandas/tests/io/conftest.py
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import shlex
|
| 2 |
+
import subprocess
|
| 3 |
+
import time
|
| 4 |
+
import uuid
|
| 5 |
+
|
| 6 |
+
import pytest
|
| 7 |
+
|
| 8 |
+
from pandas.compat import (
|
| 9 |
+
is_ci_environment,
|
| 10 |
+
is_platform_arm,
|
| 11 |
+
is_platform_mac,
|
| 12 |
+
is_platform_windows,
|
| 13 |
+
)
|
| 14 |
+
import pandas.util._test_decorators as td
|
| 15 |
+
|
| 16 |
+
import pandas.io.common as icom
|
| 17 |
+
from pandas.io.parsers import read_csv
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@pytest.fixture
|
| 21 |
+
def compression_to_extension():
|
| 22 |
+
return {value: key for key, value in icom.extension_to_compression.items()}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@pytest.fixture
|
| 26 |
+
def tips_file(datapath):
|
| 27 |
+
"""Path to the tips dataset"""
|
| 28 |
+
return datapath("io", "data", "csv", "tips.csv")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@pytest.fixture
|
| 32 |
+
def jsonl_file(datapath):
|
| 33 |
+
"""Path to a JSONL dataset"""
|
| 34 |
+
return datapath("io", "parser", "data", "items.jsonl")
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@pytest.fixture
|
| 38 |
+
def salaries_table(datapath):
|
| 39 |
+
"""DataFrame with the salaries dataset"""
|
| 40 |
+
return read_csv(datapath("io", "parser", "data", "salaries.csv"), sep="\t")
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
@pytest.fixture
|
| 44 |
+
def feather_file(datapath):
|
| 45 |
+
return datapath("io", "data", "feather", "feather-0_3_1.feather")
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
@pytest.fixture
|
| 49 |
+
def xml_file(datapath):
|
| 50 |
+
return datapath("io", "data", "xml", "books.xml")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
@pytest.fixture
|
| 54 |
+
def s3_base(worker_id, monkeypatch):
|
| 55 |
+
"""
|
| 56 |
+
Fixture for mocking S3 interaction.
|
| 57 |
+
|
| 58 |
+
Sets up moto server in separate process locally
|
| 59 |
+
Return url for motoserver/moto CI service
|
| 60 |
+
"""
|
| 61 |
+
pytest.importorskip("s3fs")
|
| 62 |
+
pytest.importorskip("boto3")
|
| 63 |
+
|
| 64 |
+
# temporary workaround as moto fails for botocore >= 1.11 otherwise,
|
| 65 |
+
# see https://github.com/spulec/moto/issues/1924 & 1952
|
| 66 |
+
monkeypatch.setenv("AWS_ACCESS_KEY_ID", "foobar_key")
|
| 67 |
+
monkeypatch.setenv("AWS_SECRET_ACCESS_KEY", "foobar_secret")
|
| 68 |
+
if is_ci_environment():
|
| 69 |
+
if is_platform_arm() or is_platform_mac() or is_platform_windows():
|
| 70 |
+
# NOT RUN on Windows/macOS/ARM, only Ubuntu
|
| 71 |
+
# - subprocess in CI can cause timeouts
|
| 72 |
+
# - GitHub Actions do not support
|
| 73 |
+
# container services for the above OSs
|
| 74 |
+
# - CircleCI will probably hit the Docker rate pull limit
|
| 75 |
+
pytest.skip(
|
| 76 |
+
"S3 tests do not have a corresponding service in "
|
| 77 |
+
"Windows, macOS or ARM platforms"
|
| 78 |
+
)
|
| 79 |
+
else:
|
| 80 |
+
# set in .github/workflows/unit-tests.yml
|
| 81 |
+
yield "http://localhost:5000"
|
| 82 |
+
else:
|
| 83 |
+
requests = pytest.importorskip("requests")
|
| 84 |
+
pytest.importorskip("moto")
|
| 85 |
+
pytest.importorskip("flask") # server mode needs flask too
|
| 86 |
+
|
| 87 |
+
# Launching moto in server mode, i.e., as a separate process
|
| 88 |
+
# with an S3 endpoint on localhost
|
| 89 |
+
|
| 90 |
+
worker_id = "5" if worker_id == "master" else worker_id.lstrip("gw")
|
| 91 |
+
endpoint_port = f"555{worker_id}"
|
| 92 |
+
endpoint_uri = f"http://127.0.0.1:{endpoint_port}/"
|
| 93 |
+
|
| 94 |
+
# pipe to null to avoid logging in terminal
|
| 95 |
+
with subprocess.Popen(
|
| 96 |
+
shlex.split(f"moto_server s3 -p {endpoint_port}"),
|
| 97 |
+
stdout=subprocess.DEVNULL,
|
| 98 |
+
stderr=subprocess.DEVNULL,
|
| 99 |
+
) as proc:
|
| 100 |
+
timeout = 5
|
| 101 |
+
while timeout > 0:
|
| 102 |
+
try:
|
| 103 |
+
# OK to go once server is accepting connections
|
| 104 |
+
r = requests.get(endpoint_uri)
|
| 105 |
+
if r.ok:
|
| 106 |
+
break
|
| 107 |
+
except Exception:
|
| 108 |
+
pass
|
| 109 |
+
timeout -= 0.1
|
| 110 |
+
time.sleep(0.1)
|
| 111 |
+
yield endpoint_uri
|
| 112 |
+
|
| 113 |
+
proc.terminate()
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
@pytest.fixture
|
| 117 |
+
def s3so(s3_base):
|
| 118 |
+
return {"client_kwargs": {"endpoint_url": s3_base}}
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
@pytest.fixture
|
| 122 |
+
def s3_resource(s3_base):
|
| 123 |
+
import boto3
|
| 124 |
+
|
| 125 |
+
s3 = boto3.resource("s3", endpoint_url=s3_base)
|
| 126 |
+
return s3
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
@pytest.fixture
|
| 130 |
+
def s3_public_bucket(s3_resource):
|
| 131 |
+
bucket = s3_resource.Bucket(f"pandas-test-{uuid.uuid4()}")
|
| 132 |
+
bucket.create()
|
| 133 |
+
yield bucket
|
| 134 |
+
bucket.objects.delete()
|
| 135 |
+
bucket.delete()
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
@pytest.fixture
|
| 139 |
+
def s3_public_bucket_with_data(
|
| 140 |
+
s3_public_bucket, tips_file, jsonl_file, feather_file, xml_file
|
| 141 |
+
):
|
| 142 |
+
"""
|
| 143 |
+
The following datasets
|
| 144 |
+
are loaded.
|
| 145 |
+
|
| 146 |
+
- tips.csv
|
| 147 |
+
- tips.csv.gz
|
| 148 |
+
- tips.csv.bz2
|
| 149 |
+
- items.jsonl
|
| 150 |
+
"""
|
| 151 |
+
test_s3_files = [
|
| 152 |
+
("tips#1.csv", tips_file),
|
| 153 |
+
("tips.csv", tips_file),
|
| 154 |
+
("tips.csv.gz", tips_file + ".gz"),
|
| 155 |
+
("tips.csv.bz2", tips_file + ".bz2"),
|
| 156 |
+
("items.jsonl", jsonl_file),
|
| 157 |
+
("simple_dataset.feather", feather_file),
|
| 158 |
+
("books.xml", xml_file),
|
| 159 |
+
]
|
| 160 |
+
for s3_key, file_name in test_s3_files:
|
| 161 |
+
with open(file_name, "rb") as f:
|
| 162 |
+
s3_public_bucket.put_object(Key=s3_key, Body=f)
|
| 163 |
+
return s3_public_bucket
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
@pytest.fixture
|
| 167 |
+
def s3_private_bucket(s3_resource):
|
| 168 |
+
bucket = s3_resource.Bucket(f"cant_get_it-{uuid.uuid4()}")
|
| 169 |
+
bucket.create(ACL="private")
|
| 170 |
+
yield bucket
|
| 171 |
+
bucket.objects.delete()
|
| 172 |
+
bucket.delete()
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
@pytest.fixture
|
| 176 |
+
def s3_private_bucket_with_data(
|
| 177 |
+
s3_private_bucket, tips_file, jsonl_file, feather_file, xml_file
|
| 178 |
+
):
|
| 179 |
+
"""
|
| 180 |
+
The following datasets
|
| 181 |
+
are loaded.
|
| 182 |
+
|
| 183 |
+
- tips.csv
|
| 184 |
+
- tips.csv.gz
|
| 185 |
+
- tips.csv.bz2
|
| 186 |
+
- items.jsonl
|
| 187 |
+
"""
|
| 188 |
+
test_s3_files = [
|
| 189 |
+
("tips#1.csv", tips_file),
|
| 190 |
+
("tips.csv", tips_file),
|
| 191 |
+
("tips.csv.gz", tips_file + ".gz"),
|
| 192 |
+
("tips.csv.bz2", tips_file + ".bz2"),
|
| 193 |
+
("items.jsonl", jsonl_file),
|
| 194 |
+
("simple_dataset.feather", feather_file),
|
| 195 |
+
("books.xml", xml_file),
|
| 196 |
+
]
|
| 197 |
+
for s3_key, file_name in test_s3_files:
|
| 198 |
+
with open(file_name, "rb") as f:
|
| 199 |
+
s3_private_bucket.put_object(Key=s3_key, Body=f)
|
| 200 |
+
return s3_private_bucket
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
_compression_formats_params = [
|
| 204 |
+
(".no_compress", None),
|
| 205 |
+
("", None),
|
| 206 |
+
(".gz", "gzip"),
|
| 207 |
+
(".GZ", "gzip"),
|
| 208 |
+
(".bz2", "bz2"),
|
| 209 |
+
(".BZ2", "bz2"),
|
| 210 |
+
(".zip", "zip"),
|
| 211 |
+
(".ZIP", "zip"),
|
| 212 |
+
(".xz", "xz"),
|
| 213 |
+
(".XZ", "xz"),
|
| 214 |
+
pytest.param((".zst", "zstd"), marks=td.skip_if_no("zstandard")),
|
| 215 |
+
pytest.param((".ZST", "zstd"), marks=td.skip_if_no("zstandard")),
|
| 216 |
+
]
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
@pytest.fixture(params=_compression_formats_params[1:])
|
| 220 |
+
def compression_format(request):
|
| 221 |
+
return request.param
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
@pytest.fixture(params=_compression_formats_params)
|
| 225 |
+
def compression_ext(request):
|
| 226 |
+
return request.param[0]
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
@pytest.fixture(
|
| 230 |
+
params=[
|
| 231 |
+
"python",
|
| 232 |
+
pytest.param("pyarrow", marks=td.skip_if_no("pyarrow")),
|
| 233 |
+
]
|
| 234 |
+
)
|
| 235 |
+
def string_storage(request):
|
| 236 |
+
"""
|
| 237 |
+
Parametrized fixture for pd.options.mode.string_storage.
|
| 238 |
+
|
| 239 |
+
* 'python'
|
| 240 |
+
* 'pyarrow'
|
| 241 |
+
"""
|
| 242 |
+
return request.param
|
vllm/lib/python3.10/site-packages/pandas/tests/io/generate_legacy_storage_files.py
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
self-contained to write legacy storage pickle files
|
| 3 |
+
|
| 4 |
+
To use this script. Create an environment where you want
|
| 5 |
+
generate pickles, say its for 0.20.3, with your pandas clone
|
| 6 |
+
in ~/pandas
|
| 7 |
+
|
| 8 |
+
. activate pandas_0.20.3
|
| 9 |
+
cd ~/pandas/pandas
|
| 10 |
+
|
| 11 |
+
$ python -m tests.io.generate_legacy_storage_files \
|
| 12 |
+
tests/io/data/legacy_pickle/0.20.3/ pickle
|
| 13 |
+
|
| 14 |
+
This script generates a storage file for the current arch, system,
|
| 15 |
+
and python version
|
| 16 |
+
pandas version: 0.20.3
|
| 17 |
+
output dir : pandas/pandas/tests/io/data/legacy_pickle/0.20.3/
|
| 18 |
+
storage format: pickle
|
| 19 |
+
created pickle file: 0.20.3_x86_64_darwin_3.5.2.pickle
|
| 20 |
+
|
| 21 |
+
The idea here is you are using the *current* version of the
|
| 22 |
+
generate_legacy_storage_files with an *older* version of pandas to
|
| 23 |
+
generate a pickle file. We will then check this file into a current
|
| 24 |
+
branch, and test using test_pickle.py. This will load the *older*
|
| 25 |
+
pickles and test versus the current data that is generated
|
| 26 |
+
(with main). These are then compared.
|
| 27 |
+
|
| 28 |
+
If we have cases where we changed the signature (e.g. we renamed
|
| 29 |
+
offset -> freq in Timestamp). Then we have to conditionally execute
|
| 30 |
+
in the generate_legacy_storage_files.py to make it
|
| 31 |
+
run under the older AND the newer version.
|
| 32 |
+
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
from datetime import timedelta
|
| 36 |
+
import os
|
| 37 |
+
import pickle
|
| 38 |
+
import platform as pl
|
| 39 |
+
import sys
|
| 40 |
+
|
| 41 |
+
# Remove script directory from path, otherwise Python will try to
|
| 42 |
+
# import the JSON test directory as the json module
|
| 43 |
+
sys.path.pop(0)
|
| 44 |
+
|
| 45 |
+
import numpy as np
|
| 46 |
+
|
| 47 |
+
import pandas
|
| 48 |
+
from pandas import (
|
| 49 |
+
Categorical,
|
| 50 |
+
DataFrame,
|
| 51 |
+
Index,
|
| 52 |
+
MultiIndex,
|
| 53 |
+
NaT,
|
| 54 |
+
Period,
|
| 55 |
+
RangeIndex,
|
| 56 |
+
Series,
|
| 57 |
+
Timestamp,
|
| 58 |
+
bdate_range,
|
| 59 |
+
date_range,
|
| 60 |
+
interval_range,
|
| 61 |
+
period_range,
|
| 62 |
+
timedelta_range,
|
| 63 |
+
)
|
| 64 |
+
from pandas.arrays import SparseArray
|
| 65 |
+
|
| 66 |
+
from pandas.tseries.offsets import (
|
| 67 |
+
FY5253,
|
| 68 |
+
BusinessDay,
|
| 69 |
+
BusinessHour,
|
| 70 |
+
CustomBusinessDay,
|
| 71 |
+
DateOffset,
|
| 72 |
+
Day,
|
| 73 |
+
Easter,
|
| 74 |
+
Hour,
|
| 75 |
+
LastWeekOfMonth,
|
| 76 |
+
Minute,
|
| 77 |
+
MonthBegin,
|
| 78 |
+
MonthEnd,
|
| 79 |
+
QuarterBegin,
|
| 80 |
+
QuarterEnd,
|
| 81 |
+
SemiMonthBegin,
|
| 82 |
+
SemiMonthEnd,
|
| 83 |
+
Week,
|
| 84 |
+
WeekOfMonth,
|
| 85 |
+
YearBegin,
|
| 86 |
+
YearEnd,
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def _create_sp_series():
|
| 91 |
+
nan = np.nan
|
| 92 |
+
|
| 93 |
+
# nan-based
|
| 94 |
+
arr = np.arange(15, dtype=np.float64)
|
| 95 |
+
arr[7:12] = nan
|
| 96 |
+
arr[-1:] = nan
|
| 97 |
+
|
| 98 |
+
bseries = Series(SparseArray(arr, kind="block"))
|
| 99 |
+
bseries.name = "bseries"
|
| 100 |
+
return bseries
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def _create_sp_tsseries():
|
| 104 |
+
nan = np.nan
|
| 105 |
+
|
| 106 |
+
# nan-based
|
| 107 |
+
arr = np.arange(15, dtype=np.float64)
|
| 108 |
+
arr[7:12] = nan
|
| 109 |
+
arr[-1:] = nan
|
| 110 |
+
|
| 111 |
+
date_index = bdate_range("1/1/2011", periods=len(arr))
|
| 112 |
+
bseries = Series(SparseArray(arr, kind="block"), index=date_index)
|
| 113 |
+
bseries.name = "btsseries"
|
| 114 |
+
return bseries
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def _create_sp_frame():
|
| 118 |
+
nan = np.nan
|
| 119 |
+
|
| 120 |
+
data = {
|
| 121 |
+
"A": [nan, nan, nan, 0, 1, 2, 3, 4, 5, 6],
|
| 122 |
+
"B": [0, 1, 2, nan, nan, nan, 3, 4, 5, 6],
|
| 123 |
+
"C": np.arange(10).astype(np.int64),
|
| 124 |
+
"D": [0, 1, 2, 3, 4, 5, nan, nan, nan, nan],
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
dates = bdate_range("1/1/2011", periods=10)
|
| 128 |
+
return DataFrame(data, index=dates).apply(SparseArray)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def create_pickle_data():
|
| 132 |
+
"""create the pickle data"""
|
| 133 |
+
data = {
|
| 134 |
+
"A": [0.0, 1.0, 2.0, 3.0, np.nan],
|
| 135 |
+
"B": [0, 1, 0, 1, 0],
|
| 136 |
+
"C": ["foo1", "foo2", "foo3", "foo4", "foo5"],
|
| 137 |
+
"D": date_range("1/1/2009", periods=5),
|
| 138 |
+
"E": [0.0, 1, Timestamp("20100101"), "foo", 2.0],
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
scalars = {"timestamp": Timestamp("20130101"), "period": Period("2012", "M")}
|
| 142 |
+
|
| 143 |
+
index = {
|
| 144 |
+
"int": Index(np.arange(10)),
|
| 145 |
+
"date": date_range("20130101", periods=10),
|
| 146 |
+
"period": period_range("2013-01-01", freq="M", periods=10),
|
| 147 |
+
"float": Index(np.arange(10, dtype=np.float64)),
|
| 148 |
+
"uint": Index(np.arange(10, dtype=np.uint64)),
|
| 149 |
+
"timedelta": timedelta_range("00:00:00", freq="30min", periods=10),
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
index["range"] = RangeIndex(10)
|
| 153 |
+
|
| 154 |
+
index["interval"] = interval_range(0, periods=10)
|
| 155 |
+
|
| 156 |
+
mi = {
|
| 157 |
+
"reg2": MultiIndex.from_tuples(
|
| 158 |
+
tuple(
|
| 159 |
+
zip(
|
| 160 |
+
*[
|
| 161 |
+
["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
|
| 162 |
+
["one", "two", "one", "two", "one", "two", "one", "two"],
|
| 163 |
+
]
|
| 164 |
+
)
|
| 165 |
+
),
|
| 166 |
+
names=["first", "second"],
|
| 167 |
+
)
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
series = {
|
| 171 |
+
"float": Series(data["A"]),
|
| 172 |
+
"int": Series(data["B"]),
|
| 173 |
+
"mixed": Series(data["E"]),
|
| 174 |
+
"ts": Series(
|
| 175 |
+
np.arange(10).astype(np.int64), index=date_range("20130101", periods=10)
|
| 176 |
+
),
|
| 177 |
+
"mi": Series(
|
| 178 |
+
np.arange(5).astype(np.float64),
|
| 179 |
+
index=MultiIndex.from_tuples(
|
| 180 |
+
tuple(zip(*[[1, 1, 2, 2, 2], [3, 4, 3, 4, 5]])), names=["one", "two"]
|
| 181 |
+
),
|
| 182 |
+
),
|
| 183 |
+
"dup": Series(np.arange(5).astype(np.float64), index=["A", "B", "C", "D", "A"]),
|
| 184 |
+
"cat": Series(Categorical(["foo", "bar", "baz"])),
|
| 185 |
+
"dt": Series(date_range("20130101", periods=5)),
|
| 186 |
+
"dt_tz": Series(date_range("20130101", periods=5, tz="US/Eastern")),
|
| 187 |
+
"period": Series([Period("2000Q1")] * 5),
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
mixed_dup_df = DataFrame(data)
|
| 191 |
+
mixed_dup_df.columns = list("ABCDA")
|
| 192 |
+
frame = {
|
| 193 |
+
"float": DataFrame({"A": series["float"], "B": series["float"] + 1}),
|
| 194 |
+
"int": DataFrame({"A": series["int"], "B": series["int"] + 1}),
|
| 195 |
+
"mixed": DataFrame({k: data[k] for k in ["A", "B", "C", "D"]}),
|
| 196 |
+
"mi": DataFrame(
|
| 197 |
+
{"A": np.arange(5).astype(np.float64), "B": np.arange(5).astype(np.int64)},
|
| 198 |
+
index=MultiIndex.from_tuples(
|
| 199 |
+
tuple(
|
| 200 |
+
zip(
|
| 201 |
+
*[
|
| 202 |
+
["bar", "bar", "baz", "baz", "baz"],
|
| 203 |
+
["one", "two", "one", "two", "three"],
|
| 204 |
+
]
|
| 205 |
+
)
|
| 206 |
+
),
|
| 207 |
+
names=["first", "second"],
|
| 208 |
+
),
|
| 209 |
+
),
|
| 210 |
+
"dup": DataFrame(
|
| 211 |
+
np.arange(15).reshape(5, 3).astype(np.float64), columns=["A", "B", "A"]
|
| 212 |
+
),
|
| 213 |
+
"cat_onecol": DataFrame({"A": Categorical(["foo", "bar"])}),
|
| 214 |
+
"cat_and_float": DataFrame(
|
| 215 |
+
{
|
| 216 |
+
"A": Categorical(["foo", "bar", "baz"]),
|
| 217 |
+
"B": np.arange(3).astype(np.int64),
|
| 218 |
+
}
|
| 219 |
+
),
|
| 220 |
+
"mixed_dup": mixed_dup_df,
|
| 221 |
+
"dt_mixed_tzs": DataFrame(
|
| 222 |
+
{
|
| 223 |
+
"A": Timestamp("20130102", tz="US/Eastern"),
|
| 224 |
+
"B": Timestamp("20130603", tz="CET"),
|
| 225 |
+
},
|
| 226 |
+
index=range(5),
|
| 227 |
+
),
|
| 228 |
+
"dt_mixed2_tzs": DataFrame(
|
| 229 |
+
{
|
| 230 |
+
"A": Timestamp("20130102", tz="US/Eastern"),
|
| 231 |
+
"B": Timestamp("20130603", tz="CET"),
|
| 232 |
+
"C": Timestamp("20130603", tz="UTC"),
|
| 233 |
+
},
|
| 234 |
+
index=range(5),
|
| 235 |
+
),
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
cat = {
|
| 239 |
+
"int8": Categorical(list("abcdefg")),
|
| 240 |
+
"int16": Categorical(np.arange(1000)),
|
| 241 |
+
"int32": Categorical(np.arange(10000)),
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
timestamp = {
|
| 245 |
+
"normal": Timestamp("2011-01-01"),
|
| 246 |
+
"nat": NaT,
|
| 247 |
+
"tz": Timestamp("2011-01-01", tz="US/Eastern"),
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
off = {
|
| 251 |
+
"DateOffset": DateOffset(years=1),
|
| 252 |
+
"DateOffset_h_ns": DateOffset(hour=6, nanoseconds=5824),
|
| 253 |
+
"BusinessDay": BusinessDay(offset=timedelta(seconds=9)),
|
| 254 |
+
"BusinessHour": BusinessHour(normalize=True, n=6, end="15:14"),
|
| 255 |
+
"CustomBusinessDay": CustomBusinessDay(weekmask="Mon Fri"),
|
| 256 |
+
"SemiMonthBegin": SemiMonthBegin(day_of_month=9),
|
| 257 |
+
"SemiMonthEnd": SemiMonthEnd(day_of_month=24),
|
| 258 |
+
"MonthBegin": MonthBegin(1),
|
| 259 |
+
"MonthEnd": MonthEnd(1),
|
| 260 |
+
"QuarterBegin": QuarterBegin(1),
|
| 261 |
+
"QuarterEnd": QuarterEnd(1),
|
| 262 |
+
"Day": Day(1),
|
| 263 |
+
"YearBegin": YearBegin(1),
|
| 264 |
+
"YearEnd": YearEnd(1),
|
| 265 |
+
"Week": Week(1),
|
| 266 |
+
"Week_Tues": Week(2, normalize=False, weekday=1),
|
| 267 |
+
"WeekOfMonth": WeekOfMonth(week=3, weekday=4),
|
| 268 |
+
"LastWeekOfMonth": LastWeekOfMonth(n=1, weekday=3),
|
| 269 |
+
"FY5253": FY5253(n=2, weekday=6, startingMonth=7, variation="last"),
|
| 270 |
+
"Easter": Easter(),
|
| 271 |
+
"Hour": Hour(1),
|
| 272 |
+
"Minute": Minute(1),
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
return {
|
| 276 |
+
"series": series,
|
| 277 |
+
"frame": frame,
|
| 278 |
+
"index": index,
|
| 279 |
+
"scalars": scalars,
|
| 280 |
+
"mi": mi,
|
| 281 |
+
"sp_series": {"float": _create_sp_series(), "ts": _create_sp_tsseries()},
|
| 282 |
+
"sp_frame": {"float": _create_sp_frame()},
|
| 283 |
+
"cat": cat,
|
| 284 |
+
"timestamp": timestamp,
|
| 285 |
+
"offsets": off,
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
|
| 289 |
+
def platform_name():
|
| 290 |
+
return "_".join(
|
| 291 |
+
[
|
| 292 |
+
str(pandas.__version__),
|
| 293 |
+
str(pl.machine()),
|
| 294 |
+
str(pl.system().lower()),
|
| 295 |
+
str(pl.python_version()),
|
| 296 |
+
]
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
+
|
| 300 |
+
def write_legacy_pickles(output_dir):
|
| 301 |
+
version = pandas.__version__
|
| 302 |
+
|
| 303 |
+
print(
|
| 304 |
+
"This script generates a storage file for the current arch, system, "
|
| 305 |
+
"and python version"
|
| 306 |
+
)
|
| 307 |
+
print(f" pandas version: {version}")
|
| 308 |
+
print(f" output dir : {output_dir}")
|
| 309 |
+
print(" storage format: pickle")
|
| 310 |
+
|
| 311 |
+
pth = f"{platform_name()}.pickle"
|
| 312 |
+
|
| 313 |
+
with open(os.path.join(output_dir, pth), "wb") as fh:
|
| 314 |
+
pickle.dump(create_pickle_data(), fh, pickle.DEFAULT_PROTOCOL)
|
| 315 |
+
|
| 316 |
+
print(f"created pickle file: {pth}")
|
| 317 |
+
|
| 318 |
+
|
| 319 |
+
def write_legacy_file():
|
| 320 |
+
# force our cwd to be the first searched
|
| 321 |
+
sys.path.insert(0, "")
|
| 322 |
+
|
| 323 |
+
if not 3 <= len(sys.argv) <= 4:
|
| 324 |
+
sys.exit(
|
| 325 |
+
"Specify output directory and storage type: generate_legacy_"
|
| 326 |
+
"storage_files.py <output_dir> <storage_type> "
|
| 327 |
+
)
|
| 328 |
+
|
| 329 |
+
output_dir = str(sys.argv[1])
|
| 330 |
+
storage_type = str(sys.argv[2])
|
| 331 |
+
|
| 332 |
+
if not os.path.exists(output_dir):
|
| 333 |
+
os.mkdir(output_dir)
|
| 334 |
+
|
| 335 |
+
if storage_type == "pickle":
|
| 336 |
+
write_legacy_pickles(output_dir=output_dir)
|
| 337 |
+
else:
|
| 338 |
+
sys.exit("storage_type must be one of {'pickle'}")
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
if __name__ == "__main__":
|
| 342 |
+
write_legacy_file()
|
vllm/lib/python3.10/site-packages/pandas/tests/io/json/__init__.py
ADDED
|
File without changes
|
vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (170 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/conftest.cpython-310.pyc
ADDED
|
Binary file (438 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/test_compression.cpython-310.pyc
ADDED
|
Binary file (4.43 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/test_deprecated_kwargs.cpython-310.pyc
ADDED
|
Binary file (926 Bytes). View file
|
|
|
vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/test_json_table_schema.cpython-310.pyc
ADDED
|
Binary file (23.8 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/test_json_table_schema_ext_dtype.cpython-310.pyc
ADDED
|
Binary file (8.37 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/test_normalize.cpython-310.pyc
ADDED
|
Binary file (20 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/test_pandas.cpython-310.pyc
ADDED
|
Binary file (65.6 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/test_readlines.cpython-310.pyc
ADDED
|
Binary file (15 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/pandas/tests/io/json/__pycache__/test_ujson.cpython-310.pyc
ADDED
|
Binary file (35.5 kB). View file
|
|
|
vllm/lib/python3.10/site-packages/pandas/tests/io/json/conftest.py
ADDED
|
@@ -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
|
vllm/lib/python3.10/site-packages/pandas/tests/io/json/test_compression.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from io import (
|
| 2 |
+
BytesIO,
|
| 3 |
+
StringIO,
|
| 4 |
+
)
|
| 5 |
+
|
| 6 |
+
import pytest
|
| 7 |
+
|
| 8 |
+
import pandas.util._test_decorators as td
|
| 9 |
+
|
| 10 |
+
import pandas as pd
|
| 11 |
+
import pandas._testing as tm
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def test_compression_roundtrip(compression):
|
| 15 |
+
df = pd.DataFrame(
|
| 16 |
+
[[0.123456, 0.234567, 0.567567], [12.32112, 123123.2, 321321.2]],
|
| 17 |
+
index=["A", "B"],
|
| 18 |
+
columns=["X", "Y", "Z"],
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
with tm.ensure_clean() as path:
|
| 22 |
+
df.to_json(path, compression=compression)
|
| 23 |
+
tm.assert_frame_equal(df, pd.read_json(path, compression=compression))
|
| 24 |
+
|
| 25 |
+
# explicitly ensure file was compressed.
|
| 26 |
+
with tm.decompress_file(path, compression) as fh:
|
| 27 |
+
result = fh.read().decode("utf8")
|
| 28 |
+
data = StringIO(result)
|
| 29 |
+
tm.assert_frame_equal(df, pd.read_json(data))
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def test_read_zipped_json(datapath):
|
| 33 |
+
uncompressed_path = datapath("io", "json", "data", "tsframe_v012.json")
|
| 34 |
+
uncompressed_df = pd.read_json(uncompressed_path)
|
| 35 |
+
|
| 36 |
+
compressed_path = datapath("io", "json", "data", "tsframe_v012.json.zip")
|
| 37 |
+
compressed_df = pd.read_json(compressed_path, compression="zip")
|
| 38 |
+
|
| 39 |
+
tm.assert_frame_equal(uncompressed_df, compressed_df)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
@td.skip_if_not_us_locale
|
| 43 |
+
@pytest.mark.single_cpu
|
| 44 |
+
def test_with_s3_url(compression, s3_public_bucket, s3so):
|
| 45 |
+
# Bucket created in tests/io/conftest.py
|
| 46 |
+
df = pd.read_json(StringIO('{"a": [1, 2, 3], "b": [4, 5, 6]}'))
|
| 47 |
+
|
| 48 |
+
with tm.ensure_clean() as path:
|
| 49 |
+
df.to_json(path, compression=compression)
|
| 50 |
+
with open(path, "rb") as f:
|
| 51 |
+
s3_public_bucket.put_object(Key="test-1", Body=f)
|
| 52 |
+
|
| 53 |
+
roundtripped_df = pd.read_json(
|
| 54 |
+
f"s3://{s3_public_bucket.name}/test-1",
|
| 55 |
+
compression=compression,
|
| 56 |
+
storage_options=s3so,
|
| 57 |
+
)
|
| 58 |
+
tm.assert_frame_equal(df, roundtripped_df)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def test_lines_with_compression(compression):
|
| 62 |
+
with tm.ensure_clean() as path:
|
| 63 |
+
df = pd.read_json(StringIO('{"a": [1, 2, 3], "b": [4, 5, 6]}'))
|
| 64 |
+
df.to_json(path, orient="records", lines=True, compression=compression)
|
| 65 |
+
roundtripped_df = pd.read_json(path, lines=True, compression=compression)
|
| 66 |
+
tm.assert_frame_equal(df, roundtripped_df)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def test_chunksize_with_compression(compression):
|
| 70 |
+
with tm.ensure_clean() as path:
|
| 71 |
+
df = pd.read_json(StringIO('{"a": ["foo", "bar", "baz"], "b": [4, 5, 6]}'))
|
| 72 |
+
df.to_json(path, orient="records", lines=True, compression=compression)
|
| 73 |
+
|
| 74 |
+
with pd.read_json(
|
| 75 |
+
path, lines=True, chunksize=1, compression=compression
|
| 76 |
+
) as res:
|
| 77 |
+
roundtripped_df = pd.concat(res)
|
| 78 |
+
tm.assert_frame_equal(df, roundtripped_df)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def test_write_unsupported_compression_type():
|
| 82 |
+
df = pd.read_json(StringIO('{"a": [1, 2, 3], "b": [4, 5, 6]}'))
|
| 83 |
+
with tm.ensure_clean() as path:
|
| 84 |
+
msg = "Unrecognized compression type: unsupported"
|
| 85 |
+
with pytest.raises(ValueError, match=msg):
|
| 86 |
+
df.to_json(path, compression="unsupported")
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def test_read_unsupported_compression_type():
|
| 90 |
+
with tm.ensure_clean() as path:
|
| 91 |
+
msg = "Unrecognized compression type: unsupported"
|
| 92 |
+
with pytest.raises(ValueError, match=msg):
|
| 93 |
+
pd.read_json(path, compression="unsupported")
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
@pytest.mark.parametrize(
|
| 97 |
+
"infer_string", [False, pytest.param(True, marks=td.skip_if_no("pyarrow"))]
|
| 98 |
+
)
|
| 99 |
+
@pytest.mark.parametrize("to_infer", [True, False])
|
| 100 |
+
@pytest.mark.parametrize("read_infer", [True, False])
|
| 101 |
+
def test_to_json_compression(
|
| 102 |
+
compression_only, read_infer, to_infer, compression_to_extension, infer_string
|
| 103 |
+
):
|
| 104 |
+
with pd.option_context("future.infer_string", infer_string):
|
| 105 |
+
# see gh-15008
|
| 106 |
+
compression = compression_only
|
| 107 |
+
|
| 108 |
+
# We'll complete file extension subsequently.
|
| 109 |
+
filename = "test."
|
| 110 |
+
filename += compression_to_extension[compression]
|
| 111 |
+
|
| 112 |
+
df = pd.DataFrame({"A": [1]})
|
| 113 |
+
|
| 114 |
+
to_compression = "infer" if to_infer else compression
|
| 115 |
+
read_compression = "infer" if read_infer else compression
|
| 116 |
+
|
| 117 |
+
with tm.ensure_clean(filename) as path:
|
| 118 |
+
df.to_json(path, compression=to_compression)
|
| 119 |
+
result = pd.read_json(path, compression=read_compression)
|
| 120 |
+
tm.assert_frame_equal(result, df)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def test_to_json_compression_mode(compression):
|
| 124 |
+
# GH 39985 (read_json does not support user-provided binary files)
|
| 125 |
+
expected = pd.DataFrame({"A": [1]})
|
| 126 |
+
|
| 127 |
+
with BytesIO() as buffer:
|
| 128 |
+
expected.to_json(buffer, compression=compression)
|
| 129 |
+
# df = pd.read_json(buffer, compression=compression)
|
| 130 |
+
# tm.assert_frame_equal(expected, df)
|
vllm/lib/python3.10/site-packages/pandas/tests/io/json/test_deprecated_kwargs.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Tests for the deprecated keyword arguments for `read_json`.
|
| 3 |
+
"""
|
| 4 |
+
from io import StringIO
|
| 5 |
+
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import pandas._testing as tm
|
| 8 |
+
|
| 9 |
+
from pandas.io.json import read_json
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def test_good_kwargs():
|
| 13 |
+
df = pd.DataFrame({"A": [2, 4, 6], "B": [3, 6, 9]}, index=[0, 1, 2])
|
| 14 |
+
|
| 15 |
+
with tm.assert_produces_warning(None):
|
| 16 |
+
data1 = StringIO(df.to_json(orient="split"))
|
| 17 |
+
tm.assert_frame_equal(df, read_json(data1, orient="split"))
|
| 18 |
+
data2 = StringIO(df.to_json(orient="columns"))
|
| 19 |
+
tm.assert_frame_equal(df, read_json(data2, orient="columns"))
|
| 20 |
+
data3 = StringIO(df.to_json(orient="index"))
|
| 21 |
+
tm.assert_frame_equal(df, read_json(data3, orient="index"))
|
vllm/lib/python3.10/site-packages/pandas/tests/io/json/test_json_table_schema.py
ADDED
|
@@ -0,0 +1,873 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for Table Schema integration."""
|
| 2 |
+
from collections import OrderedDict
|
| 3 |
+
from io import StringIO
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
import pytest
|
| 8 |
+
|
| 9 |
+
from pandas.core.dtypes.dtypes import (
|
| 10 |
+
CategoricalDtype,
|
| 11 |
+
DatetimeTZDtype,
|
| 12 |
+
PeriodDtype,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
import pandas as pd
|
| 16 |
+
from pandas import DataFrame
|
| 17 |
+
import pandas._testing as tm
|
| 18 |
+
|
| 19 |
+
from pandas.io.json._table_schema import (
|
| 20 |
+
as_json_table_type,
|
| 21 |
+
build_table_schema,
|
| 22 |
+
convert_json_field_to_pandas_type,
|
| 23 |
+
convert_pandas_type_to_json_field,
|
| 24 |
+
set_default_names,
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
@pytest.fixture
|
| 29 |
+
def df_schema():
|
| 30 |
+
return DataFrame(
|
| 31 |
+
{
|
| 32 |
+
"A": [1, 2, 3, 4],
|
| 33 |
+
"B": ["a", "b", "c", "c"],
|
| 34 |
+
"C": pd.date_range("2016-01-01", freq="d", periods=4),
|
| 35 |
+
"D": pd.timedelta_range("1h", periods=4, freq="min"),
|
| 36 |
+
},
|
| 37 |
+
index=pd.Index(range(4), name="idx"),
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
@pytest.fixture
|
| 42 |
+
def df_table():
|
| 43 |
+
return DataFrame(
|
| 44 |
+
{
|
| 45 |
+
"A": [1, 2, 3, 4],
|
| 46 |
+
"B": ["a", "b", "c", "c"],
|
| 47 |
+
"C": pd.date_range("2016-01-01", freq="d", periods=4),
|
| 48 |
+
"D": pd.timedelta_range("1h", periods=4, freq="min"),
|
| 49 |
+
"E": pd.Series(pd.Categorical(["a", "b", "c", "c"])),
|
| 50 |
+
"F": pd.Series(pd.Categorical(["a", "b", "c", "c"], ordered=True)),
|
| 51 |
+
"G": [1.0, 2.0, 3, 4.0],
|
| 52 |
+
"H": pd.date_range("2016-01-01", freq="d", periods=4, tz="US/Central"),
|
| 53 |
+
},
|
| 54 |
+
index=pd.Index(range(4), name="idx"),
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
class TestBuildSchema:
|
| 59 |
+
def test_build_table_schema(self, df_schema, using_infer_string):
|
| 60 |
+
result = build_table_schema(df_schema, version=False)
|
| 61 |
+
expected = {
|
| 62 |
+
"fields": [
|
| 63 |
+
{"name": "idx", "type": "integer"},
|
| 64 |
+
{"name": "A", "type": "integer"},
|
| 65 |
+
{"name": "B", "type": "string"},
|
| 66 |
+
{"name": "C", "type": "datetime"},
|
| 67 |
+
{"name": "D", "type": "duration"},
|
| 68 |
+
],
|
| 69 |
+
"primaryKey": ["idx"],
|
| 70 |
+
}
|
| 71 |
+
if using_infer_string:
|
| 72 |
+
expected["fields"][2] = {"name": "B", "type": "any", "extDtype": "string"}
|
| 73 |
+
assert result == expected
|
| 74 |
+
result = build_table_schema(df_schema)
|
| 75 |
+
assert "pandas_version" in result
|
| 76 |
+
|
| 77 |
+
def test_series(self):
|
| 78 |
+
s = pd.Series([1, 2, 3], name="foo")
|
| 79 |
+
result = build_table_schema(s, version=False)
|
| 80 |
+
expected = {
|
| 81 |
+
"fields": [
|
| 82 |
+
{"name": "index", "type": "integer"},
|
| 83 |
+
{"name": "foo", "type": "integer"},
|
| 84 |
+
],
|
| 85 |
+
"primaryKey": ["index"],
|
| 86 |
+
}
|
| 87 |
+
assert result == expected
|
| 88 |
+
result = build_table_schema(s)
|
| 89 |
+
assert "pandas_version" in result
|
| 90 |
+
|
| 91 |
+
def test_series_unnamed(self):
|
| 92 |
+
result = build_table_schema(pd.Series([1, 2, 3]), version=False)
|
| 93 |
+
expected = {
|
| 94 |
+
"fields": [
|
| 95 |
+
{"name": "index", "type": "integer"},
|
| 96 |
+
{"name": "values", "type": "integer"},
|
| 97 |
+
],
|
| 98 |
+
"primaryKey": ["index"],
|
| 99 |
+
}
|
| 100 |
+
assert result == expected
|
| 101 |
+
|
| 102 |
+
def test_multiindex(self, df_schema, using_infer_string):
|
| 103 |
+
df = df_schema
|
| 104 |
+
idx = pd.MultiIndex.from_product([("a", "b"), (1, 2)])
|
| 105 |
+
df.index = idx
|
| 106 |
+
|
| 107 |
+
result = build_table_schema(df, version=False)
|
| 108 |
+
expected = {
|
| 109 |
+
"fields": [
|
| 110 |
+
{"name": "level_0", "type": "string"},
|
| 111 |
+
{"name": "level_1", "type": "integer"},
|
| 112 |
+
{"name": "A", "type": "integer"},
|
| 113 |
+
{"name": "B", "type": "string"},
|
| 114 |
+
{"name": "C", "type": "datetime"},
|
| 115 |
+
{"name": "D", "type": "duration"},
|
| 116 |
+
],
|
| 117 |
+
"primaryKey": ["level_0", "level_1"],
|
| 118 |
+
}
|
| 119 |
+
if using_infer_string:
|
| 120 |
+
expected["fields"][0] = {
|
| 121 |
+
"name": "level_0",
|
| 122 |
+
"type": "any",
|
| 123 |
+
"extDtype": "string",
|
| 124 |
+
}
|
| 125 |
+
expected["fields"][3] = {"name": "B", "type": "any", "extDtype": "string"}
|
| 126 |
+
assert result == expected
|
| 127 |
+
|
| 128 |
+
df.index.names = ["idx0", None]
|
| 129 |
+
expected["fields"][0]["name"] = "idx0"
|
| 130 |
+
expected["primaryKey"] = ["idx0", "level_1"]
|
| 131 |
+
result = build_table_schema(df, version=False)
|
| 132 |
+
assert result == expected
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
class TestTableSchemaType:
|
| 136 |
+
@pytest.mark.parametrize("int_type", [int, np.int16, np.int32, np.int64])
|
| 137 |
+
def test_as_json_table_type_int_data(self, int_type):
|
| 138 |
+
int_data = [1, 2, 3]
|
| 139 |
+
assert as_json_table_type(np.array(int_data, dtype=int_type).dtype) == "integer"
|
| 140 |
+
|
| 141 |
+
@pytest.mark.parametrize("float_type", [float, np.float16, np.float32, np.float64])
|
| 142 |
+
def test_as_json_table_type_float_data(self, float_type):
|
| 143 |
+
float_data = [1.0, 2.0, 3.0]
|
| 144 |
+
assert (
|
| 145 |
+
as_json_table_type(np.array(float_data, dtype=float_type).dtype) == "number"
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
@pytest.mark.parametrize("bool_type", [bool, np.bool_])
|
| 149 |
+
def test_as_json_table_type_bool_data(self, bool_type):
|
| 150 |
+
bool_data = [True, False]
|
| 151 |
+
assert (
|
| 152 |
+
as_json_table_type(np.array(bool_data, dtype=bool_type).dtype) == "boolean"
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
@pytest.mark.parametrize(
|
| 156 |
+
"date_data",
|
| 157 |
+
[
|
| 158 |
+
pd.to_datetime(["2016"]),
|
| 159 |
+
pd.to_datetime(["2016"], utc=True),
|
| 160 |
+
pd.Series(pd.to_datetime(["2016"])),
|
| 161 |
+
pd.Series(pd.to_datetime(["2016"], utc=True)),
|
| 162 |
+
pd.period_range("2016", freq="Y", periods=3),
|
| 163 |
+
],
|
| 164 |
+
)
|
| 165 |
+
def test_as_json_table_type_date_data(self, date_data):
|
| 166 |
+
assert as_json_table_type(date_data.dtype) == "datetime"
|
| 167 |
+
|
| 168 |
+
@pytest.mark.parametrize(
|
| 169 |
+
"str_data",
|
| 170 |
+
[pd.Series(["a", "b"], dtype=object), pd.Index(["a", "b"], dtype=object)],
|
| 171 |
+
)
|
| 172 |
+
def test_as_json_table_type_string_data(self, str_data):
|
| 173 |
+
assert as_json_table_type(str_data.dtype) == "string"
|
| 174 |
+
|
| 175 |
+
@pytest.mark.parametrize(
|
| 176 |
+
"cat_data",
|
| 177 |
+
[
|
| 178 |
+
pd.Categorical(["a"]),
|
| 179 |
+
pd.Categorical([1]),
|
| 180 |
+
pd.Series(pd.Categorical([1])),
|
| 181 |
+
pd.CategoricalIndex([1]),
|
| 182 |
+
pd.Categorical([1]),
|
| 183 |
+
],
|
| 184 |
+
)
|
| 185 |
+
def test_as_json_table_type_categorical_data(self, cat_data):
|
| 186 |
+
assert as_json_table_type(cat_data.dtype) == "any"
|
| 187 |
+
|
| 188 |
+
# ------
|
| 189 |
+
# dtypes
|
| 190 |
+
# ------
|
| 191 |
+
@pytest.mark.parametrize("int_dtype", [int, np.int16, np.int32, np.int64])
|
| 192 |
+
def test_as_json_table_type_int_dtypes(self, int_dtype):
|
| 193 |
+
assert as_json_table_type(int_dtype) == "integer"
|
| 194 |
+
|
| 195 |
+
@pytest.mark.parametrize("float_dtype", [float, np.float16, np.float32, np.float64])
|
| 196 |
+
def test_as_json_table_type_float_dtypes(self, float_dtype):
|
| 197 |
+
assert as_json_table_type(float_dtype) == "number"
|
| 198 |
+
|
| 199 |
+
@pytest.mark.parametrize("bool_dtype", [bool, np.bool_])
|
| 200 |
+
def test_as_json_table_type_bool_dtypes(self, bool_dtype):
|
| 201 |
+
assert as_json_table_type(bool_dtype) == "boolean"
|
| 202 |
+
|
| 203 |
+
@pytest.mark.parametrize(
|
| 204 |
+
"date_dtype",
|
| 205 |
+
[
|
| 206 |
+
np.dtype("<M8[ns]"),
|
| 207 |
+
PeriodDtype("D"),
|
| 208 |
+
DatetimeTZDtype("ns", "US/Central"),
|
| 209 |
+
],
|
| 210 |
+
)
|
| 211 |
+
def test_as_json_table_type_date_dtypes(self, date_dtype):
|
| 212 |
+
# TODO: datedate.date? datetime.time?
|
| 213 |
+
assert as_json_table_type(date_dtype) == "datetime"
|
| 214 |
+
|
| 215 |
+
@pytest.mark.parametrize("td_dtype", [np.dtype("<m8[ns]")])
|
| 216 |
+
def test_as_json_table_type_timedelta_dtypes(self, td_dtype):
|
| 217 |
+
assert as_json_table_type(td_dtype) == "duration"
|
| 218 |
+
|
| 219 |
+
@pytest.mark.parametrize("str_dtype", [object]) # TODO(GH#14904) flesh out dtypes?
|
| 220 |
+
def test_as_json_table_type_string_dtypes(self, str_dtype):
|
| 221 |
+
assert as_json_table_type(str_dtype) == "string"
|
| 222 |
+
|
| 223 |
+
def test_as_json_table_type_categorical_dtypes(self):
|
| 224 |
+
assert as_json_table_type(pd.Categorical(["a"]).dtype) == "any"
|
| 225 |
+
assert as_json_table_type(CategoricalDtype()) == "any"
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
class TestTableOrient:
|
| 229 |
+
def test_build_series(self):
|
| 230 |
+
s = pd.Series([1, 2], name="a")
|
| 231 |
+
s.index.name = "id"
|
| 232 |
+
result = s.to_json(orient="table", date_format="iso")
|
| 233 |
+
result = json.loads(result, object_pairs_hook=OrderedDict)
|
| 234 |
+
|
| 235 |
+
assert "pandas_version" in result["schema"]
|
| 236 |
+
result["schema"].pop("pandas_version")
|
| 237 |
+
|
| 238 |
+
fields = [{"name": "id", "type": "integer"}, {"name": "a", "type": "integer"}]
|
| 239 |
+
|
| 240 |
+
schema = {"fields": fields, "primaryKey": ["id"]}
|
| 241 |
+
|
| 242 |
+
expected = OrderedDict(
|
| 243 |
+
[
|
| 244 |
+
("schema", schema),
|
| 245 |
+
(
|
| 246 |
+
"data",
|
| 247 |
+
[
|
| 248 |
+
OrderedDict([("id", 0), ("a", 1)]),
|
| 249 |
+
OrderedDict([("id", 1), ("a", 2)]),
|
| 250 |
+
],
|
| 251 |
+
),
|
| 252 |
+
]
|
| 253 |
+
)
|
| 254 |
+
|
| 255 |
+
assert result == expected
|
| 256 |
+
|
| 257 |
+
def test_read_json_from_to_json_results(self):
|
| 258 |
+
# GH32383
|
| 259 |
+
df = DataFrame(
|
| 260 |
+
{
|
| 261 |
+
"_id": {"row_0": 0},
|
| 262 |
+
"category": {"row_0": "Goods"},
|
| 263 |
+
"recommender_id": {"row_0": 3},
|
| 264 |
+
"recommender_name_jp": {"row_0": "浦田"},
|
| 265 |
+
"recommender_name_en": {"row_0": "Urata"},
|
| 266 |
+
"name_jp": {"row_0": "博多人形(松尾吉将まつお よしまさ)"},
|
| 267 |
+
"name_en": {"row_0": "Hakata Dolls Matsuo"},
|
| 268 |
+
}
|
| 269 |
+
)
|
| 270 |
+
|
| 271 |
+
result1 = pd.read_json(StringIO(df.to_json()))
|
| 272 |
+
result2 = DataFrame.from_dict(json.loads(df.to_json()))
|
| 273 |
+
tm.assert_frame_equal(result1, df)
|
| 274 |
+
tm.assert_frame_equal(result2, df)
|
| 275 |
+
|
| 276 |
+
def test_to_json(self, df_table, using_infer_string):
|
| 277 |
+
df = df_table
|
| 278 |
+
df.index.name = "idx"
|
| 279 |
+
result = df.to_json(orient="table", date_format="iso")
|
| 280 |
+
result = json.loads(result, object_pairs_hook=OrderedDict)
|
| 281 |
+
|
| 282 |
+
assert "pandas_version" in result["schema"]
|
| 283 |
+
result["schema"].pop("pandas_version")
|
| 284 |
+
|
| 285 |
+
fields = [
|
| 286 |
+
{"name": "idx", "type": "integer"},
|
| 287 |
+
{"name": "A", "type": "integer"},
|
| 288 |
+
{"name": "B", "type": "string"},
|
| 289 |
+
{"name": "C", "type": "datetime"},
|
| 290 |
+
{"name": "D", "type": "duration"},
|
| 291 |
+
{
|
| 292 |
+
"constraints": {"enum": ["a", "b", "c"]},
|
| 293 |
+
"name": "E",
|
| 294 |
+
"ordered": False,
|
| 295 |
+
"type": "any",
|
| 296 |
+
},
|
| 297 |
+
{
|
| 298 |
+
"constraints": {"enum": ["a", "b", "c"]},
|
| 299 |
+
"name": "F",
|
| 300 |
+
"ordered": True,
|
| 301 |
+
"type": "any",
|
| 302 |
+
},
|
| 303 |
+
{"name": "G", "type": "number"},
|
| 304 |
+
{"name": "H", "type": "datetime", "tz": "US/Central"},
|
| 305 |
+
]
|
| 306 |
+
|
| 307 |
+
if using_infer_string:
|
| 308 |
+
fields[2] = {"name": "B", "type": "any", "extDtype": "string"}
|
| 309 |
+
|
| 310 |
+
schema = {"fields": fields, "primaryKey": ["idx"]}
|
| 311 |
+
data = [
|
| 312 |
+
OrderedDict(
|
| 313 |
+
[
|
| 314 |
+
("idx", 0),
|
| 315 |
+
("A", 1),
|
| 316 |
+
("B", "a"),
|
| 317 |
+
("C", "2016-01-01T00:00:00.000"),
|
| 318 |
+
("D", "P0DT1H0M0S"),
|
| 319 |
+
("E", "a"),
|
| 320 |
+
("F", "a"),
|
| 321 |
+
("G", 1.0),
|
| 322 |
+
("H", "2016-01-01T06:00:00.000Z"),
|
| 323 |
+
]
|
| 324 |
+
),
|
| 325 |
+
OrderedDict(
|
| 326 |
+
[
|
| 327 |
+
("idx", 1),
|
| 328 |
+
("A", 2),
|
| 329 |
+
("B", "b"),
|
| 330 |
+
("C", "2016-01-02T00:00:00.000"),
|
| 331 |
+
("D", "P0DT1H1M0S"),
|
| 332 |
+
("E", "b"),
|
| 333 |
+
("F", "b"),
|
| 334 |
+
("G", 2.0),
|
| 335 |
+
("H", "2016-01-02T06:00:00.000Z"),
|
| 336 |
+
]
|
| 337 |
+
),
|
| 338 |
+
OrderedDict(
|
| 339 |
+
[
|
| 340 |
+
("idx", 2),
|
| 341 |
+
("A", 3),
|
| 342 |
+
("B", "c"),
|
| 343 |
+
("C", "2016-01-03T00:00:00.000"),
|
| 344 |
+
("D", "P0DT1H2M0S"),
|
| 345 |
+
("E", "c"),
|
| 346 |
+
("F", "c"),
|
| 347 |
+
("G", 3.0),
|
| 348 |
+
("H", "2016-01-03T06:00:00.000Z"),
|
| 349 |
+
]
|
| 350 |
+
),
|
| 351 |
+
OrderedDict(
|
| 352 |
+
[
|
| 353 |
+
("idx", 3),
|
| 354 |
+
("A", 4),
|
| 355 |
+
("B", "c"),
|
| 356 |
+
("C", "2016-01-04T00:00:00.000"),
|
| 357 |
+
("D", "P0DT1H3M0S"),
|
| 358 |
+
("E", "c"),
|
| 359 |
+
("F", "c"),
|
| 360 |
+
("G", 4.0),
|
| 361 |
+
("H", "2016-01-04T06:00:00.000Z"),
|
| 362 |
+
]
|
| 363 |
+
),
|
| 364 |
+
]
|
| 365 |
+
expected = OrderedDict([("schema", schema), ("data", data)])
|
| 366 |
+
|
| 367 |
+
assert result == expected
|
| 368 |
+
|
| 369 |
+
def test_to_json_float_index(self):
|
| 370 |
+
data = pd.Series(1, index=[1.0, 2.0])
|
| 371 |
+
result = data.to_json(orient="table", date_format="iso")
|
| 372 |
+
result = json.loads(result, object_pairs_hook=OrderedDict)
|
| 373 |
+
result["schema"].pop("pandas_version")
|
| 374 |
+
|
| 375 |
+
expected = OrderedDict(
|
| 376 |
+
[
|
| 377 |
+
(
|
| 378 |
+
"schema",
|
| 379 |
+
{
|
| 380 |
+
"fields": [
|
| 381 |
+
{"name": "index", "type": "number"},
|
| 382 |
+
{"name": "values", "type": "integer"},
|
| 383 |
+
],
|
| 384 |
+
"primaryKey": ["index"],
|
| 385 |
+
},
|
| 386 |
+
),
|
| 387 |
+
(
|
| 388 |
+
"data",
|
| 389 |
+
[
|
| 390 |
+
OrderedDict([("index", 1.0), ("values", 1)]),
|
| 391 |
+
OrderedDict([("index", 2.0), ("values", 1)]),
|
| 392 |
+
],
|
| 393 |
+
),
|
| 394 |
+
]
|
| 395 |
+
)
|
| 396 |
+
|
| 397 |
+
assert result == expected
|
| 398 |
+
|
| 399 |
+
def test_to_json_period_index(self):
|
| 400 |
+
idx = pd.period_range("2016", freq="Q-JAN", periods=2)
|
| 401 |
+
data = pd.Series(1, idx)
|
| 402 |
+
result = data.to_json(orient="table", date_format="iso")
|
| 403 |
+
result = json.loads(result, object_pairs_hook=OrderedDict)
|
| 404 |
+
result["schema"].pop("pandas_version")
|
| 405 |
+
|
| 406 |
+
fields = [
|
| 407 |
+
{"freq": "QE-JAN", "name": "index", "type": "datetime"},
|
| 408 |
+
{"name": "values", "type": "integer"},
|
| 409 |
+
]
|
| 410 |
+
|
| 411 |
+
schema = {"fields": fields, "primaryKey": ["index"]}
|
| 412 |
+
data = [
|
| 413 |
+
OrderedDict([("index", "2015-11-01T00:00:00.000"), ("values", 1)]),
|
| 414 |
+
OrderedDict([("index", "2016-02-01T00:00:00.000"), ("values", 1)]),
|
| 415 |
+
]
|
| 416 |
+
expected = OrderedDict([("schema", schema), ("data", data)])
|
| 417 |
+
|
| 418 |
+
assert result == expected
|
| 419 |
+
|
| 420 |
+
def test_to_json_categorical_index(self):
|
| 421 |
+
data = pd.Series(1, pd.CategoricalIndex(["a", "b"]))
|
| 422 |
+
result = data.to_json(orient="table", date_format="iso")
|
| 423 |
+
result = json.loads(result, object_pairs_hook=OrderedDict)
|
| 424 |
+
result["schema"].pop("pandas_version")
|
| 425 |
+
|
| 426 |
+
expected = OrderedDict(
|
| 427 |
+
[
|
| 428 |
+
(
|
| 429 |
+
"schema",
|
| 430 |
+
{
|
| 431 |
+
"fields": [
|
| 432 |
+
{
|
| 433 |
+
"name": "index",
|
| 434 |
+
"type": "any",
|
| 435 |
+
"constraints": {"enum": ["a", "b"]},
|
| 436 |
+
"ordered": False,
|
| 437 |
+
},
|
| 438 |
+
{"name": "values", "type": "integer"},
|
| 439 |
+
],
|
| 440 |
+
"primaryKey": ["index"],
|
| 441 |
+
},
|
| 442 |
+
),
|
| 443 |
+
(
|
| 444 |
+
"data",
|
| 445 |
+
[
|
| 446 |
+
OrderedDict([("index", "a"), ("values", 1)]),
|
| 447 |
+
OrderedDict([("index", "b"), ("values", 1)]),
|
| 448 |
+
],
|
| 449 |
+
),
|
| 450 |
+
]
|
| 451 |
+
)
|
| 452 |
+
|
| 453 |
+
assert result == expected
|
| 454 |
+
|
| 455 |
+
def test_date_format_raises(self, df_table):
|
| 456 |
+
msg = (
|
| 457 |
+
"Trying to write with `orient='table'` and `date_format='epoch'`. Table "
|
| 458 |
+
"Schema requires dates to be formatted with `date_format='iso'`"
|
| 459 |
+
)
|
| 460 |
+
with pytest.raises(ValueError, match=msg):
|
| 461 |
+
df_table.to_json(orient="table", date_format="epoch")
|
| 462 |
+
|
| 463 |
+
# others work
|
| 464 |
+
df_table.to_json(orient="table", date_format="iso")
|
| 465 |
+
df_table.to_json(orient="table")
|
| 466 |
+
|
| 467 |
+
def test_convert_pandas_type_to_json_field_int(self, index_or_series):
|
| 468 |
+
kind = index_or_series
|
| 469 |
+
data = [1, 2, 3]
|
| 470 |
+
result = convert_pandas_type_to_json_field(kind(data, name="name"))
|
| 471 |
+
expected = {"name": "name", "type": "integer"}
|
| 472 |
+
assert result == expected
|
| 473 |
+
|
| 474 |
+
def test_convert_pandas_type_to_json_field_float(self, index_or_series):
|
| 475 |
+
kind = index_or_series
|
| 476 |
+
data = [1.0, 2.0, 3.0]
|
| 477 |
+
result = convert_pandas_type_to_json_field(kind(data, name="name"))
|
| 478 |
+
expected = {"name": "name", "type": "number"}
|
| 479 |
+
assert result == expected
|
| 480 |
+
|
| 481 |
+
@pytest.mark.parametrize(
|
| 482 |
+
"dt_args,extra_exp", [({}, {}), ({"utc": True}, {"tz": "UTC"})]
|
| 483 |
+
)
|
| 484 |
+
@pytest.mark.parametrize("wrapper", [None, pd.Series])
|
| 485 |
+
def test_convert_pandas_type_to_json_field_datetime(
|
| 486 |
+
self, dt_args, extra_exp, wrapper
|
| 487 |
+
):
|
| 488 |
+
data = [1.0, 2.0, 3.0]
|
| 489 |
+
data = pd.to_datetime(data, **dt_args)
|
| 490 |
+
if wrapper is pd.Series:
|
| 491 |
+
data = pd.Series(data, name="values")
|
| 492 |
+
result = convert_pandas_type_to_json_field(data)
|
| 493 |
+
expected = {"name": "values", "type": "datetime"}
|
| 494 |
+
expected.update(extra_exp)
|
| 495 |
+
assert result == expected
|
| 496 |
+
|
| 497 |
+
def test_convert_pandas_type_to_json_period_range(self):
|
| 498 |
+
arr = pd.period_range("2016", freq="Y-DEC", periods=4)
|
| 499 |
+
result = convert_pandas_type_to_json_field(arr)
|
| 500 |
+
expected = {"name": "values", "type": "datetime", "freq": "YE-DEC"}
|
| 501 |
+
assert result == expected
|
| 502 |
+
|
| 503 |
+
@pytest.mark.parametrize("kind", [pd.Categorical, pd.CategoricalIndex])
|
| 504 |
+
@pytest.mark.parametrize("ordered", [True, False])
|
| 505 |
+
def test_convert_pandas_type_to_json_field_categorical(self, kind, ordered):
|
| 506 |
+
data = ["a", "b", "c"]
|
| 507 |
+
if kind is pd.Categorical:
|
| 508 |
+
arr = pd.Series(kind(data, ordered=ordered), name="cats")
|
| 509 |
+
elif kind is pd.CategoricalIndex:
|
| 510 |
+
arr = kind(data, ordered=ordered, name="cats")
|
| 511 |
+
|
| 512 |
+
result = convert_pandas_type_to_json_field(arr)
|
| 513 |
+
expected = {
|
| 514 |
+
"name": "cats",
|
| 515 |
+
"type": "any",
|
| 516 |
+
"constraints": {"enum": data},
|
| 517 |
+
"ordered": ordered,
|
| 518 |
+
}
|
| 519 |
+
assert result == expected
|
| 520 |
+
|
| 521 |
+
@pytest.mark.parametrize(
|
| 522 |
+
"inp,exp",
|
| 523 |
+
[
|
| 524 |
+
({"type": "integer"}, "int64"),
|
| 525 |
+
({"type": "number"}, "float64"),
|
| 526 |
+
({"type": "boolean"}, "bool"),
|
| 527 |
+
({"type": "duration"}, "timedelta64"),
|
| 528 |
+
({"type": "datetime"}, "datetime64[ns]"),
|
| 529 |
+
({"type": "datetime", "tz": "US/Hawaii"}, "datetime64[ns, US/Hawaii]"),
|
| 530 |
+
({"type": "any"}, "object"),
|
| 531 |
+
(
|
| 532 |
+
{
|
| 533 |
+
"type": "any",
|
| 534 |
+
"constraints": {"enum": ["a", "b", "c"]},
|
| 535 |
+
"ordered": False,
|
| 536 |
+
},
|
| 537 |
+
CategoricalDtype(categories=["a", "b", "c"], ordered=False),
|
| 538 |
+
),
|
| 539 |
+
(
|
| 540 |
+
{
|
| 541 |
+
"type": "any",
|
| 542 |
+
"constraints": {"enum": ["a", "b", "c"]},
|
| 543 |
+
"ordered": True,
|
| 544 |
+
},
|
| 545 |
+
CategoricalDtype(categories=["a", "b", "c"], ordered=True),
|
| 546 |
+
),
|
| 547 |
+
({"type": "string"}, "object"),
|
| 548 |
+
],
|
| 549 |
+
)
|
| 550 |
+
def test_convert_json_field_to_pandas_type(self, inp, exp):
|
| 551 |
+
field = {"name": "foo"}
|
| 552 |
+
field.update(inp)
|
| 553 |
+
assert convert_json_field_to_pandas_type(field) == exp
|
| 554 |
+
|
| 555 |
+
@pytest.mark.parametrize("inp", ["geopoint", "geojson", "fake_type"])
|
| 556 |
+
def test_convert_json_field_to_pandas_type_raises(self, inp):
|
| 557 |
+
field = {"type": inp}
|
| 558 |
+
with pytest.raises(
|
| 559 |
+
ValueError, match=f"Unsupported or invalid field type: {inp}"
|
| 560 |
+
):
|
| 561 |
+
convert_json_field_to_pandas_type(field)
|
| 562 |
+
|
| 563 |
+
def test_categorical(self):
|
| 564 |
+
s = pd.Series(pd.Categorical(["a", "b", "a"]))
|
| 565 |
+
s.index.name = "idx"
|
| 566 |
+
result = s.to_json(orient="table", date_format="iso")
|
| 567 |
+
result = json.loads(result, object_pairs_hook=OrderedDict)
|
| 568 |
+
result["schema"].pop("pandas_version")
|
| 569 |
+
|
| 570 |
+
fields = [
|
| 571 |
+
{"name": "idx", "type": "integer"},
|
| 572 |
+
{
|
| 573 |
+
"constraints": {"enum": ["a", "b"]},
|
| 574 |
+
"name": "values",
|
| 575 |
+
"ordered": False,
|
| 576 |
+
"type": "any",
|
| 577 |
+
},
|
| 578 |
+
]
|
| 579 |
+
|
| 580 |
+
expected = OrderedDict(
|
| 581 |
+
[
|
| 582 |
+
("schema", {"fields": fields, "primaryKey": ["idx"]}),
|
| 583 |
+
(
|
| 584 |
+
"data",
|
| 585 |
+
[
|
| 586 |
+
OrderedDict([("idx", 0), ("values", "a")]),
|
| 587 |
+
OrderedDict([("idx", 1), ("values", "b")]),
|
| 588 |
+
OrderedDict([("idx", 2), ("values", "a")]),
|
| 589 |
+
],
|
| 590 |
+
),
|
| 591 |
+
]
|
| 592 |
+
)
|
| 593 |
+
|
| 594 |
+
assert result == expected
|
| 595 |
+
|
| 596 |
+
@pytest.mark.parametrize(
|
| 597 |
+
"idx,nm,prop",
|
| 598 |
+
[
|
| 599 |
+
(pd.Index([1]), "index", "name"),
|
| 600 |
+
(pd.Index([1], name="myname"), "myname", "name"),
|
| 601 |
+
(
|
| 602 |
+
pd.MultiIndex.from_product([("a", "b"), ("c", "d")]),
|
| 603 |
+
["level_0", "level_1"],
|
| 604 |
+
"names",
|
| 605 |
+
),
|
| 606 |
+
(
|
| 607 |
+
pd.MultiIndex.from_product(
|
| 608 |
+
[("a", "b"), ("c", "d")], names=["n1", "n2"]
|
| 609 |
+
),
|
| 610 |
+
["n1", "n2"],
|
| 611 |
+
"names",
|
| 612 |
+
),
|
| 613 |
+
(
|
| 614 |
+
pd.MultiIndex.from_product(
|
| 615 |
+
[("a", "b"), ("c", "d")], names=["n1", None]
|
| 616 |
+
),
|
| 617 |
+
["n1", "level_1"],
|
| 618 |
+
"names",
|
| 619 |
+
),
|
| 620 |
+
],
|
| 621 |
+
)
|
| 622 |
+
def test_set_names_unset(self, idx, nm, prop):
|
| 623 |
+
data = pd.Series(1, idx)
|
| 624 |
+
result = set_default_names(data)
|
| 625 |
+
assert getattr(result.index, prop) == nm
|
| 626 |
+
|
| 627 |
+
@pytest.mark.parametrize(
|
| 628 |
+
"idx",
|
| 629 |
+
[
|
| 630 |
+
pd.Index([], name="index"),
|
| 631 |
+
pd.MultiIndex.from_arrays([["foo"], ["bar"]], names=("level_0", "level_1")),
|
| 632 |
+
pd.MultiIndex.from_arrays([["foo"], ["bar"]], names=("foo", "level_1")),
|
| 633 |
+
],
|
| 634 |
+
)
|
| 635 |
+
def test_warns_non_roundtrippable_names(self, idx):
|
| 636 |
+
# GH 19130
|
| 637 |
+
df = DataFrame(index=idx)
|
| 638 |
+
df.index.name = "index"
|
| 639 |
+
with tm.assert_produces_warning():
|
| 640 |
+
set_default_names(df)
|
| 641 |
+
|
| 642 |
+
def test_timestamp_in_columns(self):
|
| 643 |
+
df = DataFrame(
|
| 644 |
+
[[1, 2]], columns=[pd.Timestamp("2016"), pd.Timedelta(10, unit="s")]
|
| 645 |
+
)
|
| 646 |
+
result = df.to_json(orient="table")
|
| 647 |
+
js = json.loads(result)
|
| 648 |
+
assert js["schema"]["fields"][1]["name"] == "2016-01-01T00:00:00.000"
|
| 649 |
+
assert js["schema"]["fields"][2]["name"] == "P0DT0H0M10S"
|
| 650 |
+
|
| 651 |
+
@pytest.mark.parametrize(
|
| 652 |
+
"case",
|
| 653 |
+
[
|
| 654 |
+
pd.Series([1], index=pd.Index([1], name="a"), name="a"),
|
| 655 |
+
DataFrame({"A": [1]}, index=pd.Index([1], name="A")),
|
| 656 |
+
DataFrame(
|
| 657 |
+
{"A": [1]},
|
| 658 |
+
index=pd.MultiIndex.from_arrays([["a"], [1]], names=["A", "a"]),
|
| 659 |
+
),
|
| 660 |
+
],
|
| 661 |
+
)
|
| 662 |
+
def test_overlapping_names(self, case):
|
| 663 |
+
with pytest.raises(ValueError, match="Overlapping"):
|
| 664 |
+
case.to_json(orient="table")
|
| 665 |
+
|
| 666 |
+
def test_mi_falsey_name(self):
|
| 667 |
+
# GH 16203
|
| 668 |
+
df = DataFrame(
|
| 669 |
+
np.random.default_rng(2).standard_normal((4, 4)),
|
| 670 |
+
index=pd.MultiIndex.from_product([("A", "B"), ("a", "b")]),
|
| 671 |
+
)
|
| 672 |
+
result = [x["name"] for x in build_table_schema(df)["fields"]]
|
| 673 |
+
assert result == ["level_0", "level_1", 0, 1, 2, 3]
|
| 674 |
+
|
| 675 |
+
|
| 676 |
+
class TestTableOrientReader:
|
| 677 |
+
@pytest.mark.parametrize(
|
| 678 |
+
"index_nm",
|
| 679 |
+
[None, "idx", pytest.param("index", marks=pytest.mark.xfail), "level_0"],
|
| 680 |
+
)
|
| 681 |
+
@pytest.mark.parametrize(
|
| 682 |
+
"vals",
|
| 683 |
+
[
|
| 684 |
+
{"ints": [1, 2, 3, 4]},
|
| 685 |
+
{"objects": ["a", "b", "c", "d"]},
|
| 686 |
+
{"objects": ["1", "2", "3", "4"]},
|
| 687 |
+
{"date_ranges": pd.date_range("2016-01-01", freq="d", periods=4)},
|
| 688 |
+
{"categoricals": pd.Series(pd.Categorical(["a", "b", "c", "c"]))},
|
| 689 |
+
{
|
| 690 |
+
"ordered_cats": pd.Series(
|
| 691 |
+
pd.Categorical(["a", "b", "c", "c"], ordered=True)
|
| 692 |
+
)
|
| 693 |
+
},
|
| 694 |
+
{"floats": [1.0, 2.0, 3.0, 4.0]},
|
| 695 |
+
{"floats": [1.1, 2.2, 3.3, 4.4]},
|
| 696 |
+
{"bools": [True, False, False, True]},
|
| 697 |
+
{
|
| 698 |
+
"timezones": pd.date_range(
|
| 699 |
+
"2016-01-01", freq="d", periods=4, tz="US/Central"
|
| 700 |
+
) # added in # GH 35973
|
| 701 |
+
},
|
| 702 |
+
],
|
| 703 |
+
)
|
| 704 |
+
def test_read_json_table_orient(self, index_nm, vals, recwarn):
|
| 705 |
+
df = DataFrame(vals, index=pd.Index(range(4), name=index_nm))
|
| 706 |
+
out = df.to_json(orient="table")
|
| 707 |
+
result = pd.read_json(out, orient="table")
|
| 708 |
+
tm.assert_frame_equal(df, result)
|
| 709 |
+
|
| 710 |
+
@pytest.mark.parametrize("index_nm", [None, "idx", "index"])
|
| 711 |
+
@pytest.mark.parametrize(
|
| 712 |
+
"vals",
|
| 713 |
+
[{"timedeltas": pd.timedelta_range("1h", periods=4, freq="min")}],
|
| 714 |
+
)
|
| 715 |
+
def test_read_json_table_orient_raises(self, index_nm, vals, recwarn):
|
| 716 |
+
df = DataFrame(vals, index=pd.Index(range(4), name=index_nm))
|
| 717 |
+
out = df.to_json(orient="table")
|
| 718 |
+
with pytest.raises(NotImplementedError, match="can not yet read "):
|
| 719 |
+
pd.read_json(out, orient="table")
|
| 720 |
+
|
| 721 |
+
@pytest.mark.parametrize(
|
| 722 |
+
"index_nm",
|
| 723 |
+
[None, "idx", pytest.param("index", marks=pytest.mark.xfail), "level_0"],
|
| 724 |
+
)
|
| 725 |
+
@pytest.mark.parametrize(
|
| 726 |
+
"vals",
|
| 727 |
+
[
|
| 728 |
+
{"ints": [1, 2, 3, 4]},
|
| 729 |
+
{"objects": ["a", "b", "c", "d"]},
|
| 730 |
+
{"objects": ["1", "2", "3", "4"]},
|
| 731 |
+
{"date_ranges": pd.date_range("2016-01-01", freq="d", periods=4)},
|
| 732 |
+
{"categoricals": pd.Series(pd.Categorical(["a", "b", "c", "c"]))},
|
| 733 |
+
{
|
| 734 |
+
"ordered_cats": pd.Series(
|
| 735 |
+
pd.Categorical(["a", "b", "c", "c"], ordered=True)
|
| 736 |
+
)
|
| 737 |
+
},
|
| 738 |
+
{"floats": [1.0, 2.0, 3.0, 4.0]},
|
| 739 |
+
{"floats": [1.1, 2.2, 3.3, 4.4]},
|
| 740 |
+
{"bools": [True, False, False, True]},
|
| 741 |
+
{
|
| 742 |
+
"timezones": pd.date_range(
|
| 743 |
+
"2016-01-01", freq="d", periods=4, tz="US/Central"
|
| 744 |
+
) # added in # GH 35973
|
| 745 |
+
},
|
| 746 |
+
],
|
| 747 |
+
)
|
| 748 |
+
def test_read_json_table_period_orient(self, index_nm, vals, recwarn):
|
| 749 |
+
df = DataFrame(
|
| 750 |
+
vals,
|
| 751 |
+
index=pd.Index(
|
| 752 |
+
(pd.Period(f"2022Q{q}") for q in range(1, 5)), name=index_nm
|
| 753 |
+
),
|
| 754 |
+
)
|
| 755 |
+
out = df.to_json(orient="table")
|
| 756 |
+
result = pd.read_json(out, orient="table")
|
| 757 |
+
tm.assert_frame_equal(df, result)
|
| 758 |
+
|
| 759 |
+
@pytest.mark.parametrize(
|
| 760 |
+
"idx",
|
| 761 |
+
[
|
| 762 |
+
pd.Index(range(4)),
|
| 763 |
+
pd.date_range(
|
| 764 |
+
"2020-08-30",
|
| 765 |
+
freq="d",
|
| 766 |
+
periods=4,
|
| 767 |
+
)._with_freq(None),
|
| 768 |
+
pd.date_range(
|
| 769 |
+
"2020-08-30", freq="d", periods=4, tz="US/Central"
|
| 770 |
+
)._with_freq(None),
|
| 771 |
+
pd.MultiIndex.from_product(
|
| 772 |
+
[
|
| 773 |
+
pd.date_range("2020-08-30", freq="d", periods=2, tz="US/Central"),
|
| 774 |
+
["x", "y"],
|
| 775 |
+
],
|
| 776 |
+
),
|
| 777 |
+
],
|
| 778 |
+
)
|
| 779 |
+
@pytest.mark.parametrize(
|
| 780 |
+
"vals",
|
| 781 |
+
[
|
| 782 |
+
{"floats": [1.1, 2.2, 3.3, 4.4]},
|
| 783 |
+
{"dates": pd.date_range("2020-08-30", freq="d", periods=4)},
|
| 784 |
+
{
|
| 785 |
+
"timezones": pd.date_range(
|
| 786 |
+
"2020-08-30", freq="d", periods=4, tz="Europe/London"
|
| 787 |
+
)
|
| 788 |
+
},
|
| 789 |
+
],
|
| 790 |
+
)
|
| 791 |
+
def test_read_json_table_timezones_orient(self, idx, vals, recwarn):
|
| 792 |
+
# GH 35973
|
| 793 |
+
df = DataFrame(vals, index=idx)
|
| 794 |
+
out = df.to_json(orient="table")
|
| 795 |
+
result = pd.read_json(out, orient="table")
|
| 796 |
+
tm.assert_frame_equal(df, result)
|
| 797 |
+
|
| 798 |
+
def test_comprehensive(self):
|
| 799 |
+
df = DataFrame(
|
| 800 |
+
{
|
| 801 |
+
"A": [1, 2, 3, 4],
|
| 802 |
+
"B": ["a", "b", "c", "c"],
|
| 803 |
+
"C": pd.date_range("2016-01-01", freq="d", periods=4),
|
| 804 |
+
# 'D': pd.timedelta_range('1h', periods=4, freq='min'),
|
| 805 |
+
"E": pd.Series(pd.Categorical(["a", "b", "c", "c"])),
|
| 806 |
+
"F": pd.Series(pd.Categorical(["a", "b", "c", "c"], ordered=True)),
|
| 807 |
+
"G": [1.1, 2.2, 3.3, 4.4],
|
| 808 |
+
"H": pd.date_range("2016-01-01", freq="d", periods=4, tz="US/Central"),
|
| 809 |
+
"I": [True, False, False, True],
|
| 810 |
+
},
|
| 811 |
+
index=pd.Index(range(4), name="idx"),
|
| 812 |
+
)
|
| 813 |
+
|
| 814 |
+
out = StringIO(df.to_json(orient="table"))
|
| 815 |
+
result = pd.read_json(out, orient="table")
|
| 816 |
+
tm.assert_frame_equal(df, result)
|
| 817 |
+
|
| 818 |
+
@pytest.mark.parametrize(
|
| 819 |
+
"index_names",
|
| 820 |
+
[[None, None], ["foo", "bar"], ["foo", None], [None, "foo"], ["index", "foo"]],
|
| 821 |
+
)
|
| 822 |
+
def test_multiindex(self, index_names):
|
| 823 |
+
# GH 18912
|
| 824 |
+
df = DataFrame(
|
| 825 |
+
[["Arr", "alpha", [1, 2, 3, 4]], ["Bee", "Beta", [10, 20, 30, 40]]],
|
| 826 |
+
index=[["A", "B"], ["Null", "Eins"]],
|
| 827 |
+
columns=["Aussprache", "Griechisch", "Args"],
|
| 828 |
+
)
|
| 829 |
+
df.index.names = index_names
|
| 830 |
+
out = StringIO(df.to_json(orient="table"))
|
| 831 |
+
result = pd.read_json(out, orient="table")
|
| 832 |
+
tm.assert_frame_equal(df, result)
|
| 833 |
+
|
| 834 |
+
def test_empty_frame_roundtrip(self):
|
| 835 |
+
# GH 21287
|
| 836 |
+
df = DataFrame(columns=["a", "b", "c"])
|
| 837 |
+
expected = df.copy()
|
| 838 |
+
out = StringIO(df.to_json(orient="table"))
|
| 839 |
+
result = pd.read_json(out, orient="table")
|
| 840 |
+
tm.assert_frame_equal(expected, result)
|
| 841 |
+
|
| 842 |
+
def test_read_json_orient_table_old_schema_version(self):
|
| 843 |
+
df_json = """
|
| 844 |
+
{
|
| 845 |
+
"schema":{
|
| 846 |
+
"fields":[
|
| 847 |
+
{"name":"index","type":"integer"},
|
| 848 |
+
{"name":"a","type":"string"}
|
| 849 |
+
],
|
| 850 |
+
"primaryKey":["index"],
|
| 851 |
+
"pandas_version":"0.20.0"
|
| 852 |
+
},
|
| 853 |
+
"data":[
|
| 854 |
+
{"index":0,"a":1},
|
| 855 |
+
{"index":1,"a":2.0},
|
| 856 |
+
{"index":2,"a":"s"}
|
| 857 |
+
]
|
| 858 |
+
}
|
| 859 |
+
"""
|
| 860 |
+
expected = DataFrame({"a": [1, 2.0, "s"]})
|
| 861 |
+
result = pd.read_json(StringIO(df_json), orient="table")
|
| 862 |
+
tm.assert_frame_equal(expected, result)
|
| 863 |
+
|
| 864 |
+
@pytest.mark.parametrize("freq", ["M", "2M", "Q", "2Q", "Y", "2Y"])
|
| 865 |
+
def test_read_json_table_orient_period_depr_freq(self, freq, recwarn):
|
| 866 |
+
# GH#9586
|
| 867 |
+
df = DataFrame(
|
| 868 |
+
{"ints": [1, 2]},
|
| 869 |
+
index=pd.PeriodIndex(["2020-01", "2021-06"], freq=freq),
|
| 870 |
+
)
|
| 871 |
+
out = df.to_json(orient="table")
|
| 872 |
+
result = pd.read_json(out, orient="table")
|
| 873 |
+
tm.assert_frame_equal(df, result)
|
vllm/lib/python3.10/site-packages/pandas/tests/io/json/test_json_table_schema_ext_dtype.py
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for ExtensionDtype Table Schema integration."""
|
| 2 |
+
|
| 3 |
+
from collections import OrderedDict
|
| 4 |
+
import datetime as dt
|
| 5 |
+
import decimal
|
| 6 |
+
from io import StringIO
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
+
import pytest
|
| 10 |
+
|
| 11 |
+
from pandas import (
|
| 12 |
+
NA,
|
| 13 |
+
DataFrame,
|
| 14 |
+
Index,
|
| 15 |
+
array,
|
| 16 |
+
read_json,
|
| 17 |
+
)
|
| 18 |
+
import pandas._testing as tm
|
| 19 |
+
from pandas.core.arrays.integer import Int64Dtype
|
| 20 |
+
from pandas.core.arrays.string_ import StringDtype
|
| 21 |
+
from pandas.core.series import Series
|
| 22 |
+
from pandas.tests.extension.date import (
|
| 23 |
+
DateArray,
|
| 24 |
+
DateDtype,
|
| 25 |
+
)
|
| 26 |
+
from pandas.tests.extension.decimal.array import (
|
| 27 |
+
DecimalArray,
|
| 28 |
+
DecimalDtype,
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
from pandas.io.json._table_schema import (
|
| 32 |
+
as_json_table_type,
|
| 33 |
+
build_table_schema,
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class TestBuildSchema:
|
| 38 |
+
def test_build_table_schema(self):
|
| 39 |
+
df = DataFrame(
|
| 40 |
+
{
|
| 41 |
+
"A": DateArray([dt.date(2021, 10, 10)]),
|
| 42 |
+
"B": DecimalArray([decimal.Decimal(10)]),
|
| 43 |
+
"C": array(["pandas"], dtype="string"),
|
| 44 |
+
"D": array([10], dtype="Int64"),
|
| 45 |
+
}
|
| 46 |
+
)
|
| 47 |
+
result = build_table_schema(df, version=False)
|
| 48 |
+
expected = {
|
| 49 |
+
"fields": [
|
| 50 |
+
{"name": "index", "type": "integer"},
|
| 51 |
+
{"name": "A", "type": "any", "extDtype": "DateDtype"},
|
| 52 |
+
{"name": "B", "type": "number", "extDtype": "decimal"},
|
| 53 |
+
{"name": "C", "type": "any", "extDtype": "string"},
|
| 54 |
+
{"name": "D", "type": "integer", "extDtype": "Int64"},
|
| 55 |
+
],
|
| 56 |
+
"primaryKey": ["index"],
|
| 57 |
+
}
|
| 58 |
+
assert result == expected
|
| 59 |
+
result = build_table_schema(df)
|
| 60 |
+
assert "pandas_version" in result
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class TestTableSchemaType:
|
| 64 |
+
@pytest.mark.parametrize(
|
| 65 |
+
"date_data",
|
| 66 |
+
[
|
| 67 |
+
DateArray([dt.date(2021, 10, 10)]),
|
| 68 |
+
DateArray(dt.date(2021, 10, 10)),
|
| 69 |
+
Series(DateArray(dt.date(2021, 10, 10))),
|
| 70 |
+
],
|
| 71 |
+
)
|
| 72 |
+
def test_as_json_table_type_ext_date_array_dtype(self, date_data):
|
| 73 |
+
assert as_json_table_type(date_data.dtype) == "any"
|
| 74 |
+
|
| 75 |
+
def test_as_json_table_type_ext_date_dtype(self):
|
| 76 |
+
assert as_json_table_type(DateDtype()) == "any"
|
| 77 |
+
|
| 78 |
+
@pytest.mark.parametrize(
|
| 79 |
+
"decimal_data",
|
| 80 |
+
[
|
| 81 |
+
DecimalArray([decimal.Decimal(10)]),
|
| 82 |
+
Series(DecimalArray([decimal.Decimal(10)])),
|
| 83 |
+
],
|
| 84 |
+
)
|
| 85 |
+
def test_as_json_table_type_ext_decimal_array_dtype(self, decimal_data):
|
| 86 |
+
assert as_json_table_type(decimal_data.dtype) == "number"
|
| 87 |
+
|
| 88 |
+
def test_as_json_table_type_ext_decimal_dtype(self):
|
| 89 |
+
assert as_json_table_type(DecimalDtype()) == "number"
|
| 90 |
+
|
| 91 |
+
@pytest.mark.parametrize(
|
| 92 |
+
"string_data",
|
| 93 |
+
[
|
| 94 |
+
array(["pandas"], dtype="string"),
|
| 95 |
+
Series(array(["pandas"], dtype="string")),
|
| 96 |
+
],
|
| 97 |
+
)
|
| 98 |
+
def test_as_json_table_type_ext_string_array_dtype(self, string_data):
|
| 99 |
+
assert as_json_table_type(string_data.dtype) == "any"
|
| 100 |
+
|
| 101 |
+
def test_as_json_table_type_ext_string_dtype(self):
|
| 102 |
+
assert as_json_table_type(StringDtype()) == "any"
|
| 103 |
+
|
| 104 |
+
@pytest.mark.parametrize(
|
| 105 |
+
"integer_data",
|
| 106 |
+
[
|
| 107 |
+
array([10], dtype="Int64"),
|
| 108 |
+
Series(array([10], dtype="Int64")),
|
| 109 |
+
],
|
| 110 |
+
)
|
| 111 |
+
def test_as_json_table_type_ext_integer_array_dtype(self, integer_data):
|
| 112 |
+
assert as_json_table_type(integer_data.dtype) == "integer"
|
| 113 |
+
|
| 114 |
+
def test_as_json_table_type_ext_integer_dtype(self):
|
| 115 |
+
assert as_json_table_type(Int64Dtype()) == "integer"
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
class TestTableOrient:
|
| 119 |
+
@pytest.fixture
|
| 120 |
+
def da(self):
|
| 121 |
+
return DateArray([dt.date(2021, 10, 10)])
|
| 122 |
+
|
| 123 |
+
@pytest.fixture
|
| 124 |
+
def dc(self):
|
| 125 |
+
return DecimalArray([decimal.Decimal(10)])
|
| 126 |
+
|
| 127 |
+
@pytest.fixture
|
| 128 |
+
def sa(self):
|
| 129 |
+
return array(["pandas"], dtype="string")
|
| 130 |
+
|
| 131 |
+
@pytest.fixture
|
| 132 |
+
def ia(self):
|
| 133 |
+
return array([10], dtype="Int64")
|
| 134 |
+
|
| 135 |
+
@pytest.fixture
|
| 136 |
+
def df(self, da, dc, sa, ia):
|
| 137 |
+
return DataFrame(
|
| 138 |
+
{
|
| 139 |
+
"A": da,
|
| 140 |
+
"B": dc,
|
| 141 |
+
"C": sa,
|
| 142 |
+
"D": ia,
|
| 143 |
+
}
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
def test_build_date_series(self, da):
|
| 147 |
+
s = Series(da, name="a")
|
| 148 |
+
s.index.name = "id"
|
| 149 |
+
result = s.to_json(orient="table", date_format="iso")
|
| 150 |
+
result = json.loads(result, object_pairs_hook=OrderedDict)
|
| 151 |
+
|
| 152 |
+
assert "pandas_version" in result["schema"]
|
| 153 |
+
result["schema"].pop("pandas_version")
|
| 154 |
+
|
| 155 |
+
fields = [
|
| 156 |
+
{"name": "id", "type": "integer"},
|
| 157 |
+
{"name": "a", "type": "any", "extDtype": "DateDtype"},
|
| 158 |
+
]
|
| 159 |
+
|
| 160 |
+
schema = {"fields": fields, "primaryKey": ["id"]}
|
| 161 |
+
|
| 162 |
+
expected = OrderedDict(
|
| 163 |
+
[
|
| 164 |
+
("schema", schema),
|
| 165 |
+
("data", [OrderedDict([("id", 0), ("a", "2021-10-10T00:00:00.000")])]),
|
| 166 |
+
]
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
assert result == expected
|
| 170 |
+
|
| 171 |
+
def test_build_decimal_series(self, dc):
|
| 172 |
+
s = Series(dc, name="a")
|
| 173 |
+
s.index.name = "id"
|
| 174 |
+
result = s.to_json(orient="table", date_format="iso")
|
| 175 |
+
result = json.loads(result, object_pairs_hook=OrderedDict)
|
| 176 |
+
|
| 177 |
+
assert "pandas_version" in result["schema"]
|
| 178 |
+
result["schema"].pop("pandas_version")
|
| 179 |
+
|
| 180 |
+
fields = [
|
| 181 |
+
{"name": "id", "type": "integer"},
|
| 182 |
+
{"name": "a", "type": "number", "extDtype": "decimal"},
|
| 183 |
+
]
|
| 184 |
+
|
| 185 |
+
schema = {"fields": fields, "primaryKey": ["id"]}
|
| 186 |
+
|
| 187 |
+
expected = OrderedDict(
|
| 188 |
+
[
|
| 189 |
+
("schema", schema),
|
| 190 |
+
("data", [OrderedDict([("id", 0), ("a", 10.0)])]),
|
| 191 |
+
]
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
assert result == expected
|
| 195 |
+
|
| 196 |
+
def test_build_string_series(self, sa):
|
| 197 |
+
s = Series(sa, name="a")
|
| 198 |
+
s.index.name = "id"
|
| 199 |
+
result = s.to_json(orient="table", date_format="iso")
|
| 200 |
+
result = json.loads(result, object_pairs_hook=OrderedDict)
|
| 201 |
+
|
| 202 |
+
assert "pandas_version" in result["schema"]
|
| 203 |
+
result["schema"].pop("pandas_version")
|
| 204 |
+
|
| 205 |
+
fields = [
|
| 206 |
+
{"name": "id", "type": "integer"},
|
| 207 |
+
{"name": "a", "type": "any", "extDtype": "string"},
|
| 208 |
+
]
|
| 209 |
+
|
| 210 |
+
schema = {"fields": fields, "primaryKey": ["id"]}
|
| 211 |
+
|
| 212 |
+
expected = OrderedDict(
|
| 213 |
+
[
|
| 214 |
+
("schema", schema),
|
| 215 |
+
("data", [OrderedDict([("id", 0), ("a", "pandas")])]),
|
| 216 |
+
]
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
assert result == expected
|
| 220 |
+
|
| 221 |
+
def test_build_int64_series(self, ia):
|
| 222 |
+
s = Series(ia, name="a")
|
| 223 |
+
s.index.name = "id"
|
| 224 |
+
result = s.to_json(orient="table", date_format="iso")
|
| 225 |
+
result = json.loads(result, object_pairs_hook=OrderedDict)
|
| 226 |
+
|
| 227 |
+
assert "pandas_version" in result["schema"]
|
| 228 |
+
result["schema"].pop("pandas_version")
|
| 229 |
+
|
| 230 |
+
fields = [
|
| 231 |
+
{"name": "id", "type": "integer"},
|
| 232 |
+
{"name": "a", "type": "integer", "extDtype": "Int64"},
|
| 233 |
+
]
|
| 234 |
+
|
| 235 |
+
schema = {"fields": fields, "primaryKey": ["id"]}
|
| 236 |
+
|
| 237 |
+
expected = OrderedDict(
|
| 238 |
+
[
|
| 239 |
+
("schema", schema),
|
| 240 |
+
("data", [OrderedDict([("id", 0), ("a", 10)])]),
|
| 241 |
+
]
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
assert result == expected
|
| 245 |
+
|
| 246 |
+
def test_to_json(self, df):
|
| 247 |
+
df = df.copy()
|
| 248 |
+
df.index.name = "idx"
|
| 249 |
+
result = df.to_json(orient="table", date_format="iso")
|
| 250 |
+
result = json.loads(result, object_pairs_hook=OrderedDict)
|
| 251 |
+
|
| 252 |
+
assert "pandas_version" in result["schema"]
|
| 253 |
+
result["schema"].pop("pandas_version")
|
| 254 |
+
|
| 255 |
+
fields = [
|
| 256 |
+
OrderedDict({"name": "idx", "type": "integer"}),
|
| 257 |
+
OrderedDict({"name": "A", "type": "any", "extDtype": "DateDtype"}),
|
| 258 |
+
OrderedDict({"name": "B", "type": "number", "extDtype": "decimal"}),
|
| 259 |
+
OrderedDict({"name": "C", "type": "any", "extDtype": "string"}),
|
| 260 |
+
OrderedDict({"name": "D", "type": "integer", "extDtype": "Int64"}),
|
| 261 |
+
]
|
| 262 |
+
|
| 263 |
+
schema = OrderedDict({"fields": fields, "primaryKey": ["idx"]})
|
| 264 |
+
data = [
|
| 265 |
+
OrderedDict(
|
| 266 |
+
[
|
| 267 |
+
("idx", 0),
|
| 268 |
+
("A", "2021-10-10T00:00:00.000"),
|
| 269 |
+
("B", 10.0),
|
| 270 |
+
("C", "pandas"),
|
| 271 |
+
("D", 10),
|
| 272 |
+
]
|
| 273 |
+
)
|
| 274 |
+
]
|
| 275 |
+
expected = OrderedDict([("schema", schema), ("data", data)])
|
| 276 |
+
|
| 277 |
+
assert result == expected
|
| 278 |
+
|
| 279 |
+
def test_json_ext_dtype_reading_roundtrip(self):
|
| 280 |
+
# GH#40255
|
| 281 |
+
df = DataFrame(
|
| 282 |
+
{
|
| 283 |
+
"a": Series([2, NA], dtype="Int64"),
|
| 284 |
+
"b": Series([1.5, NA], dtype="Float64"),
|
| 285 |
+
"c": Series([True, NA], dtype="boolean"),
|
| 286 |
+
},
|
| 287 |
+
index=Index([1, NA], dtype="Int64"),
|
| 288 |
+
)
|
| 289 |
+
expected = df.copy()
|
| 290 |
+
data_json = df.to_json(orient="table", indent=4)
|
| 291 |
+
result = read_json(StringIO(data_json), orient="table")
|
| 292 |
+
tm.assert_frame_equal(result, expected)
|
| 293 |
+
|
| 294 |
+
def test_json_ext_dtype_reading(self):
|
| 295 |
+
# GH#40255
|
| 296 |
+
data_json = """{
|
| 297 |
+
"schema":{
|
| 298 |
+
"fields":[
|
| 299 |
+
{
|
| 300 |
+
"name":"a",
|
| 301 |
+
"type":"integer",
|
| 302 |
+
"extDtype":"Int64"
|
| 303 |
+
}
|
| 304 |
+
],
|
| 305 |
+
},
|
| 306 |
+
"data":[
|
| 307 |
+
{
|
| 308 |
+
"a":2
|
| 309 |
+
},
|
| 310 |
+
{
|
| 311 |
+
"a":null
|
| 312 |
+
}
|
| 313 |
+
]
|
| 314 |
+
}"""
|
| 315 |
+
result = read_json(StringIO(data_json), orient="table")
|
| 316 |
+
expected = DataFrame({"a": Series([2, NA], dtype="Int64")})
|
| 317 |
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
from pandas import (
|
| 7 |
+
DataFrame,
|
| 8 |
+
Index,
|
| 9 |
+
Series,
|
| 10 |
+
json_normalize,
|
| 11 |
+
)
|
| 12 |
+
import pandas._testing as tm
|
| 13 |
+
|
| 14 |
+
from pandas.io.json._normalize import nested_to_record
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@pytest.fixture
|
| 18 |
+
def deep_nested():
|
| 19 |
+
# deeply nested data
|
| 20 |
+
return [
|
| 21 |
+
{
|
| 22 |
+
"country": "USA",
|
| 23 |
+
"states": [
|
| 24 |
+
{
|
| 25 |
+
"name": "California",
|
| 26 |
+
"cities": [
|
| 27 |
+
{"name": "San Francisco", "pop": 12345},
|
| 28 |
+
{"name": "Los Angeles", "pop": 12346},
|
| 29 |
+
],
|
| 30 |
+
},
|
| 31 |
+
{
|
| 32 |
+
"name": "Ohio",
|
| 33 |
+
"cities": [
|
| 34 |
+
{"name": "Columbus", "pop": 1234},
|
| 35 |
+
{"name": "Cleveland", "pop": 1236},
|
| 36 |
+
],
|
| 37 |
+
},
|
| 38 |
+
],
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"country": "Germany",
|
| 42 |
+
"states": [
|
| 43 |
+
{"name": "Bayern", "cities": [{"name": "Munich", "pop": 12347}]},
|
| 44 |
+
{
|
| 45 |
+
"name": "Nordrhein-Westfalen",
|
| 46 |
+
"cities": [
|
| 47 |
+
{"name": "Duesseldorf", "pop": 1238},
|
| 48 |
+
{"name": "Koeln", "pop": 1239},
|
| 49 |
+
],
|
| 50 |
+
},
|
| 51 |
+
],
|
| 52 |
+
},
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
@pytest.fixture
|
| 57 |
+
def state_data():
|
| 58 |
+
return [
|
| 59 |
+
{
|
| 60 |
+
"counties": [
|
| 61 |
+
{"name": "Dade", "population": 12345},
|
| 62 |
+
{"name": "Broward", "population": 40000},
|
| 63 |
+
{"name": "Palm Beach", "population": 60000},
|
| 64 |
+
],
|
| 65 |
+
"info": {"governor": "Rick Scott"},
|
| 66 |
+
"shortname": "FL",
|
| 67 |
+
"state": "Florida",
|
| 68 |
+
},
|
| 69 |
+
{
|
| 70 |
+
"counties": [
|
| 71 |
+
{"name": "Summit", "population": 1234},
|
| 72 |
+
{"name": "Cuyahoga", "population": 1337},
|
| 73 |
+
],
|
| 74 |
+
"info": {"governor": "John Kasich"},
|
| 75 |
+
"shortname": "OH",
|
| 76 |
+
"state": "Ohio",
|
| 77 |
+
},
|
| 78 |
+
]
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
@pytest.fixture
|
| 82 |
+
def author_missing_data():
|
| 83 |
+
return [
|
| 84 |
+
{"info": None},
|
| 85 |
+
{
|
| 86 |
+
"info": {"created_at": "11/08/1993", "last_updated": "26/05/2012"},
|
| 87 |
+
"author_name": {"first": "Jane", "last_name": "Doe"},
|
| 88 |
+
},
|
| 89 |
+
]
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
@pytest.fixture
|
| 93 |
+
def missing_metadata():
|
| 94 |
+
return [
|
| 95 |
+
{
|
| 96 |
+
"name": "Alice",
|
| 97 |
+
"addresses": [
|
| 98 |
+
{
|
| 99 |
+
"number": 9562,
|
| 100 |
+
"street": "Morris St.",
|
| 101 |
+
"city": "Massillon",
|
| 102 |
+
"state": "OH",
|
| 103 |
+
"zip": 44646,
|
| 104 |
+
}
|
| 105 |
+
],
|
| 106 |
+
"previous_residences": {"cities": [{"city_name": "Foo York City"}]},
|
| 107 |
+
},
|
| 108 |
+
{
|
| 109 |
+
"addresses": [
|
| 110 |
+
{
|
| 111 |
+
"number": 8449,
|
| 112 |
+
"street": "Spring St.",
|
| 113 |
+
"city": "Elizabethton",
|
| 114 |
+
"state": "TN",
|
| 115 |
+
"zip": 37643,
|
| 116 |
+
}
|
| 117 |
+
],
|
| 118 |
+
"previous_residences": {"cities": [{"city_name": "Barmingham"}]},
|
| 119 |
+
},
|
| 120 |
+
]
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
@pytest.fixture
|
| 124 |
+
def max_level_test_input_data():
|
| 125 |
+
"""
|
| 126 |
+
input data to test json_normalize with max_level param
|
| 127 |
+
"""
|
| 128 |
+
return [
|
| 129 |
+
{
|
| 130 |
+
"CreatedBy": {"Name": "User001"},
|
| 131 |
+
"Lookup": {
|
| 132 |
+
"TextField": "Some text",
|
| 133 |
+
"UserField": {"Id": "ID001", "Name": "Name001"},
|
| 134 |
+
},
|
| 135 |
+
"Image": {"a": "b"},
|
| 136 |
+
}
|
| 137 |
+
]
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
class TestJSONNormalize:
|
| 141 |
+
def test_simple_records(self):
|
| 142 |
+
recs = [
|
| 143 |
+
{"a": 1, "b": 2, "c": 3},
|
| 144 |
+
{"a": 4, "b": 5, "c": 6},
|
| 145 |
+
{"a": 7, "b": 8, "c": 9},
|
| 146 |
+
{"a": 10, "b": 11, "c": 12},
|
| 147 |
+
]
|
| 148 |
+
|
| 149 |
+
result = json_normalize(recs)
|
| 150 |
+
expected = DataFrame(recs)
|
| 151 |
+
|
| 152 |
+
tm.assert_frame_equal(result, expected)
|
| 153 |
+
|
| 154 |
+
def test_simple_normalize(self, state_data):
|
| 155 |
+
result = json_normalize(state_data[0], "counties")
|
| 156 |
+
expected = DataFrame(state_data[0]["counties"])
|
| 157 |
+
tm.assert_frame_equal(result, expected)
|
| 158 |
+
|
| 159 |
+
result = json_normalize(state_data, "counties")
|
| 160 |
+
|
| 161 |
+
expected = []
|
| 162 |
+
for rec in state_data:
|
| 163 |
+
expected.extend(rec["counties"])
|
| 164 |
+
expected = DataFrame(expected)
|
| 165 |
+
|
| 166 |
+
tm.assert_frame_equal(result, expected)
|
| 167 |
+
|
| 168 |
+
result = json_normalize(state_data, "counties", meta="state")
|
| 169 |
+
expected["state"] = np.array(["Florida", "Ohio"]).repeat([3, 2])
|
| 170 |
+
|
| 171 |
+
tm.assert_frame_equal(result, expected)
|
| 172 |
+
|
| 173 |
+
def test_fields_list_type_normalize(self):
|
| 174 |
+
parse_metadata_fields_list_type = [
|
| 175 |
+
{"values": [1, 2, 3], "metadata": {"listdata": [1, 2]}}
|
| 176 |
+
]
|
| 177 |
+
result = json_normalize(
|
| 178 |
+
parse_metadata_fields_list_type,
|
| 179 |
+
record_path=["values"],
|
| 180 |
+
meta=[["metadata", "listdata"]],
|
| 181 |
+
)
|
| 182 |
+
expected = DataFrame(
|
| 183 |
+
{0: [1, 2, 3], "metadata.listdata": [[1, 2], [1, 2], [1, 2]]}
|
| 184 |
+
)
|
| 185 |
+
tm.assert_frame_equal(result, expected)
|
| 186 |
+
|
| 187 |
+
def test_empty_array(self):
|
| 188 |
+
result = json_normalize([])
|
| 189 |
+
expected = DataFrame()
|
| 190 |
+
tm.assert_frame_equal(result, expected)
|
| 191 |
+
|
| 192 |
+
@pytest.mark.parametrize(
|
| 193 |
+
"data, record_path, exception_type",
|
| 194 |
+
[
|
| 195 |
+
([{"a": 0}, {"a": 1}], None, None),
|
| 196 |
+
({"a": [{"a": 0}, {"a": 1}]}, "a", None),
|
| 197 |
+
('{"a": [{"a": 0}, {"a": 1}]}', None, NotImplementedError),
|
| 198 |
+
(None, None, NotImplementedError),
|
| 199 |
+
],
|
| 200 |
+
)
|
| 201 |
+
def test_accepted_input(self, data, record_path, exception_type):
|
| 202 |
+
if exception_type is not None:
|
| 203 |
+
with pytest.raises(exception_type, match=""):
|
| 204 |
+
json_normalize(data, record_path=record_path)
|
| 205 |
+
else:
|
| 206 |
+
result = json_normalize(data, record_path=record_path)
|
| 207 |
+
expected = DataFrame([0, 1], columns=["a"])
|
| 208 |
+
tm.assert_frame_equal(result, expected)
|
| 209 |
+
|
| 210 |
+
def test_simple_normalize_with_separator(self, deep_nested):
|
| 211 |
+
# GH 14883
|
| 212 |
+
result = json_normalize({"A": {"A": 1, "B": 2}})
|
| 213 |
+
expected = DataFrame([[1, 2]], columns=["A.A", "A.B"])
|
| 214 |
+
tm.assert_frame_equal(result.reindex_like(expected), expected)
|
| 215 |
+
|
| 216 |
+
result = json_normalize({"A": {"A": 1, "B": 2}}, sep="_")
|
| 217 |
+
expected = DataFrame([[1, 2]], columns=["A_A", "A_B"])
|
| 218 |
+
tm.assert_frame_equal(result.reindex_like(expected), expected)
|
| 219 |
+
|
| 220 |
+
result = json_normalize({"A": {"A": 1, "B": 2}}, sep="\u03c3")
|
| 221 |
+
expected = DataFrame([[1, 2]], columns=["A\u03c3A", "A\u03c3B"])
|
| 222 |
+
tm.assert_frame_equal(result.reindex_like(expected), expected)
|
| 223 |
+
|
| 224 |
+
result = json_normalize(
|
| 225 |
+
deep_nested,
|
| 226 |
+
["states", "cities"],
|
| 227 |
+
meta=["country", ["states", "name"]],
|
| 228 |
+
sep="_",
|
| 229 |
+
)
|
| 230 |
+
expected = Index(["name", "pop", "country", "states_name"]).sort_values()
|
| 231 |
+
assert result.columns.sort_values().equals(expected)
|
| 232 |
+
|
| 233 |
+
def test_normalize_with_multichar_separator(self):
|
| 234 |
+
# GH #43831
|
| 235 |
+
data = {"a": [1, 2], "b": {"b_1": 2, "b_2": (3, 4)}}
|
| 236 |
+
result = json_normalize(data, sep="__")
|
| 237 |
+
expected = DataFrame([[[1, 2], 2, (3, 4)]], columns=["a", "b__b_1", "b__b_2"])
|
| 238 |
+
tm.assert_frame_equal(result, expected)
|
| 239 |
+
|
| 240 |
+
def test_value_array_record_prefix(self):
|
| 241 |
+
# GH 21536
|
| 242 |
+
result = json_normalize({"A": [1, 2]}, "A", record_prefix="Prefix.")
|
| 243 |
+
expected = DataFrame([[1], [2]], columns=["Prefix.0"])
|
| 244 |
+
tm.assert_frame_equal(result, expected)
|
| 245 |
+
|
| 246 |
+
def test_nested_object_record_path(self):
|
| 247 |
+
# GH 22706
|
| 248 |
+
data = {
|
| 249 |
+
"state": "Florida",
|
| 250 |
+
"info": {
|
| 251 |
+
"governor": "Rick Scott",
|
| 252 |
+
"counties": [
|
| 253 |
+
{"name": "Dade", "population": 12345},
|
| 254 |
+
{"name": "Broward", "population": 40000},
|
| 255 |
+
{"name": "Palm Beach", "population": 60000},
|
| 256 |
+
],
|
| 257 |
+
},
|
| 258 |
+
}
|
| 259 |
+
result = json_normalize(data, record_path=["info", "counties"])
|
| 260 |
+
expected = DataFrame(
|
| 261 |
+
[["Dade", 12345], ["Broward", 40000], ["Palm Beach", 60000]],
|
| 262 |
+
columns=["name", "population"],
|
| 263 |
+
)
|
| 264 |
+
tm.assert_frame_equal(result, expected)
|
| 265 |
+
|
| 266 |
+
def test_more_deeply_nested(self, deep_nested):
|
| 267 |
+
result = json_normalize(
|
| 268 |
+
deep_nested, ["states", "cities"], meta=["country", ["states", "name"]]
|
| 269 |
+
)
|
| 270 |
+
ex_data = {
|
| 271 |
+
"country": ["USA"] * 4 + ["Germany"] * 3,
|
| 272 |
+
"states.name": [
|
| 273 |
+
"California",
|
| 274 |
+
"California",
|
| 275 |
+
"Ohio",
|
| 276 |
+
"Ohio",
|
| 277 |
+
"Bayern",
|
| 278 |
+
"Nordrhein-Westfalen",
|
| 279 |
+
"Nordrhein-Westfalen",
|
| 280 |
+
],
|
| 281 |
+
"name": [
|
| 282 |
+
"San Francisco",
|
| 283 |
+
"Los Angeles",
|
| 284 |
+
"Columbus",
|
| 285 |
+
"Cleveland",
|
| 286 |
+
"Munich",
|
| 287 |
+
"Duesseldorf",
|
| 288 |
+
"Koeln",
|
| 289 |
+
],
|
| 290 |
+
"pop": [12345, 12346, 1234, 1236, 12347, 1238, 1239],
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
expected = DataFrame(ex_data, columns=result.columns)
|
| 294 |
+
tm.assert_frame_equal(result, expected)
|
| 295 |
+
|
| 296 |
+
def test_shallow_nested(self):
|
| 297 |
+
data = [
|
| 298 |
+
{
|
| 299 |
+
"state": "Florida",
|
| 300 |
+
"shortname": "FL",
|
| 301 |
+
"info": {"governor": "Rick Scott"},
|
| 302 |
+
"counties": [
|
| 303 |
+
{"name": "Dade", "population": 12345},
|
| 304 |
+
{"name": "Broward", "population": 40000},
|
| 305 |
+
{"name": "Palm Beach", "population": 60000},
|
| 306 |
+
],
|
| 307 |
+
},
|
| 308 |
+
{
|
| 309 |
+
"state": "Ohio",
|
| 310 |
+
"shortname": "OH",
|
| 311 |
+
"info": {"governor": "John Kasich"},
|
| 312 |
+
"counties": [
|
| 313 |
+
{"name": "Summit", "population": 1234},
|
| 314 |
+
{"name": "Cuyahoga", "population": 1337},
|
| 315 |
+
],
|
| 316 |
+
},
|
| 317 |
+
]
|
| 318 |
+
|
| 319 |
+
result = json_normalize(
|
| 320 |
+
data, "counties", ["state", "shortname", ["info", "governor"]]
|
| 321 |
+
)
|
| 322 |
+
ex_data = {
|
| 323 |
+
"name": ["Dade", "Broward", "Palm Beach", "Summit", "Cuyahoga"],
|
| 324 |
+
"state": ["Florida"] * 3 + ["Ohio"] * 2,
|
| 325 |
+
"shortname": ["FL", "FL", "FL", "OH", "OH"],
|
| 326 |
+
"info.governor": ["Rick Scott"] * 3 + ["John Kasich"] * 2,
|
| 327 |
+
"population": [12345, 40000, 60000, 1234, 1337],
|
| 328 |
+
}
|
| 329 |
+
expected = DataFrame(ex_data, columns=result.columns)
|
| 330 |
+
tm.assert_frame_equal(result, expected)
|
| 331 |
+
|
| 332 |
+
def test_nested_meta_path_with_nested_record_path(self, state_data):
|
| 333 |
+
# GH 27220
|
| 334 |
+
result = json_normalize(
|
| 335 |
+
data=state_data,
|
| 336 |
+
record_path=["counties"],
|
| 337 |
+
meta=["state", "shortname", ["info", "governor"]],
|
| 338 |
+
errors="ignore",
|
| 339 |
+
)
|
| 340 |
+
|
| 341 |
+
ex_data = {
|
| 342 |
+
"name": ["Dade", "Broward", "Palm Beach", "Summit", "Cuyahoga"],
|
| 343 |
+
"population": [12345, 40000, 60000, 1234, 1337],
|
| 344 |
+
"state": ["Florida"] * 3 + ["Ohio"] * 2,
|
| 345 |
+
"shortname": ["FL"] * 3 + ["OH"] * 2,
|
| 346 |
+
"info.governor": ["Rick Scott"] * 3 + ["John Kasich"] * 2,
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
expected = DataFrame(ex_data)
|
| 350 |
+
tm.assert_frame_equal(result, expected)
|
| 351 |
+
|
| 352 |
+
def test_meta_name_conflict(self):
|
| 353 |
+
data = [
|
| 354 |
+
{
|
| 355 |
+
"foo": "hello",
|
| 356 |
+
"bar": "there",
|
| 357 |
+
"data": [
|
| 358 |
+
{"foo": "something", "bar": "else"},
|
| 359 |
+
{"foo": "something2", "bar": "else2"},
|
| 360 |
+
],
|
| 361 |
+
}
|
| 362 |
+
]
|
| 363 |
+
|
| 364 |
+
msg = r"Conflicting metadata name (foo|bar), need distinguishing prefix"
|
| 365 |
+
with pytest.raises(ValueError, match=msg):
|
| 366 |
+
json_normalize(data, "data", meta=["foo", "bar"])
|
| 367 |
+
|
| 368 |
+
result = json_normalize(data, "data", meta=["foo", "bar"], meta_prefix="meta")
|
| 369 |
+
|
| 370 |
+
for val in ["metafoo", "metabar", "foo", "bar"]:
|
| 371 |
+
assert val in result
|
| 372 |
+
|
| 373 |
+
def test_meta_parameter_not_modified(self):
|
| 374 |
+
# GH 18610
|
| 375 |
+
data = [
|
| 376 |
+
{
|
| 377 |
+
"foo": "hello",
|
| 378 |
+
"bar": "there",
|
| 379 |
+
"data": [
|
| 380 |
+
{"foo": "something", "bar": "else"},
|
| 381 |
+
{"foo": "something2", "bar": "else2"},
|
| 382 |
+
],
|
| 383 |
+
}
|
| 384 |
+
]
|
| 385 |
+
|
| 386 |
+
COLUMNS = ["foo", "bar"]
|
| 387 |
+
result = json_normalize(data, "data", meta=COLUMNS, meta_prefix="meta")
|
| 388 |
+
|
| 389 |
+
assert COLUMNS == ["foo", "bar"]
|
| 390 |
+
for val in ["metafoo", "metabar", "foo", "bar"]:
|
| 391 |
+
assert val in result
|
| 392 |
+
|
| 393 |
+
def test_record_prefix(self, state_data):
|
| 394 |
+
result = json_normalize(state_data[0], "counties")
|
| 395 |
+
expected = DataFrame(state_data[0]["counties"])
|
| 396 |
+
tm.assert_frame_equal(result, expected)
|
| 397 |
+
|
| 398 |
+
result = json_normalize(
|
| 399 |
+
state_data, "counties", meta="state", record_prefix="county_"
|
| 400 |
+
)
|
| 401 |
+
|
| 402 |
+
expected = []
|
| 403 |
+
for rec in state_data:
|
| 404 |
+
expected.extend(rec["counties"])
|
| 405 |
+
expected = DataFrame(expected)
|
| 406 |
+
expected = expected.rename(columns=lambda x: "county_" + x)
|
| 407 |
+
expected["state"] = np.array(["Florida", "Ohio"]).repeat([3, 2])
|
| 408 |
+
|
| 409 |
+
tm.assert_frame_equal(result, expected)
|
| 410 |
+
|
| 411 |
+
def test_non_ascii_key(self):
|
| 412 |
+
testjson = (
|
| 413 |
+
b'[{"\xc3\x9cnic\xc3\xb8de":0,"sub":{"A":1, "B":2}},'
|
| 414 |
+
b'{"\xc3\x9cnic\xc3\xb8de":1,"sub":{"A":3, "B":4}}]'
|
| 415 |
+
).decode("utf8")
|
| 416 |
+
|
| 417 |
+
testdata = {
|
| 418 |
+
b"\xc3\x9cnic\xc3\xb8de".decode("utf8"): [0, 1],
|
| 419 |
+
"sub.A": [1, 3],
|
| 420 |
+
"sub.B": [2, 4],
|
| 421 |
+
}
|
| 422 |
+
expected = DataFrame(testdata)
|
| 423 |
+
|
| 424 |
+
result = json_normalize(json.loads(testjson))
|
| 425 |
+
tm.assert_frame_equal(result, expected)
|
| 426 |
+
|
| 427 |
+
def test_missing_field(self, author_missing_data):
|
| 428 |
+
# GH20030:
|
| 429 |
+
result = json_normalize(author_missing_data)
|
| 430 |
+
ex_data = [
|
| 431 |
+
{
|
| 432 |
+
"info": np.nan,
|
| 433 |
+
"info.created_at": np.nan,
|
| 434 |
+
"info.last_updated": np.nan,
|
| 435 |
+
"author_name.first": np.nan,
|
| 436 |
+
"author_name.last_name": np.nan,
|
| 437 |
+
},
|
| 438 |
+
{
|
| 439 |
+
"info": None,
|
| 440 |
+
"info.created_at": "11/08/1993",
|
| 441 |
+
"info.last_updated": "26/05/2012",
|
| 442 |
+
"author_name.first": "Jane",
|
| 443 |
+
"author_name.last_name": "Doe",
|
| 444 |
+
},
|
| 445 |
+
]
|
| 446 |
+
expected = DataFrame(ex_data)
|
| 447 |
+
tm.assert_frame_equal(result, expected)
|
| 448 |
+
|
| 449 |
+
@pytest.mark.parametrize(
|
| 450 |
+
"max_level,expected",
|
| 451 |
+
[
|
| 452 |
+
(
|
| 453 |
+
0,
|
| 454 |
+
[
|
| 455 |
+
{
|
| 456 |
+
"TextField": "Some text",
|
| 457 |
+
"UserField": {"Id": "ID001", "Name": "Name001"},
|
| 458 |
+
"CreatedBy": {"Name": "User001"},
|
| 459 |
+
"Image": {"a": "b"},
|
| 460 |
+
},
|
| 461 |
+
{
|
| 462 |
+
"TextField": "Some text",
|
| 463 |
+
"UserField": {"Id": "ID001", "Name": "Name001"},
|
| 464 |
+
"CreatedBy": {"Name": "User001"},
|
| 465 |
+
"Image": {"a": "b"},
|
| 466 |
+
},
|
| 467 |
+
],
|
| 468 |
+
),
|
| 469 |
+
(
|
| 470 |
+
1,
|
| 471 |
+
[
|
| 472 |
+
{
|
| 473 |
+
"TextField": "Some text",
|
| 474 |
+
"UserField.Id": "ID001",
|
| 475 |
+
"UserField.Name": "Name001",
|
| 476 |
+
"CreatedBy": {"Name": "User001"},
|
| 477 |
+
"Image": {"a": "b"},
|
| 478 |
+
},
|
| 479 |
+
{
|
| 480 |
+
"TextField": "Some text",
|
| 481 |
+
"UserField.Id": "ID001",
|
| 482 |
+
"UserField.Name": "Name001",
|
| 483 |
+
"CreatedBy": {"Name": "User001"},
|
| 484 |
+
"Image": {"a": "b"},
|
| 485 |
+
},
|
| 486 |
+
],
|
| 487 |
+
),
|
| 488 |
+
],
|
| 489 |
+
)
|
| 490 |
+
def test_max_level_with_records_path(self, max_level, expected):
|
| 491 |
+
# GH23843: Enhanced JSON normalize
|
| 492 |
+
test_input = [
|
| 493 |
+
{
|
| 494 |
+
"CreatedBy": {"Name": "User001"},
|
| 495 |
+
"Lookup": [
|
| 496 |
+
{
|
| 497 |
+
"TextField": "Some text",
|
| 498 |
+
"UserField": {"Id": "ID001", "Name": "Name001"},
|
| 499 |
+
},
|
| 500 |
+
{
|
| 501 |
+
"TextField": "Some text",
|
| 502 |
+
"UserField": {"Id": "ID001", "Name": "Name001"},
|
| 503 |
+
},
|
| 504 |
+
],
|
| 505 |
+
"Image": {"a": "b"},
|
| 506 |
+
"tags": [
|
| 507 |
+
{"foo": "something", "bar": "else"},
|
| 508 |
+
{"foo": "something2", "bar": "else2"},
|
| 509 |
+
],
|
| 510 |
+
}
|
| 511 |
+
]
|
| 512 |
+
|
| 513 |
+
result = json_normalize(
|
| 514 |
+
test_input,
|
| 515 |
+
record_path=["Lookup"],
|
| 516 |
+
meta=[["CreatedBy"], ["Image"]],
|
| 517 |
+
max_level=max_level,
|
| 518 |
+
)
|
| 519 |
+
expected_df = DataFrame(data=expected, columns=result.columns.values)
|
| 520 |
+
tm.assert_equal(expected_df, result)
|
| 521 |
+
|
| 522 |
+
def test_nested_flattening_consistent(self):
|
| 523 |
+
# see gh-21537
|
| 524 |
+
df1 = json_normalize([{"A": {"B": 1}}])
|
| 525 |
+
df2 = json_normalize({"dummy": [{"A": {"B": 1}}]}, "dummy")
|
| 526 |
+
|
| 527 |
+
# They should be the same.
|
| 528 |
+
tm.assert_frame_equal(df1, df2)
|
| 529 |
+
|
| 530 |
+
def test_nonetype_record_path(self, nulls_fixture):
|
| 531 |
+
# see gh-30148
|
| 532 |
+
# should not raise TypeError
|
| 533 |
+
result = json_normalize(
|
| 534 |
+
[
|
| 535 |
+
{"state": "Texas", "info": nulls_fixture},
|
| 536 |
+
{"state": "Florida", "info": [{"i": 2}]},
|
| 537 |
+
],
|
| 538 |
+
record_path=["info"],
|
| 539 |
+
)
|
| 540 |
+
expected = DataFrame({"i": 2}, index=[0])
|
| 541 |
+
tm.assert_equal(result, expected)
|
| 542 |
+
|
| 543 |
+
@pytest.mark.parametrize("value", ["false", "true", "{}", "1", '"text"'])
|
| 544 |
+
def test_non_list_record_path_errors(self, value):
|
| 545 |
+
# see gh-30148, GH 26284
|
| 546 |
+
parsed_value = json.loads(value)
|
| 547 |
+
test_input = {"state": "Texas", "info": parsed_value}
|
| 548 |
+
test_path = "info"
|
| 549 |
+
msg = (
|
| 550 |
+
f"{test_input} has non list value {parsed_value} for path {test_path}. "
|
| 551 |
+
"Must be list or null."
|
| 552 |
+
)
|
| 553 |
+
with pytest.raises(TypeError, match=msg):
|
| 554 |
+
json_normalize([test_input], record_path=[test_path])
|
| 555 |
+
|
| 556 |
+
def test_meta_non_iterable(self):
|
| 557 |
+
# GH 31507
|
| 558 |
+
data = """[{"id": 99, "data": [{"one": 1, "two": 2}]}]"""
|
| 559 |
+
|
| 560 |
+
result = json_normalize(json.loads(data), record_path=["data"], meta=["id"])
|
| 561 |
+
expected = DataFrame(
|
| 562 |
+
{"one": [1], "two": [2], "id": np.array([99], dtype=object)}
|
| 563 |
+
)
|
| 564 |
+
tm.assert_frame_equal(result, expected)
|
| 565 |
+
|
| 566 |
+
def test_generator(self, state_data):
|
| 567 |
+
# GH35923 Fix pd.json_normalize to not skip the first element of a
|
| 568 |
+
# generator input
|
| 569 |
+
def generator_data():
|
| 570 |
+
yield from state_data[0]["counties"]
|
| 571 |
+
|
| 572 |
+
result = json_normalize(generator_data())
|
| 573 |
+
expected = DataFrame(state_data[0]["counties"])
|
| 574 |
+
|
| 575 |
+
tm.assert_frame_equal(result, expected)
|
| 576 |
+
|
| 577 |
+
def test_top_column_with_leading_underscore(self):
|
| 578 |
+
# 49861
|
| 579 |
+
data = {"_id": {"a1": 10, "l2": {"l3": 0}}, "gg": 4}
|
| 580 |
+
result = json_normalize(data, sep="_")
|
| 581 |
+
expected = DataFrame([[4, 10, 0]], columns=["gg", "_id_a1", "_id_l2_l3"])
|
| 582 |
+
|
| 583 |
+
tm.assert_frame_equal(result, expected)
|
| 584 |
+
|
| 585 |
+
|
| 586 |
+
class TestNestedToRecord:
|
| 587 |
+
def test_flat_stays_flat(self):
|
| 588 |
+
recs = [{"flat1": 1, "flat2": 2}, {"flat3": 3, "flat2": 4}]
|
| 589 |
+
result = nested_to_record(recs)
|
| 590 |
+
expected = recs
|
| 591 |
+
assert result == expected
|
| 592 |
+
|
| 593 |
+
def test_one_level_deep_flattens(self):
|
| 594 |
+
data = {"flat1": 1, "dict1": {"c": 1, "d": 2}}
|
| 595 |
+
|
| 596 |
+
result = nested_to_record(data)
|
| 597 |
+
expected = {"dict1.c": 1, "dict1.d": 2, "flat1": 1}
|
| 598 |
+
|
| 599 |
+
assert result == expected
|
| 600 |
+
|
| 601 |
+
def test_nested_flattens(self):
|
| 602 |
+
data = {
|
| 603 |
+
"flat1": 1,
|
| 604 |
+
"dict1": {"c": 1, "d": 2},
|
| 605 |
+
"nested": {"e": {"c": 1, "d": 2}, "d": 2},
|
| 606 |
+
}
|
| 607 |
+
|
| 608 |
+
result = nested_to_record(data)
|
| 609 |
+
expected = {
|
| 610 |
+
"dict1.c": 1,
|
| 611 |
+
"dict1.d": 2,
|
| 612 |
+
"flat1": 1,
|
| 613 |
+
"nested.d": 2,
|
| 614 |
+
"nested.e.c": 1,
|
| 615 |
+
"nested.e.d": 2,
|
| 616 |
+
}
|
| 617 |
+
|
| 618 |
+
assert result == expected
|
| 619 |
+
|
| 620 |
+
def test_json_normalize_errors(self, missing_metadata):
|
| 621 |
+
# GH14583:
|
| 622 |
+
# If meta keys are not always present a new option to set
|
| 623 |
+
# errors='ignore' has been implemented
|
| 624 |
+
|
| 625 |
+
msg = (
|
| 626 |
+
"Key 'name' not found. To replace missing values of "
|
| 627 |
+
"'name' with np.nan, pass in errors='ignore'"
|
| 628 |
+
)
|
| 629 |
+
with pytest.raises(KeyError, match=msg):
|
| 630 |
+
json_normalize(
|
| 631 |
+
data=missing_metadata,
|
| 632 |
+
record_path="addresses",
|
| 633 |
+
meta="name",
|
| 634 |
+
errors="raise",
|
| 635 |
+
)
|
| 636 |
+
|
| 637 |
+
def test_missing_meta(self, missing_metadata):
|
| 638 |
+
# GH25468
|
| 639 |
+
# If metadata is nullable with errors set to ignore, the null values
|
| 640 |
+
# should be numpy.nan values
|
| 641 |
+
result = json_normalize(
|
| 642 |
+
data=missing_metadata, record_path="addresses", meta="name", errors="ignore"
|
| 643 |
+
)
|
| 644 |
+
ex_data = [
|
| 645 |
+
[9562, "Morris St.", "Massillon", "OH", 44646, "Alice"],
|
| 646 |
+
[8449, "Spring St.", "Elizabethton", "TN", 37643, np.nan],
|
| 647 |
+
]
|
| 648 |
+
columns = ["number", "street", "city", "state", "zip", "name"]
|
| 649 |
+
expected = DataFrame(ex_data, columns=columns)
|
| 650 |
+
tm.assert_frame_equal(result, expected)
|
| 651 |
+
|
| 652 |
+
def test_missing_nested_meta(self):
|
| 653 |
+
# GH44312
|
| 654 |
+
# If errors="ignore" and nested metadata is null, we should return nan
|
| 655 |
+
data = {"meta": "foo", "nested_meta": None, "value": [{"rec": 1}, {"rec": 2}]}
|
| 656 |
+
result = json_normalize(
|
| 657 |
+
data,
|
| 658 |
+
record_path="value",
|
| 659 |
+
meta=["meta", ["nested_meta", "leaf"]],
|
| 660 |
+
errors="ignore",
|
| 661 |
+
)
|
| 662 |
+
ex_data = [[1, "foo", np.nan], [2, "foo", np.nan]]
|
| 663 |
+
columns = ["rec", "meta", "nested_meta.leaf"]
|
| 664 |
+
expected = DataFrame(ex_data, columns=columns).astype(
|
| 665 |
+
{"nested_meta.leaf": object}
|
| 666 |
+
)
|
| 667 |
+
tm.assert_frame_equal(result, expected)
|
| 668 |
+
|
| 669 |
+
# If errors="raise" and nested metadata is null, we should raise with the
|
| 670 |
+
# key of the first missing level
|
| 671 |
+
with pytest.raises(KeyError, match="'leaf' not found"):
|
| 672 |
+
json_normalize(
|
| 673 |
+
data,
|
| 674 |
+
record_path="value",
|
| 675 |
+
meta=["meta", ["nested_meta", "leaf"]],
|
| 676 |
+
errors="raise",
|
| 677 |
+
)
|
| 678 |
+
|
| 679 |
+
def test_missing_meta_multilevel_record_path_errors_raise(self, missing_metadata):
|
| 680 |
+
# GH41876
|
| 681 |
+
# Ensure errors='raise' works as intended even when a record_path of length
|
| 682 |
+
# greater than one is passed in
|
| 683 |
+
msg = (
|
| 684 |
+
"Key 'name' not found. To replace missing values of "
|
| 685 |
+
"'name' with np.nan, pass in errors='ignore'"
|
| 686 |
+
)
|
| 687 |
+
with pytest.raises(KeyError, match=msg):
|
| 688 |
+
json_normalize(
|
| 689 |
+
data=missing_metadata,
|
| 690 |
+
record_path=["previous_residences", "cities"],
|
| 691 |
+
meta="name",
|
| 692 |
+
errors="raise",
|
| 693 |
+
)
|
| 694 |
+
|
| 695 |
+
def test_missing_meta_multilevel_record_path_errors_ignore(self, missing_metadata):
|
| 696 |
+
# GH41876
|
| 697 |
+
# Ensure errors='ignore' works as intended even when a record_path of length
|
| 698 |
+
# greater than one is passed in
|
| 699 |
+
result = json_normalize(
|
| 700 |
+
data=missing_metadata,
|
| 701 |
+
record_path=["previous_residences", "cities"],
|
| 702 |
+
meta="name",
|
| 703 |
+
errors="ignore",
|
| 704 |
+
)
|
| 705 |
+
ex_data = [
|
| 706 |
+
["Foo York City", "Alice"],
|
| 707 |
+
["Barmingham", np.nan],
|
| 708 |
+
]
|
| 709 |
+
columns = ["city_name", "name"]
|
| 710 |
+
expected = DataFrame(ex_data, columns=columns)
|
| 711 |
+
tm.assert_frame_equal(result, expected)
|
| 712 |
+
|
| 713 |
+
def test_donot_drop_nonevalues(self):
|
| 714 |
+
# GH21356
|
| 715 |
+
data = [
|
| 716 |
+
{"info": None, "author_name": {"first": "Smith", "last_name": "Appleseed"}},
|
| 717 |
+
{
|
| 718 |
+
"info": {"created_at": "11/08/1993", "last_updated": "26/05/2012"},
|
| 719 |
+
"author_name": {"first": "Jane", "last_name": "Doe"},
|
| 720 |
+
},
|
| 721 |
+
]
|
| 722 |
+
result = nested_to_record(data)
|
| 723 |
+
expected = [
|
| 724 |
+
{
|
| 725 |
+
"info": None,
|
| 726 |
+
"author_name.first": "Smith",
|
| 727 |
+
"author_name.last_name": "Appleseed",
|
| 728 |
+
},
|
| 729 |
+
{
|
| 730 |
+
"author_name.first": "Jane",
|
| 731 |
+
"author_name.last_name": "Doe",
|
| 732 |
+
"info.created_at": "11/08/1993",
|
| 733 |
+
"info.last_updated": "26/05/2012",
|
| 734 |
+
},
|
| 735 |
+
]
|
| 736 |
+
|
| 737 |
+
assert result == expected
|
| 738 |
+
|
| 739 |
+
def test_nonetype_top_level_bottom_level(self):
|
| 740 |
+
# GH21158: If inner level json has a key with a null value
|
| 741 |
+
# make sure it does not do a new_d.pop twice and except
|
| 742 |
+
data = {
|
| 743 |
+
"id": None,
|
| 744 |
+
"location": {
|
| 745 |
+
"country": {
|
| 746 |
+
"state": {
|
| 747 |
+
"id": None,
|
| 748 |
+
"town.info": {
|
| 749 |
+
"id": None,
|
| 750 |
+
"region": None,
|
| 751 |
+
"x": 49.151580810546875,
|
| 752 |
+
"y": -33.148521423339844,
|
| 753 |
+
"z": 27.572303771972656,
|
| 754 |
+
},
|
| 755 |
+
}
|
| 756 |
+
}
|
| 757 |
+
},
|
| 758 |
+
}
|
| 759 |
+
result = nested_to_record(data)
|
| 760 |
+
expected = {
|
| 761 |
+
"id": None,
|
| 762 |
+
"location.country.state.id": None,
|
| 763 |
+
"location.country.state.town.info.id": None,
|
| 764 |
+
"location.country.state.town.info.region": None,
|
| 765 |
+
"location.country.state.town.info.x": 49.151580810546875,
|
| 766 |
+
"location.country.state.town.info.y": -33.148521423339844,
|
| 767 |
+
"location.country.state.town.info.z": 27.572303771972656,
|
| 768 |
+
}
|
| 769 |
+
assert result == expected
|
| 770 |
+
|
| 771 |
+
def test_nonetype_multiple_levels(self):
|
| 772 |
+
# GH21158: If inner level json has a key with a null value
|
| 773 |
+
# make sure it does not do a new_d.pop twice and except
|
| 774 |
+
data = {
|
| 775 |
+
"id": None,
|
| 776 |
+
"location": {
|
| 777 |
+
"id": None,
|
| 778 |
+
"country": {
|
| 779 |
+
"id": None,
|
| 780 |
+
"state": {
|
| 781 |
+
"id": None,
|
| 782 |
+
"town.info": {
|
| 783 |
+
"region": None,
|
| 784 |
+
"x": 49.151580810546875,
|
| 785 |
+
"y": -33.148521423339844,
|
| 786 |
+
"z": 27.572303771972656,
|
| 787 |
+
},
|
| 788 |
+
},
|
| 789 |
+
},
|
| 790 |
+
},
|
| 791 |
+
}
|
| 792 |
+
result = nested_to_record(data)
|
| 793 |
+
expected = {
|
| 794 |
+
"id": None,
|
| 795 |
+
"location.id": None,
|
| 796 |
+
"location.country.id": None,
|
| 797 |
+
"location.country.state.id": None,
|
| 798 |
+
"location.country.state.town.info.region": None,
|
| 799 |
+
"location.country.state.town.info.x": 49.151580810546875,
|
| 800 |
+
"location.country.state.town.info.y": -33.148521423339844,
|
| 801 |
+
"location.country.state.town.info.z": 27.572303771972656,
|
| 802 |
+
}
|
| 803 |
+
assert result == expected
|
| 804 |
+
|
| 805 |
+
@pytest.mark.parametrize(
|
| 806 |
+
"max_level, expected",
|
| 807 |
+
[
|
| 808 |
+
(
|
| 809 |
+
None,
|
| 810 |
+
[
|
| 811 |
+
{
|
| 812 |
+
"CreatedBy.Name": "User001",
|
| 813 |
+
"Lookup.TextField": "Some text",
|
| 814 |
+
"Lookup.UserField.Id": "ID001",
|
| 815 |
+
"Lookup.UserField.Name": "Name001",
|
| 816 |
+
"Image.a": "b",
|
| 817 |
+
}
|
| 818 |
+
],
|
| 819 |
+
),
|
| 820 |
+
(
|
| 821 |
+
0,
|
| 822 |
+
[
|
| 823 |
+
{
|
| 824 |
+
"CreatedBy": {"Name": "User001"},
|
| 825 |
+
"Lookup": {
|
| 826 |
+
"TextField": "Some text",
|
| 827 |
+
"UserField": {"Id": "ID001", "Name": "Name001"},
|
| 828 |
+
},
|
| 829 |
+
"Image": {"a": "b"},
|
| 830 |
+
}
|
| 831 |
+
],
|
| 832 |
+
),
|
| 833 |
+
(
|
| 834 |
+
1,
|
| 835 |
+
[
|
| 836 |
+
{
|
| 837 |
+
"CreatedBy.Name": "User001",
|
| 838 |
+
"Lookup.TextField": "Some text",
|
| 839 |
+
"Lookup.UserField": {"Id": "ID001", "Name": "Name001"},
|
| 840 |
+
"Image.a": "b",
|
| 841 |
+
}
|
| 842 |
+
],
|
| 843 |
+
),
|
| 844 |
+
],
|
| 845 |
+
)
|
| 846 |
+
def test_with_max_level(self, max_level, expected, max_level_test_input_data):
|
| 847 |
+
# GH23843: Enhanced JSON normalize
|
| 848 |
+
output = nested_to_record(max_level_test_input_data, max_level=max_level)
|
| 849 |
+
assert output == expected
|
| 850 |
+
|
| 851 |
+
def test_with_large_max_level(self):
|
| 852 |
+
# GH23843: Enhanced JSON normalize
|
| 853 |
+
max_level = 100
|
| 854 |
+
input_data = [
|
| 855 |
+
{
|
| 856 |
+
"CreatedBy": {
|
| 857 |
+
"user": {
|
| 858 |
+
"name": {"firstname": "Leo", "LastName": "Thomson"},
|
| 859 |
+
"family_tree": {
|
| 860 |
+
"father": {
|
| 861 |
+
"name": "Father001",
|
| 862 |
+
"father": {
|
| 863 |
+
"Name": "Father002",
|
| 864 |
+
"father": {
|
| 865 |
+
"name": "Father003",
|
| 866 |
+
"father": {"Name": "Father004"},
|
| 867 |
+
},
|
| 868 |
+
},
|
| 869 |
+
}
|
| 870 |
+
},
|
| 871 |
+
}
|
| 872 |
+
}
|
| 873 |
+
}
|
| 874 |
+
]
|
| 875 |
+
expected = [
|
| 876 |
+
{
|
| 877 |
+
"CreatedBy.user.name.firstname": "Leo",
|
| 878 |
+
"CreatedBy.user.name.LastName": "Thomson",
|
| 879 |
+
"CreatedBy.user.family_tree.father.name": "Father001",
|
| 880 |
+
"CreatedBy.user.family_tree.father.father.Name": "Father002",
|
| 881 |
+
"CreatedBy.user.family_tree.father.father.father.name": "Father003",
|
| 882 |
+
"CreatedBy.user.family_tree.father.father.father.father.Name": "Father004", # noqa: E501
|
| 883 |
+
}
|
| 884 |
+
]
|
| 885 |
+
output = nested_to_record(input_data, max_level=max_level)
|
| 886 |
+
assert output == expected
|
| 887 |
+
|
| 888 |
+
def test_series_non_zero_index(self):
|
| 889 |
+
# GH 19020
|
| 890 |
+
data = {
|
| 891 |
+
0: {"id": 1, "name": "Foo", "elements": {"a": 1}},
|
| 892 |
+
1: {"id": 2, "name": "Bar", "elements": {"b": 2}},
|
| 893 |
+
2: {"id": 3, "name": "Baz", "elements": {"c": 3}},
|
| 894 |
+
}
|
| 895 |
+
s = Series(data)
|
| 896 |
+
s.index = [1, 2, 3]
|
| 897 |
+
result = json_normalize(s)
|
| 898 |
+
expected = DataFrame(
|
| 899 |
+
{
|
| 900 |
+
"id": [1, 2, 3],
|
| 901 |
+
"name": ["Foo", "Bar", "Baz"],
|
| 902 |
+
"elements.a": [1.0, np.nan, np.nan],
|
| 903 |
+
"elements.b": [np.nan, 2.0, np.nan],
|
| 904 |
+
"elements.c": [np.nan, np.nan, 3.0],
|
| 905 |
+
}
|
| 906 |
+
)
|
| 907 |
+
tm.assert_frame_equal(result, expected)
|
vllm/lib/python3.10/site-packages/pandas/tests/io/json/test_pandas.py
ADDED
|
@@ -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ytesIO,
|
| 6 |
+
StringIO,
|
| 7 |
+
)
|
| 8 |
+
import json
|
| 9 |
+
import os
|
| 10 |
+
import sys
|
| 11 |
+
import time
|
| 12 |
+
|
| 13 |
+
import numpy as np
|
| 14 |
+
import pytest
|
| 15 |
+
|
| 16 |
+
from pandas._config import using_pyarrow_string_dtype
|
| 17 |
+
|
| 18 |
+
from pandas.compat import IS64
|
| 19 |
+
import pandas.util._test_decorators as td
|
| 20 |
+
|
| 21 |
+
import pandas as pd
|
| 22 |
+
from pandas import (
|
| 23 |
+
NA,
|
| 24 |
+
DataFrame,
|
| 25 |
+
DatetimeIndex,
|
| 26 |
+
Index,
|
| 27 |
+
RangeIndex,
|
| 28 |
+
Series,
|
| 29 |
+
Timestamp,
|
| 30 |
+
date_range,
|
| 31 |
+
read_json,
|
| 32 |
+
)
|
| 33 |
+
import pandas._testing as tm
|
| 34 |
+
from pandas.core.arrays import (
|
| 35 |
+
ArrowStringArray,
|
| 36 |
+
StringArray,
|
| 37 |
+
)
|
| 38 |
+
from pandas.core.arrays.string_arrow import ArrowStringArrayNumpySemantics
|
| 39 |
+
|
| 40 |
+
from pandas.io.json import ujson_dumps
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def test_literal_json_deprecation():
|
| 44 |
+
# PR 53409
|
| 45 |
+
expected = DataFrame([[1, 2], [1, 2]], columns=["a", "b"])
|
| 46 |
+
|
| 47 |
+
jsonl = """{"a": 1, "b": 2}
|
| 48 |
+
{"a": 3, "b": 4}
|
| 49 |
+
{"a": 5, "b": 6}
|
| 50 |
+
{"a": 7, "b": 8}"""
|
| 51 |
+
|
| 52 |
+
msg = (
|
| 53 |
+
"Passing literal json to 'read_json' is deprecated and "
|
| 54 |
+
"will be removed in a future version. To read from a "
|
| 55 |
+
"literal string, wrap it in a 'StringIO' object."
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
| 59 |
+
try:
|
| 60 |
+
read_json(jsonl, lines=False)
|
| 61 |
+
except ValueError:
|
| 62 |
+
pass
|
| 63 |
+
|
| 64 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
| 65 |
+
read_json(expected.to_json(), lines=False)
|
| 66 |
+
|
| 67 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
| 68 |
+
result = read_json('{"a": 1, "b": 2}\n{"b":2, "a" :1}\n', lines=True)
|
| 69 |
+
tm.assert_frame_equal(result, expected)
|
| 70 |
+
|
| 71 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
| 72 |
+
try:
|
| 73 |
+
result = read_json(
|
| 74 |
+
'{"a\\\\":"foo\\\\","b":"bar"}\n{"a\\\\":"foo\\"","b":"bar"}\n',
|
| 75 |
+
lines=False,
|
| 76 |
+
)
|
| 77 |
+
except ValueError:
|
| 78 |
+
pass
|
| 79 |
+
|
| 80 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
| 81 |
+
try:
|
| 82 |
+
result = read_json('{"a": 1, "b": 2}\n{"b":2, "a" :1}\n', lines=False)
|
| 83 |
+
except ValueError:
|
| 84 |
+
pass
|
| 85 |
+
tm.assert_frame_equal(result, expected)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def assert_json_roundtrip_equal(result, expected, orient):
|
| 89 |
+
if orient in ("records", "values"):
|
| 90 |
+
expected = expected.reset_index(drop=True)
|
| 91 |
+
if orient == "values":
|
| 92 |
+
expected.columns = range(len(expected.columns))
|
| 93 |
+
tm.assert_frame_equal(result, expected)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
class TestPandasContainer:
|
| 97 |
+
@pytest.fixture
|
| 98 |
+
def categorical_frame(self):
|
| 99 |
+
data = {
|
| 100 |
+
c: np.random.default_rng(i).standard_normal(30)
|
| 101 |
+
for i, c in enumerate(list("ABCD"))
|
| 102 |
+
}
|
| 103 |
+
cat = ["bah"] * 5 + ["bar"] * 5 + ["baz"] * 5 + ["foo"] * 15
|
| 104 |
+
data["E"] = list(reversed(cat))
|
| 105 |
+
data["sort"] = np.arange(30, dtype="int64")
|
| 106 |
+
return DataFrame(data, index=pd.CategoricalIndex(cat, name="E"))
|
| 107 |
+
|
| 108 |
+
@pytest.fixture
|
| 109 |
+
def datetime_series(self):
|
| 110 |
+
# Same as usual datetime_series, but with index freq set to None,
|
| 111 |
+
# since that doesn't round-trip, see GH#33711
|
| 112 |
+
ser = Series(
|
| 113 |
+
1.1 * np.arange(10, dtype=np.float64),
|
| 114 |
+
index=date_range("2020-01-01", periods=10),
|
| 115 |
+
name="ts",
|
| 116 |
+
)
|
| 117 |
+
ser.index = ser.index._with_freq(None)
|
| 118 |
+
return ser
|
| 119 |
+
|
| 120 |
+
@pytest.fixture
|
| 121 |
+
def datetime_frame(self):
|
| 122 |
+
# Same as usual datetime_frame, but with index freq set to None,
|
| 123 |
+
# since that doesn't round-trip, see GH#33711
|
| 124 |
+
df = DataFrame(
|
| 125 |
+
np.random.default_rng(2).standard_normal((30, 4)),
|
| 126 |
+
columns=Index(list("ABCD"), dtype=object),
|
| 127 |
+
index=date_range("2000-01-01", periods=30, freq="B"),
|
| 128 |
+
)
|
| 129 |
+
df.index = df.index._with_freq(None)
|
| 130 |
+
return df
|
| 131 |
+
|
| 132 |
+
def test_frame_double_encoded_labels(self, orient):
|
| 133 |
+
df = DataFrame(
|
| 134 |
+
[["a", "b"], ["c", "d"]],
|
| 135 |
+
index=['index " 1', "index / 2"],
|
| 136 |
+
columns=["a \\ b", "y / z"],
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
data = StringIO(df.to_json(orient=orient))
|
| 140 |
+
result = read_json(data, orient=orient)
|
| 141 |
+
expected = df.copy()
|
| 142 |
+
assert_json_roundtrip_equal(result, expected, orient)
|
| 143 |
+
|
| 144 |
+
@pytest.mark.parametrize("orient", ["split", "records", "values"])
|
| 145 |
+
def test_frame_non_unique_index(self, orient):
|
| 146 |
+
df = DataFrame([["a", "b"], ["c", "d"]], index=[1, 1], columns=["x", "y"])
|
| 147 |
+
data = StringIO(df.to_json(orient=orient))
|
| 148 |
+
result = read_json(data, orient=orient)
|
| 149 |
+
expected = df.copy()
|
| 150 |
+
|
| 151 |
+
assert_json_roundtrip_equal(result, expected, orient)
|
| 152 |
+
|
| 153 |
+
@pytest.mark.parametrize("orient", ["index", "columns"])
|
| 154 |
+
def test_frame_non_unique_index_raises(self, orient):
|
| 155 |
+
df = DataFrame([["a", "b"], ["c", "d"]], index=[1, 1], columns=["x", "y"])
|
| 156 |
+
msg = f"DataFrame index must be unique for orient='{orient}'"
|
| 157 |
+
with pytest.raises(ValueError, match=msg):
|
| 158 |
+
df.to_json(orient=orient)
|
| 159 |
+
|
| 160 |
+
@pytest.mark.parametrize("orient", ["split", "values"])
|
| 161 |
+
@pytest.mark.parametrize(
|
| 162 |
+
"data",
|
| 163 |
+
[
|
| 164 |
+
[["a", "b"], ["c", "d"]],
|
| 165 |
+
[[1.5, 2.5], [3.5, 4.5]],
|
| 166 |
+
[[1, 2.5], [3, 4.5]],
|
| 167 |
+
[[Timestamp("20130101"), 3.5], [Timestamp("20130102"), 4.5]],
|
| 168 |
+
],
|
| 169 |
+
)
|
| 170 |
+
def test_frame_non_unique_columns(self, orient, data):
|
| 171 |
+
df = DataFrame(data, index=[1, 2], columns=["x", "x"])
|
| 172 |
+
|
| 173 |
+
result = read_json(
|
| 174 |
+
StringIO(df.to_json(orient=orient)), orient=orient, convert_dates=["x"]
|
| 175 |
+
)
|
| 176 |
+
if orient == "values":
|
| 177 |
+
expected = DataFrame(data)
|
| 178 |
+
if expected.iloc[:, 0].dtype == "datetime64[ns]":
|
| 179 |
+
# orient == "values" by default will write Timestamp objects out
|
| 180 |
+
# in milliseconds; these are internally stored in nanosecond,
|
| 181 |
+
# so divide to get where we need
|
| 182 |
+
# TODO: a to_epoch method would also solve; see GH 14772
|
| 183 |
+
expected.isetitem(0, expected.iloc[:, 0].astype(np.int64) // 1000000)
|
| 184 |
+
elif orient == "split":
|
| 185 |
+
expected = df
|
| 186 |
+
expected.columns = ["x", "x.1"]
|
| 187 |
+
|
| 188 |
+
tm.assert_frame_equal(result, expected)
|
| 189 |
+
|
| 190 |
+
@pytest.mark.parametrize("orient", ["index", "columns", "records"])
|
| 191 |
+
def test_frame_non_unique_columns_raises(self, orient):
|
| 192 |
+
df = DataFrame([["a", "b"], ["c", "d"]], index=[1, 2], columns=["x", "x"])
|
| 193 |
+
|
| 194 |
+
msg = f"DataFrame columns must be unique for orient='{orient}'"
|
| 195 |
+
with pytest.raises(ValueError, match=msg):
|
| 196 |
+
df.to_json(orient=orient)
|
| 197 |
+
|
| 198 |
+
def test_frame_default_orient(self, float_frame):
|
| 199 |
+
assert float_frame.to_json() == float_frame.to_json(orient="columns")
|
| 200 |
+
|
| 201 |
+
@pytest.mark.parametrize("dtype", [False, float])
|
| 202 |
+
@pytest.mark.parametrize("convert_axes", [True, False])
|
| 203 |
+
def test_roundtrip_simple(self, orient, convert_axes, dtype, float_frame):
|
| 204 |
+
data = StringIO(float_frame.to_json(orient=orient))
|
| 205 |
+
result = read_json(data, orient=orient, convert_axes=convert_axes, dtype=dtype)
|
| 206 |
+
|
| 207 |
+
expected = float_frame
|
| 208 |
+
|
| 209 |
+
assert_json_roundtrip_equal(result, expected, orient)
|
| 210 |
+
|
| 211 |
+
@pytest.mark.parametrize("dtype", [False, np.int64])
|
| 212 |
+
@pytest.mark.parametrize("convert_axes", [True, False])
|
| 213 |
+
def test_roundtrip_intframe(self, orient, convert_axes, dtype, int_frame):
|
| 214 |
+
data = StringIO(int_frame.to_json(orient=orient))
|
| 215 |
+
result = read_json(data, orient=orient, convert_axes=convert_axes, dtype=dtype)
|
| 216 |
+
expected = int_frame
|
| 217 |
+
assert_json_roundtrip_equal(result, expected, orient)
|
| 218 |
+
|
| 219 |
+
@pytest.mark.parametrize("dtype", [None, np.float64, int, "U3"])
|
| 220 |
+
@pytest.mark.parametrize("convert_axes", [True, False])
|
| 221 |
+
def test_roundtrip_str_axes(self, orient, convert_axes, dtype):
|
| 222 |
+
df = DataFrame(
|
| 223 |
+
np.zeros((200, 4)),
|
| 224 |
+
columns=[str(i) for i in range(4)],
|
| 225 |
+
index=[str(i) for i in range(200)],
|
| 226 |
+
dtype=dtype,
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
data = StringIO(df.to_json(orient=orient))
|
| 230 |
+
result = read_json(data, orient=orient, convert_axes=convert_axes, dtype=dtype)
|
| 231 |
+
|
| 232 |
+
expected = df.copy()
|
| 233 |
+
if not dtype:
|
| 234 |
+
expected = expected.astype(np.int64)
|
| 235 |
+
|
| 236 |
+
# index columns, and records orients cannot fully preserve the string
|
| 237 |
+
# dtype for axes as the index and column labels are used as keys in
|
| 238 |
+
# JSON objects. JSON keys are by definition strings, so there's no way
|
| 239 |
+
# to disambiguate whether those keys actually were strings or numeric
|
| 240 |
+
# beforehand and numeric wins out.
|
| 241 |
+
if convert_axes and (orient in ("index", "columns")):
|
| 242 |
+
expected.columns = expected.columns.astype(np.int64)
|
| 243 |
+
expected.index = expected.index.astype(np.int64)
|
| 244 |
+
elif orient == "records" and convert_axes:
|
| 245 |
+
expected.columns = expected.columns.astype(np.int64)
|
| 246 |
+
elif convert_axes and orient == "split":
|
| 247 |
+
expected.columns = expected.columns.astype(np.int64)
|
| 248 |
+
|
| 249 |
+
assert_json_roundtrip_equal(result, expected, orient)
|
| 250 |
+
|
| 251 |
+
@pytest.mark.parametrize("convert_axes", [True, False])
|
| 252 |
+
def test_roundtrip_categorical(
|
| 253 |
+
self, request, orient, categorical_frame, convert_axes, using_infer_string
|
| 254 |
+
):
|
| 255 |
+
# TODO: create a better frame to test with and improve coverage
|
| 256 |
+
if orient in ("index", "columns"):
|
| 257 |
+
request.applymarker(
|
| 258 |
+
pytest.mark.xfail(
|
| 259 |
+
reason=f"Can't have duplicate index values for orient '{orient}')"
|
| 260 |
+
)
|
| 261 |
+
)
|
| 262 |
+
|
| 263 |
+
data = StringIO(categorical_frame.to_json(orient=orient))
|
| 264 |
+
result = read_json(data, orient=orient, convert_axes=convert_axes)
|
| 265 |
+
|
| 266 |
+
expected = categorical_frame.copy()
|
| 267 |
+
expected.index = expected.index.astype(
|
| 268 |
+
str if not using_infer_string else "string[pyarrow_numpy]"
|
| 269 |
+
) # Categorical not preserved
|
| 270 |
+
expected.index.name = None # index names aren't preserved in JSON
|
| 271 |
+
assert_json_roundtrip_equal(result, expected, orient)
|
| 272 |
+
|
| 273 |
+
@pytest.mark.parametrize("convert_axes", [True, False])
|
| 274 |
+
def test_roundtrip_empty(self, orient, convert_axes):
|
| 275 |
+
empty_frame = DataFrame()
|
| 276 |
+
data = StringIO(empty_frame.to_json(orient=orient))
|
| 277 |
+
result = read_json(data, orient=orient, convert_axes=convert_axes)
|
| 278 |
+
if orient == "split":
|
| 279 |
+
idx = Index([], dtype=(float if convert_axes else object))
|
| 280 |
+
expected = DataFrame(index=idx, columns=idx)
|
| 281 |
+
elif orient in ["index", "columns"]:
|
| 282 |
+
expected = DataFrame()
|
| 283 |
+
else:
|
| 284 |
+
expected = empty_frame.copy()
|
| 285 |
+
|
| 286 |
+
tm.assert_frame_equal(result, expected)
|
| 287 |
+
|
| 288 |
+
@pytest.mark.parametrize("convert_axes", [True, False])
|
| 289 |
+
def test_roundtrip_timestamp(self, orient, convert_axes, datetime_frame):
|
| 290 |
+
# TODO: improve coverage with date_format parameter
|
| 291 |
+
data = StringIO(datetime_frame.to_json(orient=orient))
|
| 292 |
+
result = read_json(data, orient=orient, convert_axes=convert_axes)
|
| 293 |
+
expected = datetime_frame.copy()
|
| 294 |
+
|
| 295 |
+
if not convert_axes: # one off for ts handling
|
| 296 |
+
# DTI gets converted to epoch values
|
| 297 |
+
idx = expected.index.view(np.int64) // 1000000
|
| 298 |
+
if orient != "split": # TODO: handle consistently across orients
|
| 299 |
+
idx = idx.astype(str)
|
| 300 |
+
|
| 301 |
+
expected.index = idx
|
| 302 |
+
|
| 303 |
+
assert_json_roundtrip_equal(result, expected, orient)
|
| 304 |
+
|
| 305 |
+
@pytest.mark.parametrize("convert_axes", [True, False])
|
| 306 |
+
def test_roundtrip_mixed(self, orient, convert_axes):
|
| 307 |
+
index = Index(["a", "b", "c", "d", "e"])
|
| 308 |
+
values = {
|
| 309 |
+
"A": [0.0, 1.0, 2.0, 3.0, 4.0],
|
| 310 |
+
"B": [0.0, 1.0, 0.0, 1.0, 0.0],
|
| 311 |
+
"C": ["foo1", "foo2", "foo3", "foo4", "foo5"],
|
| 312 |
+
"D": [True, False, True, False, True],
|
| 313 |
+
}
|
| 314 |
+
|
| 315 |
+
df = DataFrame(data=values, index=index)
|
| 316 |
+
|
| 317 |
+
data = StringIO(df.to_json(orient=orient))
|
| 318 |
+
result = read_json(data, orient=orient, convert_axes=convert_axes)
|
| 319 |
+
|
| 320 |
+
expected = df.copy()
|
| 321 |
+
expected = expected.assign(**expected.select_dtypes("number").astype(np.int64))
|
| 322 |
+
|
| 323 |
+
assert_json_roundtrip_equal(result, expected, orient)
|
| 324 |
+
|
| 325 |
+
@pytest.mark.xfail(
|
| 326 |
+
reason="#50456 Column multiindex is stored and loaded differently",
|
| 327 |
+
raises=AssertionError,
|
| 328 |
+
)
|
| 329 |
+
@pytest.mark.parametrize(
|
| 330 |
+
"columns",
|
| 331 |
+
[
|
| 332 |
+
[["2022", "2022"], ["JAN", "FEB"]],
|
| 333 |
+
[["2022", "2023"], ["JAN", "JAN"]],
|
| 334 |
+
[["2022", "2022"], ["JAN", "JAN"]],
|
| 335 |
+
],
|
| 336 |
+
)
|
| 337 |
+
def test_roundtrip_multiindex(self, columns):
|
| 338 |
+
df = DataFrame(
|
| 339 |
+
[[1, 2], [3, 4]],
|
| 340 |
+
columns=pd.MultiIndex.from_arrays(columns),
|
| 341 |
+
)
|
| 342 |
+
data = StringIO(df.to_json(orient="split"))
|
| 343 |
+
result = read_json(data, orient="split")
|
| 344 |
+
tm.assert_frame_equal(result, df)
|
| 345 |
+
|
| 346 |
+
@pytest.mark.parametrize(
|
| 347 |
+
"data,msg,orient",
|
| 348 |
+
[
|
| 349 |
+
('{"key":b:a:d}', "Expected object or value", "columns"),
|
| 350 |
+
# too few indices
|
| 351 |
+
(
|
| 352 |
+
'{"columns":["A","B"],'
|
| 353 |
+
'"index":["2","3"],'
|
| 354 |
+
'"data":[[1.0,"1"],[2.0,"2"],[null,"3"]]}',
|
| 355 |
+
"|".join(
|
| 356 |
+
[
|
| 357 |
+
r"Length of values \(3\) does not match length of index \(2\)",
|
| 358 |
+
]
|
| 359 |
+
),
|
| 360 |
+
"split",
|
| 361 |
+
),
|
| 362 |
+
# too many columns
|
| 363 |
+
(
|
| 364 |
+
'{"columns":["A","B","C"],'
|
| 365 |
+
'"index":["1","2","3"],'
|
| 366 |
+
'"data":[[1.0,"1"],[2.0,"2"],[null,"3"]]}',
|
| 367 |
+
"3 columns passed, passed data had 2 columns",
|
| 368 |
+
"split",
|
| 369 |
+
),
|
| 370 |
+
# bad key
|
| 371 |
+
(
|
| 372 |
+
'{"badkey":["A","B"],'
|
| 373 |
+
'"index":["2","3"],'
|
| 374 |
+
'"data":[[1.0,"1"],[2.0,"2"],[null,"3"]]}',
|
| 375 |
+
r"unexpected key\(s\): badkey",
|
| 376 |
+
"split",
|
| 377 |
+
),
|
| 378 |
+
],
|
| 379 |
+
)
|
| 380 |
+
def test_frame_from_json_bad_data_raises(self, data, msg, orient):
|
| 381 |
+
with pytest.raises(ValueError, match=msg):
|
| 382 |
+
read_json(StringIO(data), orient=orient)
|
| 383 |
+
|
| 384 |
+
@pytest.mark.parametrize("dtype", [True, False])
|
| 385 |
+
@pytest.mark.parametrize("convert_axes", [True, False])
|
| 386 |
+
def test_frame_from_json_missing_data(self, orient, convert_axes, dtype):
|
| 387 |
+
num_df = DataFrame([[1, 2], [4, 5, 6]])
|
| 388 |
+
|
| 389 |
+
result = read_json(
|
| 390 |
+
StringIO(num_df.to_json(orient=orient)),
|
| 391 |
+
orient=orient,
|
| 392 |
+
convert_axes=convert_axes,
|
| 393 |
+
dtype=dtype,
|
| 394 |
+
)
|
| 395 |
+
assert np.isnan(result.iloc[0, 2])
|
| 396 |
+
|
| 397 |
+
obj_df = DataFrame([["1", "2"], ["4", "5", "6"]])
|
| 398 |
+
result = read_json(
|
| 399 |
+
StringIO(obj_df.to_json(orient=orient)),
|
| 400 |
+
orient=orient,
|
| 401 |
+
convert_axes=convert_axes,
|
| 402 |
+
dtype=dtype,
|
| 403 |
+
)
|
| 404 |
+
assert np.isnan(result.iloc[0, 2])
|
| 405 |
+
|
| 406 |
+
@pytest.mark.parametrize("dtype", [True, False])
|
| 407 |
+
def test_frame_read_json_dtype_missing_value(self, dtype):
|
| 408 |
+
# GH28501 Parse missing values using read_json with dtype=False
|
| 409 |
+
# to NaN instead of None
|
| 410 |
+
result = read_json(StringIO("[null]"), dtype=dtype)
|
| 411 |
+
expected = DataFrame([np.nan])
|
| 412 |
+
|
| 413 |
+
tm.assert_frame_equal(result, expected)
|
| 414 |
+
|
| 415 |
+
@pytest.mark.parametrize("inf", [np.inf, -np.inf])
|
| 416 |
+
@pytest.mark.parametrize("dtype", [True, False])
|
| 417 |
+
def test_frame_infinity(self, inf, dtype):
|
| 418 |
+
# infinities get mapped to nulls which get mapped to NaNs during
|
| 419 |
+
# deserialisation
|
| 420 |
+
df = DataFrame([[1, 2], [4, 5, 6]])
|
| 421 |
+
df.loc[0, 2] = inf
|
| 422 |
+
|
| 423 |
+
data = StringIO(df.to_json())
|
| 424 |
+
result = read_json(data, dtype=dtype)
|
| 425 |
+
assert np.isnan(result.iloc[0, 2])
|
| 426 |
+
|
| 427 |
+
@pytest.mark.skipif(not IS64, reason="not compliant on 32-bit, xref #15865")
|
| 428 |
+
@pytest.mark.parametrize(
|
| 429 |
+
"value,precision,expected_val",
|
| 430 |
+
[
|
| 431 |
+
(0.95, 1, 1.0),
|
| 432 |
+
(1.95, 1, 2.0),
|
| 433 |
+
(-1.95, 1, -2.0),
|
| 434 |
+
(0.995, 2, 1.0),
|
| 435 |
+
(0.9995, 3, 1.0),
|
| 436 |
+
(0.99999999999999944, 15, 1.0),
|
| 437 |
+
],
|
| 438 |
+
)
|
| 439 |
+
def test_frame_to_json_float_precision(self, value, precision, expected_val):
|
| 440 |
+
df = DataFrame([{"a_float": value}])
|
| 441 |
+
encoded = df.to_json(double_precision=precision)
|
| 442 |
+
assert encoded == f'{{"a_float":{{"0":{expected_val}}}}}'
|
| 443 |
+
|
| 444 |
+
def test_frame_to_json_except(self):
|
| 445 |
+
df = DataFrame([1, 2, 3])
|
| 446 |
+
msg = "Invalid value 'garbage' for option 'orient'"
|
| 447 |
+
with pytest.raises(ValueError, match=msg):
|
| 448 |
+
df.to_json(orient="garbage")
|
| 449 |
+
|
| 450 |
+
def test_frame_empty(self):
|
| 451 |
+
df = DataFrame(columns=["jim", "joe"])
|
| 452 |
+
assert not df._is_mixed_type
|
| 453 |
+
|
| 454 |
+
data = StringIO(df.to_json())
|
| 455 |
+
result = read_json(data, dtype=dict(df.dtypes))
|
| 456 |
+
tm.assert_frame_equal(result, df, check_index_type=False)
|
| 457 |
+
|
| 458 |
+
def test_frame_empty_to_json(self):
|
| 459 |
+
# GH 7445
|
| 460 |
+
df = DataFrame({"test": []}, index=[])
|
| 461 |
+
result = df.to_json(orient="columns")
|
| 462 |
+
expected = '{"test":{}}'
|
| 463 |
+
assert result == expected
|
| 464 |
+
|
| 465 |
+
def test_frame_empty_mixedtype(self):
|
| 466 |
+
# mixed type
|
| 467 |
+
df = DataFrame(columns=["jim", "joe"])
|
| 468 |
+
df["joe"] = df["joe"].astype("i8")
|
| 469 |
+
assert df._is_mixed_type
|
| 470 |
+
data = df.to_json()
|
| 471 |
+
tm.assert_frame_equal(
|
| 472 |
+
read_json(StringIO(data), dtype=dict(df.dtypes)),
|
| 473 |
+
df,
|
| 474 |
+
check_index_type=False,
|
| 475 |
+
)
|
| 476 |
+
|
| 477 |
+
def test_frame_mixedtype_orient(self): # GH10289
|
| 478 |
+
vals = [
|
| 479 |
+
[10, 1, "foo", 0.1, 0.01],
|
| 480 |
+
[20, 2, "bar", 0.2, 0.02],
|
| 481 |
+
[30, 3, "baz", 0.3, 0.03],
|
| 482 |
+
[40, 4, "qux", 0.4, 0.04],
|
| 483 |
+
]
|
| 484 |
+
|
| 485 |
+
df = DataFrame(
|
| 486 |
+
vals, index=list("abcd"), columns=["1st", "2nd", "3rd", "4th", "5th"]
|
| 487 |
+
)
|
| 488 |
+
|
| 489 |
+
assert df._is_mixed_type
|
| 490 |
+
right = df.copy()
|
| 491 |
+
|
| 492 |
+
for orient in ["split", "index", "columns"]:
|
| 493 |
+
inp = StringIO(df.to_json(orient=orient))
|
| 494 |
+
left = read_json(inp, orient=orient, convert_axes=False)
|
| 495 |
+
tm.assert_frame_equal(left, right)
|
| 496 |
+
|
| 497 |
+
right.index = RangeIndex(len(df))
|
| 498 |
+
inp = StringIO(df.to_json(orient="records"))
|
| 499 |
+
left = read_json(inp, orient="records", convert_axes=False)
|
| 500 |
+
tm.assert_frame_equal(left, right)
|
| 501 |
+
|
| 502 |
+
right.columns = RangeIndex(df.shape[1])
|
| 503 |
+
inp = StringIO(df.to_json(orient="values"))
|
| 504 |
+
left = read_json(inp, orient="values", convert_axes=False)
|
| 505 |
+
tm.assert_frame_equal(left, right)
|
| 506 |
+
|
| 507 |
+
def test_v12_compat(self, datapath):
|
| 508 |
+
dti = date_range("2000-01-03", "2000-01-07")
|
| 509 |
+
# freq doesn't roundtrip
|
| 510 |
+
dti = DatetimeIndex(np.asarray(dti), freq=None)
|
| 511 |
+
df = DataFrame(
|
| 512 |
+
[
|
| 513 |
+
[1.56808523, 0.65727391, 1.81021139, -0.17251653],
|
| 514 |
+
[-0.2550111, -0.08072427, -0.03202878, -0.17581665],
|
| 515 |
+
[1.51493992, 0.11805825, 1.629455, -1.31506612],
|
| 516 |
+
[-0.02765498, 0.44679743, 0.33192641, -0.27885413],
|
| 517 |
+
[0.05951614, -2.69652057, 1.28163262, 0.34703478],
|
| 518 |
+
],
|
| 519 |
+
columns=["A", "B", "C", "D"],
|
| 520 |
+
index=dti,
|
| 521 |
+
)
|
| 522 |
+
df["date"] = Timestamp("19920106 18:21:32.12").as_unit("ns")
|
| 523 |
+
df.iloc[3, df.columns.get_loc("date")] = Timestamp("20130101")
|
| 524 |
+
df["modified"] = df["date"]
|
| 525 |
+
df.iloc[1, df.columns.get_loc("modified")] = pd.NaT
|
| 526 |
+
|
| 527 |
+
dirpath = datapath("io", "json", "data")
|
| 528 |
+
v12_json = os.path.join(dirpath, "tsframe_v012.json")
|
| 529 |
+
df_unser = read_json(v12_json)
|
| 530 |
+
tm.assert_frame_equal(df, df_unser)
|
| 531 |
+
|
| 532 |
+
df_iso = df.drop(["modified"], axis=1)
|
| 533 |
+
v12_iso_json = os.path.join(dirpath, "tsframe_iso_v012.json")
|
| 534 |
+
df_unser_iso = read_json(v12_iso_json)
|
| 535 |
+
tm.assert_frame_equal(df_iso, df_unser_iso, check_column_type=False)
|
| 536 |
+
|
| 537 |
+
def test_blocks_compat_GH9037(self, using_infer_string):
|
| 538 |
+
index = date_range("20000101", periods=10, freq="h")
|
| 539 |
+
# freq doesn't round-trip
|
| 540 |
+
index = DatetimeIndex(list(index), freq=None)
|
| 541 |
+
|
| 542 |
+
df_mixed = DataFrame(
|
| 543 |
+
{
|
| 544 |
+
"float_1": [
|
| 545 |
+
-0.92077639,
|
| 546 |
+
0.77434435,
|
| 547 |
+
1.25234727,
|
| 548 |
+
0.61485564,
|
| 549 |
+
-0.60316077,
|
| 550 |
+
0.24653374,
|
| 551 |
+
0.28668979,
|
| 552 |
+
-2.51969012,
|
| 553 |
+
0.95748401,
|
| 554 |
+
-1.02970536,
|
| 555 |
+
],
|
| 556 |
+
"int_1": [
|
| 557 |
+
19680418,
|
| 558 |
+
75337055,
|
| 559 |
+
99973684,
|
| 560 |
+
65103179,
|
| 561 |
+
79373900,
|
| 562 |
+
40314334,
|
| 563 |
+
21290235,
|
| 564 |
+
4991321,
|
| 565 |
+
41903419,
|
| 566 |
+
16008365,
|
| 567 |
+
],
|
| 568 |
+
"str_1": [
|
| 569 |
+
"78c608f1",
|
| 570 |
+
"64a99743",
|
| 571 |
+
"13d2ff52",
|
| 572 |
+
"ca7f4af2",
|
| 573 |
+
"97236474",
|
| 574 |
+
"bde7e214",
|
| 575 |
+
"1a6bde47",
|
| 576 |
+
"b1190be5",
|
| 577 |
+
"7a669144",
|
| 578 |
+
"8d64d068",
|
| 579 |
+
],
|
| 580 |
+
"float_2": [
|
| 581 |
+
-0.0428278,
|
| 582 |
+
-1.80872357,
|
| 583 |
+
3.36042349,
|
| 584 |
+
-0.7573685,
|
| 585 |
+
-0.48217572,
|
| 586 |
+
0.86229683,
|
| 587 |
+
1.08935819,
|
| 588 |
+
0.93898739,
|
| 589 |
+
-0.03030452,
|
| 590 |
+
1.43366348,
|
| 591 |
+
],
|
| 592 |
+
"str_2": [
|
| 593 |
+
"14f04af9",
|
| 594 |
+
"d085da90",
|
| 595 |
+
"4bcfac83",
|
| 596 |
+
"81504caf",
|
| 597 |
+
"2ffef4a9",
|
| 598 |
+
"08e2f5c4",
|
| 599 |
+
"07e1af03",
|
| 600 |
+
"addbd4a7",
|
| 601 |
+
"1f6a09ba",
|
| 602 |
+
"4bfc4d87",
|
| 603 |
+
],
|
| 604 |
+
"int_2": [
|
| 605 |
+
86967717,
|
| 606 |
+
98098830,
|
| 607 |
+
51927505,
|
| 608 |
+
20372254,
|
| 609 |
+
12601730,
|
| 610 |
+
20884027,
|
| 611 |
+
34193846,
|
| 612 |
+
10561746,
|
| 613 |
+
24867120,
|
| 614 |
+
76131025,
|
| 615 |
+
],
|
| 616 |
+
},
|
| 617 |
+
index=index,
|
| 618 |
+
)
|
| 619 |
+
|
| 620 |
+
# JSON deserialisation always creates unicode strings
|
| 621 |
+
df_mixed.columns = df_mixed.columns.astype(
|
| 622 |
+
np.str_ if not using_infer_string else "string[pyarrow_numpy]"
|
| 623 |
+
)
|
| 624 |
+
data = StringIO(df_mixed.to_json(orient="split"))
|
| 625 |
+
df_roundtrip = read_json(data, orient="split")
|
| 626 |
+
tm.assert_frame_equal(
|
| 627 |
+
df_mixed,
|
| 628 |
+
df_roundtrip,
|
| 629 |
+
check_index_type=True,
|
| 630 |
+
check_column_type=True,
|
| 631 |
+
by_blocks=True,
|
| 632 |
+
check_exact=True,
|
| 633 |
+
)
|
| 634 |
+
|
| 635 |
+
def test_frame_nonprintable_bytes(self):
|
| 636 |
+
# GH14256: failing column caused segfaults, if it is not the last one
|
| 637 |
+
|
| 638 |
+
class BinaryThing:
|
| 639 |
+
def __init__(self, hexed) -> None:
|
| 640 |
+
self.hexed = hexed
|
| 641 |
+
self.binary = bytes.fromhex(hexed)
|
| 642 |
+
|
| 643 |
+
def __str__(self) -> str:
|
| 644 |
+
return self.hexed
|
| 645 |
+
|
| 646 |
+
hexed = "574b4454ba8c5eb4f98a8f45"
|
| 647 |
+
binthing = BinaryThing(hexed)
|
| 648 |
+
|
| 649 |
+
# verify the proper conversion of printable content
|
| 650 |
+
df_printable = DataFrame({"A": [binthing.hexed]})
|
| 651 |
+
assert df_printable.to_json() == f'{{"A":{{"0":"{hexed}"}}}}'
|
| 652 |
+
|
| 653 |
+
# check if non-printable content throws appropriate Exception
|
| 654 |
+
df_nonprintable = DataFrame({"A": [binthing]})
|
| 655 |
+
msg = "Unsupported UTF-8 sequence length when encoding string"
|
| 656 |
+
with pytest.raises(OverflowError, match=msg):
|
| 657 |
+
df_nonprintable.to_json()
|
| 658 |
+
|
| 659 |
+
# the same with multiple columns threw segfaults
|
| 660 |
+
df_mixed = DataFrame({"A": [binthing], "B": [1]}, columns=["A", "B"])
|
| 661 |
+
with pytest.raises(OverflowError, match=msg):
|
| 662 |
+
df_mixed.to_json()
|
| 663 |
+
|
| 664 |
+
# default_handler should resolve exceptions for non-string types
|
| 665 |
+
result = df_nonprintable.to_json(default_handler=str)
|
| 666 |
+
expected = f'{{"A":{{"0":"{hexed}"}}}}'
|
| 667 |
+
assert result == expected
|
| 668 |
+
assert (
|
| 669 |
+
df_mixed.to_json(default_handler=str)
|
| 670 |
+
== f'{{"A":{{"0":"{hexed}"}},"B":{{"0":1}}}}'
|
| 671 |
+
)
|
| 672 |
+
|
| 673 |
+
def test_label_overflow(self):
|
| 674 |
+
# GH14256: buffer length not checked when writing label
|
| 675 |
+
result = DataFrame({"bar" * 100000: [1], "foo": [1337]}).to_json()
|
| 676 |
+
expected = f'{{"{"bar" * 100000}":{{"0":1}},"foo":{{"0":1337}}}}'
|
| 677 |
+
assert result == expected
|
| 678 |
+
|
| 679 |
+
def test_series_non_unique_index(self):
|
| 680 |
+
s = Series(["a", "b"], index=[1, 1])
|
| 681 |
+
|
| 682 |
+
msg = "Series index must be unique for orient='index'"
|
| 683 |
+
with pytest.raises(ValueError, match=msg):
|
| 684 |
+
s.to_json(orient="index")
|
| 685 |
+
|
| 686 |
+
tm.assert_series_equal(
|
| 687 |
+
s,
|
| 688 |
+
read_json(
|
| 689 |
+
StringIO(s.to_json(orient="split")), orient="split", typ="series"
|
| 690 |
+
),
|
| 691 |
+
)
|
| 692 |
+
unserialized = read_json(
|
| 693 |
+
StringIO(s.to_json(orient="records")), orient="records", typ="series"
|
| 694 |
+
)
|
| 695 |
+
tm.assert_equal(s.values, unserialized.values)
|
| 696 |
+
|
| 697 |
+
def test_series_default_orient(self, string_series):
|
| 698 |
+
assert string_series.to_json() == string_series.to_json(orient="index")
|
| 699 |
+
|
| 700 |
+
def test_series_roundtrip_simple(self, orient, string_series, using_infer_string):
|
| 701 |
+
data = StringIO(string_series.to_json(orient=orient))
|
| 702 |
+
result = read_json(data, typ="series", orient=orient)
|
| 703 |
+
|
| 704 |
+
expected = string_series
|
| 705 |
+
if using_infer_string and orient in ("split", "index", "columns"):
|
| 706 |
+
# These schemas don't contain dtypes, so we infer string
|
| 707 |
+
expected.index = expected.index.astype("string[pyarrow_numpy]")
|
| 708 |
+
if orient in ("values", "records"):
|
| 709 |
+
expected = expected.reset_index(drop=True)
|
| 710 |
+
if orient != "split":
|
| 711 |
+
expected.name = None
|
| 712 |
+
|
| 713 |
+
tm.assert_series_equal(result, expected)
|
| 714 |
+
|
| 715 |
+
@pytest.mark.parametrize("dtype", [False, None])
|
| 716 |
+
def test_series_roundtrip_object(self, orient, dtype, object_series):
|
| 717 |
+
data = StringIO(object_series.to_json(orient=orient))
|
| 718 |
+
result = read_json(data, typ="series", orient=orient, dtype=dtype)
|
| 719 |
+
|
| 720 |
+
expected = object_series
|
| 721 |
+
if orient in ("values", "records"):
|
| 722 |
+
expected = expected.reset_index(drop=True)
|
| 723 |
+
if orient != "split":
|
| 724 |
+
expected.name = None
|
| 725 |
+
|
| 726 |
+
tm.assert_series_equal(result, expected)
|
| 727 |
+
|
| 728 |
+
def test_series_roundtrip_empty(self, orient):
|
| 729 |
+
empty_series = Series([], index=[], dtype=np.float64)
|
| 730 |
+
data = StringIO(empty_series.to_json(orient=orient))
|
| 731 |
+
result = read_json(data, typ="series", orient=orient)
|
| 732 |
+
|
| 733 |
+
expected = empty_series.reset_index(drop=True)
|
| 734 |
+
if orient in ("split"):
|
| 735 |
+
expected.index = expected.index.astype(np.float64)
|
| 736 |
+
|
| 737 |
+
tm.assert_series_equal(result, expected)
|
| 738 |
+
|
| 739 |
+
def test_series_roundtrip_timeseries(self, orient, datetime_series):
|
| 740 |
+
data = StringIO(datetime_series.to_json(orient=orient))
|
| 741 |
+
result = read_json(data, typ="series", orient=orient)
|
| 742 |
+
|
| 743 |
+
expected = datetime_series
|
| 744 |
+
if orient in ("values", "records"):
|
| 745 |
+
expected = expected.reset_index(drop=True)
|
| 746 |
+
if orient != "split":
|
| 747 |
+
expected.name = None
|
| 748 |
+
|
| 749 |
+
tm.assert_series_equal(result, expected)
|
| 750 |
+
|
| 751 |
+
@pytest.mark.parametrize("dtype", [np.float64, int])
|
| 752 |
+
def test_series_roundtrip_numeric(self, orient, dtype):
|
| 753 |
+
s = Series(range(6), index=["a", "b", "c", "d", "e", "f"])
|
| 754 |
+
data = StringIO(s.to_json(orient=orient))
|
| 755 |
+
result = read_json(data, typ="series", orient=orient)
|
| 756 |
+
|
| 757 |
+
expected = s.copy()
|
| 758 |
+
if orient in ("values", "records"):
|
| 759 |
+
expected = expected.reset_index(drop=True)
|
| 760 |
+
|
| 761 |
+
tm.assert_series_equal(result, expected)
|
| 762 |
+
|
| 763 |
+
def test_series_to_json_except(self):
|
| 764 |
+
s = Series([1, 2, 3])
|
| 765 |
+
msg = "Invalid value 'garbage' for option 'orient'"
|
| 766 |
+
with pytest.raises(ValueError, match=msg):
|
| 767 |
+
s.to_json(orient="garbage")
|
| 768 |
+
|
| 769 |
+
def test_series_from_json_precise_float(self):
|
| 770 |
+
s = Series([4.56, 4.56, 4.56])
|
| 771 |
+
result = read_json(StringIO(s.to_json()), typ="series", precise_float=True)
|
| 772 |
+
tm.assert_series_equal(result, s, check_index_type=False)
|
| 773 |
+
|
| 774 |
+
def test_series_with_dtype(self):
|
| 775 |
+
# GH 21986
|
| 776 |
+
s = Series([4.56, 4.56, 4.56])
|
| 777 |
+
result = read_json(StringIO(s.to_json()), typ="series", dtype=np.int64)
|
| 778 |
+
expected = Series([4] * 3)
|
| 779 |
+
tm.assert_series_equal(result, expected)
|
| 780 |
+
|
| 781 |
+
@pytest.mark.parametrize(
|
| 782 |
+
"dtype,expected",
|
| 783 |
+
[
|
| 784 |
+
(True, Series(["2000-01-01"], dtype="datetime64[ns]")),
|
| 785 |
+
(False, Series([946684800000])),
|
| 786 |
+
],
|
| 787 |
+
)
|
| 788 |
+
def test_series_with_dtype_datetime(self, dtype, expected):
|
| 789 |
+
s = Series(["2000-01-01"], dtype="datetime64[ns]")
|
| 790 |
+
data = StringIO(s.to_json())
|
| 791 |
+
result = read_json(data, typ="series", dtype=dtype)
|
| 792 |
+
tm.assert_series_equal(result, expected)
|
| 793 |
+
|
| 794 |
+
def test_frame_from_json_precise_float(self):
|
| 795 |
+
df = DataFrame([[4.56, 4.56, 4.56], [4.56, 4.56, 4.56]])
|
| 796 |
+
result = read_json(StringIO(df.to_json()), precise_float=True)
|
| 797 |
+
tm.assert_frame_equal(result, df)
|
| 798 |
+
|
| 799 |
+
def test_typ(self):
|
| 800 |
+
s = Series(range(6), index=["a", "b", "c", "d", "e", "f"], dtype="int64")
|
| 801 |
+
result = read_json(StringIO(s.to_json()), typ=None)
|
| 802 |
+
tm.assert_series_equal(result, s)
|
| 803 |
+
|
| 804 |
+
def test_reconstruction_index(self):
|
| 805 |
+
df = DataFrame([[1, 2, 3], [4, 5, 6]])
|
| 806 |
+
result = read_json(StringIO(df.to_json()))
|
| 807 |
+
tm.assert_frame_equal(result, df)
|
| 808 |
+
|
| 809 |
+
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}, index=["A", "B", "C"])
|
| 810 |
+
result = read_json(StringIO(df.to_json()))
|
| 811 |
+
tm.assert_frame_equal(result, df)
|
| 812 |
+
|
| 813 |
+
def test_path(self, float_frame, int_frame, datetime_frame):
|
| 814 |
+
with tm.ensure_clean("test.json") as path:
|
| 815 |
+
for df in [float_frame, int_frame, datetime_frame]:
|
| 816 |
+
df.to_json(path)
|
| 817 |
+
read_json(path)
|
| 818 |
+
|
| 819 |
+
def test_axis_dates(self, datetime_series, datetime_frame):
|
| 820 |
+
# frame
|
| 821 |
+
json = StringIO(datetime_frame.to_json())
|
| 822 |
+
result = read_json(json)
|
| 823 |
+
tm.assert_frame_equal(result, datetime_frame)
|
| 824 |
+
|
| 825 |
+
# series
|
| 826 |
+
json = StringIO(datetime_series.to_json())
|
| 827 |
+
result = read_json(json, typ="series")
|
| 828 |
+
tm.assert_series_equal(result, datetime_series, check_names=False)
|
| 829 |
+
assert result.name is None
|
| 830 |
+
|
| 831 |
+
def test_convert_dates(self, datetime_series, datetime_frame):
|
| 832 |
+
# frame
|
| 833 |
+
df = datetime_frame
|
| 834 |
+
df["date"] = Timestamp("20130101").as_unit("ns")
|
| 835 |
+
|
| 836 |
+
json = StringIO(df.to_json())
|
| 837 |
+
result = read_json(json)
|
| 838 |
+
tm.assert_frame_equal(result, df)
|
| 839 |
+
|
| 840 |
+
df["foo"] = 1.0
|
| 841 |
+
json = StringIO(df.to_json(date_unit="ns"))
|
| 842 |
+
|
| 843 |
+
result = read_json(json, convert_dates=False)
|
| 844 |
+
expected = df.copy()
|
| 845 |
+
expected["date"] = expected["date"].values.view("i8")
|
| 846 |
+
expected["foo"] = expected["foo"].astype("int64")
|
| 847 |
+
tm.assert_frame_equal(result, expected)
|
| 848 |
+
|
| 849 |
+
# series
|
| 850 |
+
ts = Series(Timestamp("20130101").as_unit("ns"), index=datetime_series.index)
|
| 851 |
+
json = StringIO(ts.to_json())
|
| 852 |
+
result = read_json(json, typ="series")
|
| 853 |
+
tm.assert_series_equal(result, ts)
|
| 854 |
+
|
| 855 |
+
@pytest.mark.parametrize("date_format", ["epoch", "iso"])
|
| 856 |
+
@pytest.mark.parametrize("as_object", [True, False])
|
| 857 |
+
@pytest.mark.parametrize("date_typ", [datetime.date, datetime.datetime, Timestamp])
|
| 858 |
+
def test_date_index_and_values(self, date_format, as_object, date_typ):
|
| 859 |
+
data = [date_typ(year=2020, month=1, day=1), pd.NaT]
|
| 860 |
+
if as_object:
|
| 861 |
+
data.append("a")
|
| 862 |
+
|
| 863 |
+
ser = Series(data, index=data)
|
| 864 |
+
result = ser.to_json(date_format=date_format)
|
| 865 |
+
|
| 866 |
+
if date_format == "epoch":
|
| 867 |
+
expected = '{"1577836800000":1577836800000,"null":null}'
|
| 868 |
+
else:
|
| 869 |
+
expected = (
|
| 870 |
+
'{"2020-01-01T00:00:00.000":"2020-01-01T00:00:00.000","null":null}'
|
| 871 |
+
)
|
| 872 |
+
|
| 873 |
+
if as_object:
|
| 874 |
+
expected = expected.replace("}", ',"a":"a"}')
|
| 875 |
+
|
| 876 |
+
assert result == expected
|
| 877 |
+
|
| 878 |
+
@pytest.mark.parametrize(
|
| 879 |
+
"infer_word",
|
| 880 |
+
[
|
| 881 |
+
"trade_time",
|
| 882 |
+
"date",
|
| 883 |
+
"datetime",
|
| 884 |
+
"sold_at",
|
| 885 |
+
"modified",
|
| 886 |
+
"timestamp",
|
| 887 |
+
"timestamps",
|
| 888 |
+
],
|
| 889 |
+
)
|
| 890 |
+
def test_convert_dates_infer(self, infer_word):
|
| 891 |
+
# GH10747
|
| 892 |
+
|
| 893 |
+
data = [{"id": 1, infer_word: 1036713600000}, {"id": 2}]
|
| 894 |
+
expected = DataFrame(
|
| 895 |
+
[[1, Timestamp("2002-11-08")], [2, pd.NaT]], columns=["id", infer_word]
|
| 896 |
+
)
|
| 897 |
+
|
| 898 |
+
result = read_json(StringIO(ujson_dumps(data)))[["id", infer_word]]
|
| 899 |
+
tm.assert_frame_equal(result, expected)
|
| 900 |
+
|
| 901 |
+
@pytest.mark.parametrize(
|
| 902 |
+
"date,date_unit",
|
| 903 |
+
[
|
| 904 |
+
("20130101 20:43:42.123", None),
|
| 905 |
+
("20130101 20:43:42", "s"),
|
| 906 |
+
("20130101 20:43:42.123", "ms"),
|
| 907 |
+
("20130101 20:43:42.123456", "us"),
|
| 908 |
+
("20130101 20:43:42.123456789", "ns"),
|
| 909 |
+
],
|
| 910 |
+
)
|
| 911 |
+
def test_date_format_frame(self, date, date_unit, datetime_frame):
|
| 912 |
+
df = datetime_frame
|
| 913 |
+
|
| 914 |
+
df["date"] = Timestamp(date).as_unit("ns")
|
| 915 |
+
df.iloc[1, df.columns.get_loc("date")] = pd.NaT
|
| 916 |
+
df.iloc[5, df.columns.get_loc("date")] = pd.NaT
|
| 917 |
+
if date_unit:
|
| 918 |
+
json = df.to_json(date_format="iso", date_unit=date_unit)
|
| 919 |
+
else:
|
| 920 |
+
json = df.to_json(date_format="iso")
|
| 921 |
+
|
| 922 |
+
result = read_json(StringIO(json))
|
| 923 |
+
expected = df.copy()
|
| 924 |
+
tm.assert_frame_equal(result, expected)
|
| 925 |
+
|
| 926 |
+
def test_date_format_frame_raises(self, datetime_frame):
|
| 927 |
+
df = datetime_frame
|
| 928 |
+
msg = "Invalid value 'foo' for option 'date_unit'"
|
| 929 |
+
with pytest.raises(ValueError, match=msg):
|
| 930 |
+
df.to_json(date_format="iso", date_unit="foo")
|
| 931 |
+
|
| 932 |
+
@pytest.mark.parametrize(
|
| 933 |
+
"date,date_unit",
|
| 934 |
+
[
|
| 935 |
+
("20130101 20:43:42.123", None),
|
| 936 |
+
("20130101 20:43:42", "s"),
|
| 937 |
+
("20130101 20:43:42.123", "ms"),
|
| 938 |
+
("20130101 20:43:42.123456", "us"),
|
| 939 |
+
("20130101 20:43:42.123456789", "ns"),
|
| 940 |
+
],
|
| 941 |
+
)
|
| 942 |
+
def test_date_format_series(self, date, date_unit, datetime_series):
|
| 943 |
+
ts = Series(Timestamp(date).as_unit("ns"), index=datetime_series.index)
|
| 944 |
+
ts.iloc[1] = pd.NaT
|
| 945 |
+
ts.iloc[5] = pd.NaT
|
| 946 |
+
if date_unit:
|
| 947 |
+
json = ts.to_json(date_format="iso", date_unit=date_unit)
|
| 948 |
+
else:
|
| 949 |
+
json = ts.to_json(date_format="iso")
|
| 950 |
+
|
| 951 |
+
result = read_json(StringIO(json), typ="series")
|
| 952 |
+
expected = ts.copy()
|
| 953 |
+
tm.assert_series_equal(result, expected)
|
| 954 |
+
|
| 955 |
+
def test_date_format_series_raises(self, datetime_series):
|
| 956 |
+
ts = Series(Timestamp("20130101 20:43:42.123"), index=datetime_series.index)
|
| 957 |
+
msg = "Invalid value 'foo' for option 'date_unit'"
|
| 958 |
+
with pytest.raises(ValueError, match=msg):
|
| 959 |
+
ts.to_json(date_format="iso", date_unit="foo")
|
| 960 |
+
|
| 961 |
+
@pytest.mark.parametrize("unit", ["s", "ms", "us", "ns"])
|
| 962 |
+
def test_date_unit(self, unit, datetime_frame):
|
| 963 |
+
df = datetime_frame
|
| 964 |
+
df["date"] = Timestamp("20130101 20:43:42").as_unit("ns")
|
| 965 |
+
dl = df.columns.get_loc("date")
|
| 966 |
+
df.iloc[1, dl] = Timestamp("19710101 20:43:42")
|
| 967 |
+
df.iloc[2, dl] = Timestamp("21460101 20:43:42")
|
| 968 |
+
df.iloc[4, dl] = pd.NaT
|
| 969 |
+
|
| 970 |
+
json = df.to_json(date_format="epoch", date_unit=unit)
|
| 971 |
+
|
| 972 |
+
# force date unit
|
| 973 |
+
result = read_json(StringIO(json), date_unit=unit)
|
| 974 |
+
tm.assert_frame_equal(result, df)
|
| 975 |
+
|
| 976 |
+
# detect date unit
|
| 977 |
+
result = read_json(StringIO(json), date_unit=None)
|
| 978 |
+
tm.assert_frame_equal(result, df)
|
| 979 |
+
|
| 980 |
+
@pytest.mark.parametrize("unit", ["s", "ms", "us"])
|
| 981 |
+
def test_iso_non_nano_datetimes(self, unit):
|
| 982 |
+
# Test that numpy datetimes
|
| 983 |
+
# in an Index or a column with non-nano resolution can be serialized
|
| 984 |
+
# correctly
|
| 985 |
+
# GH53686
|
| 986 |
+
index = DatetimeIndex(
|
| 987 |
+
[np.datetime64("2023-01-01T11:22:33.123456", unit)],
|
| 988 |
+
dtype=f"datetime64[{unit}]",
|
| 989 |
+
)
|
| 990 |
+
df = DataFrame(
|
| 991 |
+
{
|
| 992 |
+
"date": Series(
|
| 993 |
+
[np.datetime64("2022-01-01T11:22:33.123456", unit)],
|
| 994 |
+
dtype=f"datetime64[{unit}]",
|
| 995 |
+
index=index,
|
| 996 |
+
),
|
| 997 |
+
"date_obj": Series(
|
| 998 |
+
[np.datetime64("2023-01-01T11:22:33.123456", unit)],
|
| 999 |
+
dtype=object,
|
| 1000 |
+
index=index,
|
| 1001 |
+
),
|
| 1002 |
+
},
|
| 1003 |
+
)
|
| 1004 |
+
|
| 1005 |
+
buf = StringIO()
|
| 1006 |
+
df.to_json(buf, date_format="iso", date_unit=unit)
|
| 1007 |
+
buf.seek(0)
|
| 1008 |
+
|
| 1009 |
+
# read_json always reads datetimes in nanosecond resolution
|
| 1010 |
+
# TODO: check_dtype/check_index_type should be removable
|
| 1011 |
+
# once read_json gets non-nano support
|
| 1012 |
+
tm.assert_frame_equal(
|
| 1013 |
+
read_json(buf, convert_dates=["date", "date_obj"]),
|
| 1014 |
+
df,
|
| 1015 |
+
check_index_type=False,
|
| 1016 |
+
check_dtype=False,
|
| 1017 |
+
)
|
| 1018 |
+
|
| 1019 |
+
def test_weird_nested_json(self):
|
| 1020 |
+
# this used to core dump the parser
|
| 1021 |
+
s = r"""{
|
| 1022 |
+
"status": "success",
|
| 1023 |
+
"data": {
|
| 1024 |
+
"posts": [
|
| 1025 |
+
{
|
| 1026 |
+
"id": 1,
|
| 1027 |
+
"title": "A blog post",
|
| 1028 |
+
"body": "Some useful content"
|
| 1029 |
+
},
|
| 1030 |
+
{
|
| 1031 |
+
"id": 2,
|
| 1032 |
+
"title": "Another blog post",
|
| 1033 |
+
"body": "More content"
|
| 1034 |
+
}
|
| 1035 |
+
]
|
| 1036 |
+
}
|
| 1037 |
+
}"""
|
| 1038 |
+
read_json(StringIO(s))
|
| 1039 |
+
|
| 1040 |
+
def test_doc_example(self):
|
| 1041 |
+
dfj2 = DataFrame(
|
| 1042 |
+
np.random.default_rng(2).standard_normal((5, 2)), columns=list("AB")
|
| 1043 |
+
)
|
| 1044 |
+
dfj2["date"] = Timestamp("20130101")
|
| 1045 |
+
dfj2["ints"] = range(5)
|
| 1046 |
+
dfj2["bools"] = True
|
| 1047 |
+
dfj2.index = date_range("20130101", periods=5)
|
| 1048 |
+
|
| 1049 |
+
json = StringIO(dfj2.to_json())
|
| 1050 |
+
result = read_json(json, dtype={"ints": np.int64, "bools": np.bool_})
|
| 1051 |
+
tm.assert_frame_equal(result, result)
|
| 1052 |
+
|
| 1053 |
+
def test_round_trip_exception(self, datapath):
|
| 1054 |
+
# GH 3867
|
| 1055 |
+
path = datapath("io", "json", "data", "teams.csv")
|
| 1056 |
+
df = pd.read_csv(path)
|
| 1057 |
+
s = df.to_json()
|
| 1058 |
+
|
| 1059 |
+
result = read_json(StringIO(s))
|
| 1060 |
+
res = result.reindex(index=df.index, columns=df.columns)
|
| 1061 |
+
msg = "The 'downcast' keyword in fillna is deprecated"
|
| 1062 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
| 1063 |
+
res = res.fillna(np.nan, downcast=False)
|
| 1064 |
+
tm.assert_frame_equal(res, df)
|
| 1065 |
+
|
| 1066 |
+
@pytest.mark.network
|
| 1067 |
+
@pytest.mark.single_cpu
|
| 1068 |
+
@pytest.mark.parametrize(
|
| 1069 |
+
"field,dtype",
|
| 1070 |
+
[
|
| 1071 |
+
["created_at", pd.DatetimeTZDtype(tz="UTC")],
|
| 1072 |
+
["closed_at", "datetime64[ns]"],
|
| 1073 |
+
["updated_at", pd.DatetimeTZDtype(tz="UTC")],
|
| 1074 |
+
],
|
| 1075 |
+
)
|
| 1076 |
+
def test_url(self, field, dtype, httpserver):
|
| 1077 |
+
data = '{"created_at": ["2023-06-23T18:21:36Z"], "closed_at": ["2023-06-23T18:21:36"], "updated_at": ["2023-06-23T18:21:36Z"]}\n' # noqa: E501
|
| 1078 |
+
httpserver.serve_content(content=data)
|
| 1079 |
+
result = read_json(httpserver.url, convert_dates=True)
|
| 1080 |
+
assert result[field].dtype == dtype
|
| 1081 |
+
|
| 1082 |
+
def test_timedelta(self):
|
| 1083 |
+
converter = lambda x: pd.to_timedelta(x, unit="ms")
|
| 1084 |
+
|
| 1085 |
+
ser = Series([timedelta(23), timedelta(seconds=5)])
|
| 1086 |
+
assert ser.dtype == "timedelta64[ns]"
|
| 1087 |
+
|
| 1088 |
+
result = read_json(StringIO(ser.to_json()), typ="series").apply(converter)
|
| 1089 |
+
tm.assert_series_equal(result, ser)
|
| 1090 |
+
|
| 1091 |
+
ser = Series([timedelta(23), timedelta(seconds=5)], index=Index([0, 1]))
|
| 1092 |
+
assert ser.dtype == "timedelta64[ns]"
|
| 1093 |
+
result = read_json(StringIO(ser.to_json()), typ="series").apply(converter)
|
| 1094 |
+
tm.assert_series_equal(result, ser)
|
| 1095 |
+
|
| 1096 |
+
frame = DataFrame([timedelta(23), timedelta(seconds=5)])
|
| 1097 |
+
assert frame[0].dtype == "timedelta64[ns]"
|
| 1098 |
+
tm.assert_frame_equal(
|
| 1099 |
+
frame, read_json(StringIO(frame.to_json())).apply(converter)
|
| 1100 |
+
)
|
| 1101 |
+
|
| 1102 |
+
def test_timedelta2(self):
|
| 1103 |
+
frame = DataFrame(
|
| 1104 |
+
{
|
| 1105 |
+
"a": [timedelta(days=23), timedelta(seconds=5)],
|
| 1106 |
+
"b": [1, 2],
|
| 1107 |
+
"c": date_range(start="20130101", periods=2),
|
| 1108 |
+
}
|
| 1109 |
+
)
|
| 1110 |
+
data = StringIO(frame.to_json(date_unit="ns"))
|
| 1111 |
+
result = read_json(data)
|
| 1112 |
+
result["a"] = pd.to_timedelta(result.a, unit="ns")
|
| 1113 |
+
result["c"] = pd.to_datetime(result.c)
|
| 1114 |
+
tm.assert_frame_equal(frame, result)
|
| 1115 |
+
|
| 1116 |
+
def test_mixed_timedelta_datetime(self):
|
| 1117 |
+
td = timedelta(23)
|
| 1118 |
+
ts = Timestamp("20130101")
|
| 1119 |
+
frame = DataFrame({"a": [td, ts]}, dtype=object)
|
| 1120 |
+
|
| 1121 |
+
expected = DataFrame(
|
| 1122 |
+
{"a": [pd.Timedelta(td).as_unit("ns")._value, ts.as_unit("ns")._value]}
|
| 1123 |
+
)
|
| 1124 |
+
data = StringIO(frame.to_json(date_unit="ns"))
|
| 1125 |
+
result = read_json(data, dtype={"a": "int64"})
|
| 1126 |
+
tm.assert_frame_equal(result, expected, check_index_type=False)
|
| 1127 |
+
|
| 1128 |
+
@pytest.mark.parametrize("as_object", [True, False])
|
| 1129 |
+
@pytest.mark.parametrize("date_format", ["iso", "epoch"])
|
| 1130 |
+
@pytest.mark.parametrize("timedelta_typ", [pd.Timedelta, timedelta])
|
| 1131 |
+
def test_timedelta_to_json(self, as_object, date_format, timedelta_typ):
|
| 1132 |
+
# GH28156: to_json not correctly formatting Timedelta
|
| 1133 |
+
data = [timedelta_typ(days=1), timedelta_typ(days=2), pd.NaT]
|
| 1134 |
+
if as_object:
|
| 1135 |
+
data.append("a")
|
| 1136 |
+
|
| 1137 |
+
ser = Series(data, index=data)
|
| 1138 |
+
if date_format == "iso":
|
| 1139 |
+
expected = (
|
| 1140 |
+
'{"P1DT0H0M0S":"P1DT0H0M0S","P2DT0H0M0S":"P2DT0H0M0S","null":null}'
|
| 1141 |
+
)
|
| 1142 |
+
else:
|
| 1143 |
+
expected = '{"86400000":86400000,"172800000":172800000,"null":null}'
|
| 1144 |
+
|
| 1145 |
+
if as_object:
|
| 1146 |
+
expected = expected.replace("}", ',"a":"a"}')
|
| 1147 |
+
|
| 1148 |
+
result = ser.to_json(date_format=date_format)
|
| 1149 |
+
assert result == expected
|
| 1150 |
+
|
| 1151 |
+
@pytest.mark.parametrize("as_object", [True, False])
|
| 1152 |
+
@pytest.mark.parametrize("timedelta_typ", [pd.Timedelta, timedelta])
|
| 1153 |
+
def test_timedelta_to_json_fractional_precision(self, as_object, timedelta_typ):
|
| 1154 |
+
data = [timedelta_typ(milliseconds=42)]
|
| 1155 |
+
ser = Series(data, index=data)
|
| 1156 |
+
if as_object:
|
| 1157 |
+
ser = ser.astype(object)
|
| 1158 |
+
|
| 1159 |
+
result = ser.to_json()
|
| 1160 |
+
expected = '{"42":42}'
|
| 1161 |
+
assert result == expected
|
| 1162 |
+
|
| 1163 |
+
def test_default_handler(self):
|
| 1164 |
+
value = object()
|
| 1165 |
+
frame = DataFrame({"a": [7, value]})
|
| 1166 |
+
expected = DataFrame({"a": [7, str(value)]})
|
| 1167 |
+
result = read_json(StringIO(frame.to_json(default_handler=str)))
|
| 1168 |
+
tm.assert_frame_equal(expected, result, check_index_type=False)
|
| 1169 |
+
|
| 1170 |
+
def test_default_handler_indirect(self):
|
| 1171 |
+
def default(obj):
|
| 1172 |
+
if isinstance(obj, complex):
|
| 1173 |
+
return [("mathjs", "Complex"), ("re", obj.real), ("im", obj.imag)]
|
| 1174 |
+
return str(obj)
|
| 1175 |
+
|
| 1176 |
+
df_list = [
|
| 1177 |
+
9,
|
| 1178 |
+
DataFrame(
|
| 1179 |
+
{"a": [1, "STR", complex(4, -5)], "b": [float("nan"), None, "N/A"]},
|
| 1180 |
+
columns=["a", "b"],
|
| 1181 |
+
),
|
| 1182 |
+
]
|
| 1183 |
+
expected = (
|
| 1184 |
+
'[9,[[1,null],["STR",null],[[["mathjs","Complex"],'
|
| 1185 |
+
'["re",4.0],["im",-5.0]],"N\\/A"]]]'
|
| 1186 |
+
)
|
| 1187 |
+
assert (
|
| 1188 |
+
ujson_dumps(df_list, default_handler=default, orient="values") == expected
|
| 1189 |
+
)
|
| 1190 |
+
|
| 1191 |
+
def test_default_handler_numpy_unsupported_dtype(self):
|
| 1192 |
+
# GH12554 to_json raises 'Unhandled numpy dtype 15'
|
| 1193 |
+
df = DataFrame(
|
| 1194 |
+
{"a": [1, 2.3, complex(4, -5)], "b": [float("nan"), None, complex(1.2, 0)]},
|
| 1195 |
+
columns=["a", "b"],
|
| 1196 |
+
)
|
| 1197 |
+
expected = (
|
| 1198 |
+
'[["(1+0j)","(nan+0j)"],'
|
| 1199 |
+
'["(2.3+0j)","(nan+0j)"],'
|
| 1200 |
+
'["(4-5j)","(1.2+0j)"]]'
|
| 1201 |
+
)
|
| 1202 |
+
assert df.to_json(default_handler=str, orient="values") == expected
|
| 1203 |
+
|
| 1204 |
+
def test_default_handler_raises(self):
|
| 1205 |
+
msg = "raisin"
|
| 1206 |
+
|
| 1207 |
+
def my_handler_raises(obj):
|
| 1208 |
+
raise TypeError(msg)
|
| 1209 |
+
|
| 1210 |
+
with pytest.raises(TypeError, match=msg):
|
| 1211 |
+
DataFrame({"a": [1, 2, object()]}).to_json(
|
| 1212 |
+
default_handler=my_handler_raises
|
| 1213 |
+
)
|
| 1214 |
+
with pytest.raises(TypeError, match=msg):
|
| 1215 |
+
DataFrame({"a": [1, 2, complex(4, -5)]}).to_json(
|
| 1216 |
+
default_handler=my_handler_raises
|
| 1217 |
+
)
|
| 1218 |
+
|
| 1219 |
+
def test_categorical(self):
|
| 1220 |
+
# GH4377 df.to_json segfaults with non-ndarray blocks
|
| 1221 |
+
df = DataFrame({"A": ["a", "b", "c", "a", "b", "b", "a"]})
|
| 1222 |
+
df["B"] = df["A"]
|
| 1223 |
+
expected = df.to_json()
|
| 1224 |
+
|
| 1225 |
+
df["B"] = df["A"].astype("category")
|
| 1226 |
+
assert expected == df.to_json()
|
| 1227 |
+
|
| 1228 |
+
s = df["A"]
|
| 1229 |
+
sc = df["B"]
|
| 1230 |
+
assert s.to_json() == sc.to_json()
|
| 1231 |
+
|
| 1232 |
+
def test_datetime_tz(self):
|
| 1233 |
+
# GH4377 df.to_json segfaults with non-ndarray blocks
|
| 1234 |
+
tz_range = date_range("20130101", periods=3, tz="US/Eastern")
|
| 1235 |
+
tz_naive = tz_range.tz_convert("utc").tz_localize(None)
|
| 1236 |
+
|
| 1237 |
+
df = DataFrame({"A": tz_range, "B": date_range("20130101", periods=3)})
|
| 1238 |
+
|
| 1239 |
+
df_naive = df.copy()
|
| 1240 |
+
df_naive["A"] = tz_naive
|
| 1241 |
+
expected = df_naive.to_json()
|
| 1242 |
+
assert expected == df.to_json()
|
| 1243 |
+
|
| 1244 |
+
stz = Series(tz_range)
|
| 1245 |
+
s_naive = Series(tz_naive)
|
| 1246 |
+
assert stz.to_json() == s_naive.to_json()
|
| 1247 |
+
|
| 1248 |
+
def test_sparse(self):
|
| 1249 |
+
# GH4377 df.to_json segfaults with non-ndarray blocks
|
| 1250 |
+
df = DataFrame(np.random.default_rng(2).standard_normal((10, 4)))
|
| 1251 |
+
df.loc[:8] = np.nan
|
| 1252 |
+
|
| 1253 |
+
sdf = df.astype("Sparse")
|
| 1254 |
+
expected = df.to_json()
|
| 1255 |
+
assert expected == sdf.to_json()
|
| 1256 |
+
|
| 1257 |
+
s = Series(np.random.default_rng(2).standard_normal(10))
|
| 1258 |
+
s.loc[:8] = np.nan
|
| 1259 |
+
ss = s.astype("Sparse")
|
| 1260 |
+
|
| 1261 |
+
expected = s.to_json()
|
| 1262 |
+
assert expected == ss.to_json()
|
| 1263 |
+
|
| 1264 |
+
@pytest.mark.parametrize(
|
| 1265 |
+
"ts",
|
| 1266 |
+
[
|
| 1267 |
+
Timestamp("2013-01-10 05:00:00Z"),
|
| 1268 |
+
Timestamp("2013-01-10 00:00:00", tz="US/Eastern"),
|
| 1269 |
+
Timestamp("2013-01-10 00:00:00-0500"),
|
| 1270 |
+
],
|
| 1271 |
+
)
|
| 1272 |
+
def test_tz_is_utc(self, ts):
|
| 1273 |
+
exp = '"2013-01-10T05:00:00.000Z"'
|
| 1274 |
+
|
| 1275 |
+
assert ujson_dumps(ts, iso_dates=True) == exp
|
| 1276 |
+
dt = ts.to_pydatetime()
|
| 1277 |
+
assert ujson_dumps(dt, iso_dates=True) == exp
|
| 1278 |
+
|
| 1279 |
+
def test_tz_is_naive(self):
|
| 1280 |
+
ts = Timestamp("2013-01-10 05:00:00")
|
| 1281 |
+
exp = '"2013-01-10T05:00:00.000"'
|
| 1282 |
+
|
| 1283 |
+
assert ujson_dumps(ts, iso_dates=True) == exp
|
| 1284 |
+
dt = ts.to_pydatetime()
|
| 1285 |
+
assert ujson_dumps(dt, iso_dates=True) == exp
|
| 1286 |
+
|
| 1287 |
+
@pytest.mark.parametrize(
|
| 1288 |
+
"tz_range",
|
| 1289 |
+
[
|
| 1290 |
+
date_range("2013-01-01 05:00:00Z", periods=2),
|
| 1291 |
+
date_range("2013-01-01 00:00:00", periods=2, tz="US/Eastern"),
|
| 1292 |
+
date_range("2013-01-01 00:00:00-0500", periods=2),
|
| 1293 |
+
],
|
| 1294 |
+
)
|
| 1295 |
+
def test_tz_range_is_utc(self, tz_range):
|
| 1296 |
+
exp = '["2013-01-01T05:00:00.000Z","2013-01-02T05:00:00.000Z"]'
|
| 1297 |
+
dfexp = (
|
| 1298 |
+
'{"DT":{'
|
| 1299 |
+
'"0":"2013-01-01T05:00:00.000Z",'
|
| 1300 |
+
'"1":"2013-01-02T05:00:00.000Z"}}'
|
| 1301 |
+
)
|
| 1302 |
+
|
| 1303 |
+
assert ujson_dumps(tz_range, iso_dates=True) == exp
|
| 1304 |
+
dti = DatetimeIndex(tz_range)
|
| 1305 |
+
# Ensure datetimes in object array are serialized correctly
|
| 1306 |
+
# in addition to the normal DTI case
|
| 1307 |
+
assert ujson_dumps(dti, iso_dates=True) == exp
|
| 1308 |
+
assert ujson_dumps(dti.astype(object), iso_dates=True) == exp
|
| 1309 |
+
df = DataFrame({"DT": dti})
|
| 1310 |
+
result = ujson_dumps(df, iso_dates=True)
|
| 1311 |
+
assert result == dfexp
|
| 1312 |
+
assert ujson_dumps(df.astype({"DT": object}), iso_dates=True)
|
| 1313 |
+
|
| 1314 |
+
def test_tz_range_is_naive(self):
|
| 1315 |
+
dti = date_range("2013-01-01 05:00:00", periods=2)
|
| 1316 |
+
|
| 1317 |
+
exp = '["2013-01-01T05:00:00.000","2013-01-02T05:00:00.000"]'
|
| 1318 |
+
dfexp = '{"DT":{"0":"2013-01-01T05:00:00.000","1":"2013-01-02T05:00:00.000"}}'
|
| 1319 |
+
|
| 1320 |
+
# Ensure datetimes in object array are serialized correctly
|
| 1321 |
+
# in addition to the normal DTI case
|
| 1322 |
+
assert ujson_dumps(dti, iso_dates=True) == exp
|
| 1323 |
+
assert ujson_dumps(dti.astype(object), iso_dates=True) == exp
|
| 1324 |
+
df = DataFrame({"DT": dti})
|
| 1325 |
+
result = ujson_dumps(df, iso_dates=True)
|
| 1326 |
+
assert result == dfexp
|
| 1327 |
+
assert ujson_dumps(df.astype({"DT": object}), iso_dates=True)
|
| 1328 |
+
|
| 1329 |
+
def test_read_inline_jsonl(self):
|
| 1330 |
+
# GH9180
|
| 1331 |
+
|
| 1332 |
+
result = read_json(StringIO('{"a": 1, "b": 2}\n{"b":2, "a" :1}\n'), lines=True)
|
| 1333 |
+
expected = DataFrame([[1, 2], [1, 2]], columns=["a", "b"])
|
| 1334 |
+
tm.assert_frame_equal(result, expected)
|
| 1335 |
+
|
| 1336 |
+
@pytest.mark.single_cpu
|
| 1337 |
+
@td.skip_if_not_us_locale
|
| 1338 |
+
def test_read_s3_jsonl(self, s3_public_bucket_with_data, s3so):
|
| 1339 |
+
# GH17200
|
| 1340 |
+
|
| 1341 |
+
result = read_json(
|
| 1342 |
+
f"s3n://{s3_public_bucket_with_data.name}/items.jsonl",
|
| 1343 |
+
lines=True,
|
| 1344 |
+
storage_options=s3so,
|
| 1345 |
+
)
|
| 1346 |
+
expected = DataFrame([[1, 2], [1, 2]], columns=["a", "b"])
|
| 1347 |
+
tm.assert_frame_equal(result, expected)
|
| 1348 |
+
|
| 1349 |
+
def test_read_local_jsonl(self):
|
| 1350 |
+
# GH17200
|
| 1351 |
+
with tm.ensure_clean("tmp_items.json") as path:
|
| 1352 |
+
with open(path, "w", encoding="utf-8") as infile:
|
| 1353 |
+
infile.write('{"a": 1, "b": 2}\n{"b":2, "a" :1}\n')
|
| 1354 |
+
result = read_json(path, lines=True)
|
| 1355 |
+
expected = DataFrame([[1, 2], [1, 2]], columns=["a", "b"])
|
| 1356 |
+
tm.assert_frame_equal(result, expected)
|
| 1357 |
+
|
| 1358 |
+
def test_read_jsonl_unicode_chars(self):
|
| 1359 |
+
# GH15132: non-ascii unicode characters
|
| 1360 |
+
# \u201d == RIGHT DOUBLE QUOTATION MARK
|
| 1361 |
+
|
| 1362 |
+
# simulate file handle
|
| 1363 |
+
json = '{"a": "foo”", "b": "bar"}\n{"a": "foo", "b": "bar"}\n'
|
| 1364 |
+
json = StringIO(json)
|
| 1365 |
+
result = read_json(json, lines=True)
|
| 1366 |
+
expected = DataFrame([["foo\u201d", "bar"], ["foo", "bar"]], columns=["a", "b"])
|
| 1367 |
+
tm.assert_frame_equal(result, expected)
|
| 1368 |
+
|
| 1369 |
+
# simulate string
|
| 1370 |
+
json = StringIO('{"a": "foo”", "b": "bar"}\n{"a": "foo", "b": "bar"}\n')
|
| 1371 |
+
result = read_json(json, lines=True)
|
| 1372 |
+
expected = DataFrame([["foo\u201d", "bar"], ["foo", "bar"]], columns=["a", "b"])
|
| 1373 |
+
tm.assert_frame_equal(result, expected)
|
| 1374 |
+
|
| 1375 |
+
@pytest.mark.parametrize("bigNum", [sys.maxsize + 1, -(sys.maxsize + 2)])
|
| 1376 |
+
def test_to_json_large_numbers(self, bigNum):
|
| 1377 |
+
# GH34473
|
| 1378 |
+
series = Series(bigNum, dtype=object, index=["articleId"])
|
| 1379 |
+
json = series.to_json()
|
| 1380 |
+
expected = '{"articleId":' + str(bigNum) + "}"
|
| 1381 |
+
assert json == expected
|
| 1382 |
+
|
| 1383 |
+
df = DataFrame(bigNum, dtype=object, index=["articleId"], columns=[0])
|
| 1384 |
+
json = df.to_json()
|
| 1385 |
+
expected = '{"0":{"articleId":' + str(bigNum) + "}}"
|
| 1386 |
+
assert json == expected
|
| 1387 |
+
|
| 1388 |
+
@pytest.mark.parametrize("bigNum", [-(2**63) - 1, 2**64])
|
| 1389 |
+
def test_read_json_large_numbers(self, bigNum):
|
| 1390 |
+
# GH20599, 26068
|
| 1391 |
+
json = StringIO('{"articleId":' + str(bigNum) + "}")
|
| 1392 |
+
msg = r"Value is too small|Value is too big"
|
| 1393 |
+
with pytest.raises(ValueError, match=msg):
|
| 1394 |
+
read_json(json)
|
| 1395 |
+
|
| 1396 |
+
json = StringIO('{"0":{"articleId":' + str(bigNum) + "}}")
|
| 1397 |
+
with pytest.raises(ValueError, match=msg):
|
| 1398 |
+
read_json(json)
|
| 1399 |
+
|
| 1400 |
+
def test_read_json_large_numbers2(self):
|
| 1401 |
+
# GH18842
|
| 1402 |
+
json = '{"articleId": "1404366058080022500245"}'
|
| 1403 |
+
json = StringIO(json)
|
| 1404 |
+
result = read_json(json, typ="series")
|
| 1405 |
+
expected = Series(1.404366e21, index=["articleId"])
|
| 1406 |
+
tm.assert_series_equal(result, expected)
|
| 1407 |
+
|
| 1408 |
+
json = '{"0": {"articleId": "1404366058080022500245"}}'
|
| 1409 |
+
json = StringIO(json)
|
| 1410 |
+
result = read_json(json)
|
| 1411 |
+
expected = DataFrame(1.404366e21, index=["articleId"], columns=[0])
|
| 1412 |
+
tm.assert_frame_equal(result, expected)
|
| 1413 |
+
|
| 1414 |
+
def test_to_jsonl(self):
|
| 1415 |
+
# GH9180
|
| 1416 |
+
df = DataFrame([[1, 2], [1, 2]], columns=["a", "b"])
|
| 1417 |
+
result = df.to_json(orient="records", lines=True)
|
| 1418 |
+
expected = '{"a":1,"b":2}\n{"a":1,"b":2}\n'
|
| 1419 |
+
assert result == expected
|
| 1420 |
+
|
| 1421 |
+
df = DataFrame([["foo}", "bar"], ['foo"', "bar"]], columns=["a", "b"])
|
| 1422 |
+
result = df.to_json(orient="records", lines=True)
|
| 1423 |
+
expected = '{"a":"foo}","b":"bar"}\n{"a":"foo\\"","b":"bar"}\n'
|
| 1424 |
+
assert result == expected
|
| 1425 |
+
tm.assert_frame_equal(read_json(StringIO(result), lines=True), df)
|
| 1426 |
+
|
| 1427 |
+
# GH15096: escaped characters in columns and data
|
| 1428 |
+
df = DataFrame([["foo\\", "bar"], ['foo"', "bar"]], columns=["a\\", "b"])
|
| 1429 |
+
result = df.to_json(orient="records", lines=True)
|
| 1430 |
+
expected = '{"a\\\\":"foo\\\\","b":"bar"}\n{"a\\\\":"foo\\"","b":"bar"}\n'
|
| 1431 |
+
assert result == expected
|
| 1432 |
+
|
| 1433 |
+
tm.assert_frame_equal(read_json(StringIO(result), lines=True), df)
|
| 1434 |
+
|
| 1435 |
+
# TODO: there is a near-identical test for pytables; can we share?
|
| 1436 |
+
@pytest.mark.xfail(reason="GH#13774 encoding kwarg not supported", raises=TypeError)
|
| 1437 |
+
@pytest.mark.parametrize(
|
| 1438 |
+
"val",
|
| 1439 |
+
[
|
| 1440 |
+
[b"E\xc9, 17", b"", b"a", b"b", b"c"],
|
| 1441 |
+
[b"E\xc9, 17", b"a", b"b", b"c"],
|
| 1442 |
+
[b"EE, 17", b"", b"a", b"b", b"c"],
|
| 1443 |
+
[b"E\xc9, 17", b"\xf8\xfc", b"a", b"b", b"c"],
|
| 1444 |
+
[b"", b"a", b"b", b"c"],
|
| 1445 |
+
[b"\xf8\xfc", b"a", b"b", b"c"],
|
| 1446 |
+
[b"A\xf8\xfc", b"", b"a", b"b", b"c"],
|
| 1447 |
+
[np.nan, b"", b"b", b"c"],
|
| 1448 |
+
[b"A\xf8\xfc", np.nan, b"", b"b", b"c"],
|
| 1449 |
+
],
|
| 1450 |
+
)
|
| 1451 |
+
@pytest.mark.parametrize("dtype", ["category", object])
|
| 1452 |
+
def test_latin_encoding(self, dtype, val):
|
| 1453 |
+
# GH 13774
|
| 1454 |
+
ser = Series(
|
| 1455 |
+
[x.decode("latin-1") if isinstance(x, bytes) else x for x in val],
|
| 1456 |
+
dtype=dtype,
|
| 1457 |
+
)
|
| 1458 |
+
encoding = "latin-1"
|
| 1459 |
+
with tm.ensure_clean("test.json") as path:
|
| 1460 |
+
ser.to_json(path, encoding=encoding)
|
| 1461 |
+
retr = read_json(StringIO(path), encoding=encoding)
|
| 1462 |
+
tm.assert_series_equal(ser, retr, check_categorical=False)
|
| 1463 |
+
|
| 1464 |
+
def test_data_frame_size_after_to_json(self):
|
| 1465 |
+
# GH15344
|
| 1466 |
+
df = DataFrame({"a": [str(1)]})
|
| 1467 |
+
|
| 1468 |
+
size_before = df.memory_usage(index=True, deep=True).sum()
|
| 1469 |
+
df.to_json()
|
| 1470 |
+
size_after = df.memory_usage(index=True, deep=True).sum()
|
| 1471 |
+
|
| 1472 |
+
assert size_before == size_after
|
| 1473 |
+
|
| 1474 |
+
@pytest.mark.parametrize(
|
| 1475 |
+
"index", [None, [1, 2], [1.0, 2.0], ["a", "b"], ["1", "2"], ["1.", "2."]]
|
| 1476 |
+
)
|
| 1477 |
+
@pytest.mark.parametrize("columns", [["a", "b"], ["1", "2"], ["1.", "2."]])
|
| 1478 |
+
def test_from_json_to_json_table_index_and_columns(self, index, columns):
|
| 1479 |
+
# GH25433 GH25435
|
| 1480 |
+
expected = DataFrame([[1, 2], [3, 4]], index=index, columns=columns)
|
| 1481 |
+
dfjson = expected.to_json(orient="table")
|
| 1482 |
+
|
| 1483 |
+
result = read_json(StringIO(dfjson), orient="table")
|
| 1484 |
+
tm.assert_frame_equal(result, expected)
|
| 1485 |
+
|
| 1486 |
+
def test_from_json_to_json_table_dtypes(self):
|
| 1487 |
+
# GH21345
|
| 1488 |
+
expected = DataFrame({"a": [1, 2], "b": [3.0, 4.0], "c": ["5", "6"]})
|
| 1489 |
+
dfjson = expected.to_json(orient="table")
|
| 1490 |
+
result = read_json(StringIO(dfjson), orient="table")
|
| 1491 |
+
tm.assert_frame_equal(result, expected)
|
| 1492 |
+
|
| 1493 |
+
# TODO: We are casting to string which coerces None to NaN before casting back
|
| 1494 |
+
# to object, ending up with incorrect na values
|
| 1495 |
+
@pytest.mark.xfail(using_pyarrow_string_dtype(), reason="incorrect na conversion")
|
| 1496 |
+
@pytest.mark.parametrize("orient", ["split", "records", "index", "columns"])
|
| 1497 |
+
def test_to_json_from_json_columns_dtypes(self, orient):
|
| 1498 |
+
# GH21892 GH33205
|
| 1499 |
+
expected = DataFrame.from_dict(
|
| 1500 |
+
{
|
| 1501 |
+
"Integer": Series([1, 2, 3], dtype="int64"),
|
| 1502 |
+
"Float": Series([None, 2.0, 3.0], dtype="float64"),
|
| 1503 |
+
"Object": Series([None, "", "c"], dtype="object"),
|
| 1504 |
+
"Bool": Series([True, False, True], dtype="bool"),
|
| 1505 |
+
"Category": Series(["a", "b", None], dtype="category"),
|
| 1506 |
+
"Datetime": Series(
|
| 1507 |
+
["2020-01-01", None, "2020-01-03"], dtype="datetime64[ns]"
|
| 1508 |
+
),
|
| 1509 |
+
}
|
| 1510 |
+
)
|
| 1511 |
+
dfjson = expected.to_json(orient=orient)
|
| 1512 |
+
|
| 1513 |
+
result = read_json(
|
| 1514 |
+
StringIO(dfjson),
|
| 1515 |
+
orient=orient,
|
| 1516 |
+
dtype={
|
| 1517 |
+
"Integer": "int64",
|
| 1518 |
+
"Float": "float64",
|
| 1519 |
+
"Object": "object",
|
| 1520 |
+
"Bool": "bool",
|
| 1521 |
+
"Category": "category",
|
| 1522 |
+
"Datetime": "datetime64[ns]",
|
| 1523 |
+
},
|
| 1524 |
+
)
|
| 1525 |
+
tm.assert_frame_equal(result, expected)
|
| 1526 |
+
|
| 1527 |
+
@pytest.mark.parametrize("dtype", [True, {"b": int, "c": int}])
|
| 1528 |
+
def test_read_json_table_dtype_raises(self, dtype):
|
| 1529 |
+
# GH21345
|
| 1530 |
+
df = DataFrame({"a": [1, 2], "b": [3.0, 4.0], "c": ["5", "6"]})
|
| 1531 |
+
dfjson = df.to_json(orient="table")
|
| 1532 |
+
msg = "cannot pass both dtype and orient='table'"
|
| 1533 |
+
with pytest.raises(ValueError, match=msg):
|
| 1534 |
+
read_json(dfjson, orient="table", dtype=dtype)
|
| 1535 |
+
|
| 1536 |
+
@pytest.mark.parametrize("orient", ["index", "columns", "records", "values"])
|
| 1537 |
+
def test_read_json_table_empty_axes_dtype(self, orient):
|
| 1538 |
+
# GH28558
|
| 1539 |
+
|
| 1540 |
+
expected = DataFrame()
|
| 1541 |
+
result = read_json(StringIO("{}"), orient=orient, convert_axes=True)
|
| 1542 |
+
tm.assert_index_equal(result.index, expected.index)
|
| 1543 |
+
tm.assert_index_equal(result.columns, expected.columns)
|
| 1544 |
+
|
| 1545 |
+
def test_read_json_table_convert_axes_raises(self):
|
| 1546 |
+
# GH25433 GH25435
|
| 1547 |
+
df = DataFrame([[1, 2], [3, 4]], index=[1.0, 2.0], columns=["1.", "2."])
|
| 1548 |
+
dfjson = df.to_json(orient="table")
|
| 1549 |
+
msg = "cannot pass both convert_axes and orient='table'"
|
| 1550 |
+
with pytest.raises(ValueError, match=msg):
|
| 1551 |
+
read_json(dfjson, orient="table", convert_axes=True)
|
| 1552 |
+
|
| 1553 |
+
@pytest.mark.parametrize(
|
| 1554 |
+
"data, expected",
|
| 1555 |
+
[
|
| 1556 |
+
(
|
| 1557 |
+
DataFrame([[1, 2], [4, 5]], columns=["a", "b"]),
|
| 1558 |
+
{"columns": ["a", "b"], "data": [[1, 2], [4, 5]]},
|
| 1559 |
+
),
|
| 1560 |
+
(
|
| 1561 |
+
DataFrame([[1, 2], [4, 5]], columns=["a", "b"]).rename_axis("foo"),
|
| 1562 |
+
{"columns": ["a", "b"], "data": [[1, 2], [4, 5]]},
|
| 1563 |
+
),
|
| 1564 |
+
(
|
| 1565 |
+
DataFrame(
|
| 1566 |
+
[[1, 2], [4, 5]], columns=["a", "b"], index=[["a", "b"], ["c", "d"]]
|
| 1567 |
+
),
|
| 1568 |
+
{"columns": ["a", "b"], "data": [[1, 2], [4, 5]]},
|
| 1569 |
+
),
|
| 1570 |
+
(Series([1, 2, 3], name="A"), {"name": "A", "data": [1, 2, 3]}),
|
| 1571 |
+
(
|
| 1572 |
+
Series([1, 2, 3], name="A").rename_axis("foo"),
|
| 1573 |
+
{"name": "A", "data": [1, 2, 3]},
|
| 1574 |
+
),
|
| 1575 |
+
(
|
| 1576 |
+
Series([1, 2], name="A", index=[["a", "b"], ["c", "d"]]),
|
| 1577 |
+
{"name": "A", "data": [1, 2]},
|
| 1578 |
+
),
|
| 1579 |
+
],
|
| 1580 |
+
)
|
| 1581 |
+
def test_index_false_to_json_split(self, data, expected):
|
| 1582 |
+
# GH 17394
|
| 1583 |
+
# Testing index=False in to_json with orient='split'
|
| 1584 |
+
|
| 1585 |
+
result = data.to_json(orient="split", index=False)
|
| 1586 |
+
result = json.loads(result)
|
| 1587 |
+
|
| 1588 |
+
assert result == expected
|
| 1589 |
+
|
| 1590 |
+
@pytest.mark.parametrize(
|
| 1591 |
+
"data",
|
| 1592 |
+
[
|
| 1593 |
+
(DataFrame([[1, 2], [4, 5]], columns=["a", "b"])),
|
| 1594 |
+
(DataFrame([[1, 2], [4, 5]], columns=["a", "b"]).rename_axis("foo")),
|
| 1595 |
+
(
|
| 1596 |
+
DataFrame(
|
| 1597 |
+
[[1, 2], [4, 5]], columns=["a", "b"], index=[["a", "b"], ["c", "d"]]
|
| 1598 |
+
)
|
| 1599 |
+
),
|
| 1600 |
+
(Series([1, 2, 3], name="A")),
|
| 1601 |
+
(Series([1, 2, 3], name="A").rename_axis("foo")),
|
| 1602 |
+
(Series([1, 2], name="A", index=[["a", "b"], ["c", "d"]])),
|
| 1603 |
+
],
|
| 1604 |
+
)
|
| 1605 |
+
def test_index_false_to_json_table(self, data):
|
| 1606 |
+
# GH 17394
|
| 1607 |
+
# Testing index=False in to_json with orient='table'
|
| 1608 |
+
|
| 1609 |
+
result = data.to_json(orient="table", index=False)
|
| 1610 |
+
result = json.loads(result)
|
| 1611 |
+
|
| 1612 |
+
expected = {
|
| 1613 |
+
"schema": pd.io.json.build_table_schema(data, index=False),
|
| 1614 |
+
"data": DataFrame(data).to_dict(orient="records"),
|
| 1615 |
+
}
|
| 1616 |
+
|
| 1617 |
+
assert result == expected
|
| 1618 |
+
|
| 1619 |
+
@pytest.mark.parametrize("orient", ["index", "columns"])
|
| 1620 |
+
def test_index_false_error_to_json(self, orient):
|
| 1621 |
+
# GH 17394, 25513
|
| 1622 |
+
# Testing error message from to_json with index=False
|
| 1623 |
+
|
| 1624 |
+
df = DataFrame([[1, 2], [4, 5]], columns=["a", "b"])
|
| 1625 |
+
|
| 1626 |
+
msg = (
|
| 1627 |
+
"'index=False' is only valid when 'orient' is 'split', "
|
| 1628 |
+
"'table', 'records', or 'values'"
|
| 1629 |
+
)
|
| 1630 |
+
with pytest.raises(ValueError, match=msg):
|
| 1631 |
+
df.to_json(orient=orient, index=False)
|
| 1632 |
+
|
| 1633 |
+
@pytest.mark.parametrize("orient", ["records", "values"])
|
| 1634 |
+
def test_index_true_error_to_json(self, orient):
|
| 1635 |
+
# GH 25513
|
| 1636 |
+
# Testing error message from to_json with index=True
|
| 1637 |
+
|
| 1638 |
+
df = DataFrame([[1, 2], [4, 5]], columns=["a", "b"])
|
| 1639 |
+
|
| 1640 |
+
msg = (
|
| 1641 |
+
"'index=True' is only valid when 'orient' is 'split', "
|
| 1642 |
+
"'table', 'index', or 'columns'"
|
| 1643 |
+
)
|
| 1644 |
+
with pytest.raises(ValueError, match=msg):
|
| 1645 |
+
df.to_json(orient=orient, index=True)
|
| 1646 |
+
|
| 1647 |
+
@pytest.mark.parametrize("orient", ["split", "table"])
|
| 1648 |
+
@pytest.mark.parametrize("index", [True, False])
|
| 1649 |
+
def test_index_false_from_json_to_json(self, orient, index):
|
| 1650 |
+
# GH25170
|
| 1651 |
+
# Test index=False in from_json to_json
|
| 1652 |
+
expected = DataFrame({"a": [1, 2], "b": [3, 4]})
|
| 1653 |
+
dfjson = expected.to_json(orient=orient, index=index)
|
| 1654 |
+
result = read_json(StringIO(dfjson), orient=orient)
|
| 1655 |
+
tm.assert_frame_equal(result, expected)
|
| 1656 |
+
|
| 1657 |
+
def test_read_timezone_information(self):
|
| 1658 |
+
# GH 25546
|
| 1659 |
+
result = read_json(
|
| 1660 |
+
StringIO('{"2019-01-01T11:00:00.000Z":88}'), typ="series", orient="index"
|
| 1661 |
+
)
|
| 1662 |
+
exp_dti = DatetimeIndex(["2019-01-01 11:00:00"], dtype="M8[ns, UTC]")
|
| 1663 |
+
expected = Series([88], index=exp_dti)
|
| 1664 |
+
tm.assert_series_equal(result, expected)
|
| 1665 |
+
|
| 1666 |
+
@pytest.mark.parametrize(
|
| 1667 |
+
"url",
|
| 1668 |
+
[
|
| 1669 |
+
"s3://example-fsspec/",
|
| 1670 |
+
"gcs://another-fsspec/file.json",
|
| 1671 |
+
"https://example-site.com/data",
|
| 1672 |
+
"some-protocol://data.txt",
|
| 1673 |
+
],
|
| 1674 |
+
)
|
| 1675 |
+
def test_read_json_with_url_value(self, url):
|
| 1676 |
+
# GH 36271
|
| 1677 |
+
result = read_json(StringIO(f'{{"url":{{"0":"{url}"}}}}'))
|
| 1678 |
+
expected = DataFrame({"url": [url]})
|
| 1679 |
+
tm.assert_frame_equal(result, expected)
|
| 1680 |
+
|
| 1681 |
+
@pytest.mark.parametrize(
|
| 1682 |
+
"compression",
|
| 1683 |
+
["", ".gz", ".bz2", ".tar"],
|
| 1684 |
+
)
|
| 1685 |
+
def test_read_json_with_very_long_file_path(self, compression):
|
| 1686 |
+
# GH 46718
|
| 1687 |
+
long_json_path = f'{"a" * 1000}.json{compression}'
|
| 1688 |
+
with pytest.raises(
|
| 1689 |
+
FileNotFoundError, match=f"File {long_json_path} does not exist"
|
| 1690 |
+
):
|
| 1691 |
+
# path too long for Windows is handled in file_exists() but raises in
|
| 1692 |
+
# _get_data_from_filepath()
|
| 1693 |
+
read_json(long_json_path)
|
| 1694 |
+
|
| 1695 |
+
@pytest.mark.parametrize(
|
| 1696 |
+
"date_format,key", [("epoch", 86400000), ("iso", "P1DT0H0M0S")]
|
| 1697 |
+
)
|
| 1698 |
+
def test_timedelta_as_label(self, date_format, key):
|
| 1699 |
+
df = DataFrame([[1]], columns=[pd.Timedelta("1D")])
|
| 1700 |
+
expected = f'{{"{key}":{{"0":1}}}}'
|
| 1701 |
+
result = df.to_json(date_format=date_format)
|
| 1702 |
+
|
| 1703 |
+
assert result == expected
|
| 1704 |
+
|
| 1705 |
+
@pytest.mark.parametrize(
|
| 1706 |
+
"orient,expected",
|
| 1707 |
+
[
|
| 1708 |
+
("index", "{\"('a', 'b')\":{\"('c', 'd')\":1}}"),
|
| 1709 |
+
("columns", "{\"('c', 'd')\":{\"('a', 'b')\":1}}"),
|
| 1710 |
+
# TODO: the below have separate encoding procedures
|
| 1711 |
+
pytest.param(
|
| 1712 |
+
"split",
|
| 1713 |
+
"",
|
| 1714 |
+
marks=pytest.mark.xfail(
|
| 1715 |
+
reason="Produces JSON but not in a consistent manner"
|
| 1716 |
+
),
|
| 1717 |
+
),
|
| 1718 |
+
pytest.param(
|
| 1719 |
+
"table",
|
| 1720 |
+
"",
|
| 1721 |
+
marks=pytest.mark.xfail(
|
| 1722 |
+
reason="Produces JSON but not in a consistent manner"
|
| 1723 |
+
),
|
| 1724 |
+
),
|
| 1725 |
+
],
|
| 1726 |
+
)
|
| 1727 |
+
def test_tuple_labels(self, orient, expected):
|
| 1728 |
+
# GH 20500
|
| 1729 |
+
df = DataFrame([[1]], index=[("a", "b")], columns=[("c", "d")])
|
| 1730 |
+
result = df.to_json(orient=orient)
|
| 1731 |
+
assert result == expected
|
| 1732 |
+
|
| 1733 |
+
@pytest.mark.parametrize("indent", [1, 2, 4])
|
| 1734 |
+
def test_to_json_indent(self, indent):
|
| 1735 |
+
# GH 12004
|
| 1736 |
+
df = DataFrame([["foo", "bar"], ["baz", "qux"]], columns=["a", "b"])
|
| 1737 |
+
|
| 1738 |
+
result = df.to_json(indent=indent)
|
| 1739 |
+
spaces = " " * indent
|
| 1740 |
+
expected = f"""{{
|
| 1741 |
+
{spaces}"a":{{
|
| 1742 |
+
{spaces}{spaces}"0":"foo",
|
| 1743 |
+
{spaces}{spaces}"1":"baz"
|
| 1744 |
+
{spaces}}},
|
| 1745 |
+
{spaces}"b":{{
|
| 1746 |
+
{spaces}{spaces}"0":"bar",
|
| 1747 |
+
{spaces}{spaces}"1":"qux"
|
| 1748 |
+
{spaces}}}
|
| 1749 |
+
}}"""
|
| 1750 |
+
|
| 1751 |
+
assert result == expected
|
| 1752 |
+
|
| 1753 |
+
@pytest.mark.skipif(
|
| 1754 |
+
using_pyarrow_string_dtype(),
|
| 1755 |
+
reason="Adjust expected when infer_string is default, no bug here, "
|
| 1756 |
+
"just a complicated parametrization",
|
| 1757 |
+
)
|
| 1758 |
+
@pytest.mark.parametrize(
|
| 1759 |
+
"orient,expected",
|
| 1760 |
+
[
|
| 1761 |
+
(
|
| 1762 |
+
"split",
|
| 1763 |
+
"""{
|
| 1764 |
+
"columns":[
|
| 1765 |
+
"a",
|
| 1766 |
+
"b"
|
| 1767 |
+
],
|
| 1768 |
+
"index":[
|
| 1769 |
+
0,
|
| 1770 |
+
1
|
| 1771 |
+
],
|
| 1772 |
+
"data":[
|
| 1773 |
+
[
|
| 1774 |
+
"foo",
|
| 1775 |
+
"bar"
|
| 1776 |
+
],
|
| 1777 |
+
[
|
| 1778 |
+
"baz",
|
| 1779 |
+
"qux"
|
| 1780 |
+
]
|
| 1781 |
+
]
|
| 1782 |
+
}""",
|
| 1783 |
+
),
|
| 1784 |
+
(
|
| 1785 |
+
"records",
|
| 1786 |
+
"""[
|
| 1787 |
+
{
|
| 1788 |
+
"a":"foo",
|
| 1789 |
+
"b":"bar"
|
| 1790 |
+
},
|
| 1791 |
+
{
|
| 1792 |
+
"a":"baz",
|
| 1793 |
+
"b":"qux"
|
| 1794 |
+
}
|
| 1795 |
+
]""",
|
| 1796 |
+
),
|
| 1797 |
+
(
|
| 1798 |
+
"index",
|
| 1799 |
+
"""{
|
| 1800 |
+
"0":{
|
| 1801 |
+
"a":"foo",
|
| 1802 |
+
"b":"bar"
|
| 1803 |
+
},
|
| 1804 |
+
"1":{
|
| 1805 |
+
"a":"baz",
|
| 1806 |
+
"b":"qux"
|
| 1807 |
+
}
|
| 1808 |
+
}""",
|
| 1809 |
+
),
|
| 1810 |
+
(
|
| 1811 |
+
"columns",
|
| 1812 |
+
"""{
|
| 1813 |
+
"a":{
|
| 1814 |
+
"0":"foo",
|
| 1815 |
+
"1":"baz"
|
| 1816 |
+
},
|
| 1817 |
+
"b":{
|
| 1818 |
+
"0":"bar",
|
| 1819 |
+
"1":"qux"
|
| 1820 |
+
}
|
| 1821 |
+
}""",
|
| 1822 |
+
),
|
| 1823 |
+
(
|
| 1824 |
+
"values",
|
| 1825 |
+
"""[
|
| 1826 |
+
[
|
| 1827 |
+
"foo",
|
| 1828 |
+
"bar"
|
| 1829 |
+
],
|
| 1830 |
+
[
|
| 1831 |
+
"baz",
|
| 1832 |
+
"qux"
|
| 1833 |
+
]
|
| 1834 |
+
]""",
|
| 1835 |
+
),
|
| 1836 |
+
(
|
| 1837 |
+
"table",
|
| 1838 |
+
"""{
|
| 1839 |
+
"schema":{
|
| 1840 |
+
"fields":[
|
| 1841 |
+
{
|
| 1842 |
+
"name":"index",
|
| 1843 |
+
"type":"integer"
|
| 1844 |
+
},
|
| 1845 |
+
{
|
| 1846 |
+
"name":"a",
|
| 1847 |
+
"type":"string"
|
| 1848 |
+
},
|
| 1849 |
+
{
|
| 1850 |
+
"name":"b",
|
| 1851 |
+
"type":"string"
|
| 1852 |
+
}
|
| 1853 |
+
],
|
| 1854 |
+
"primaryKey":[
|
| 1855 |
+
"index"
|
| 1856 |
+
],
|
| 1857 |
+
"pandas_version":"1.4.0"
|
| 1858 |
+
},
|
| 1859 |
+
"data":[
|
| 1860 |
+
{
|
| 1861 |
+
"index":0,
|
| 1862 |
+
"a":"foo",
|
| 1863 |
+
"b":"bar"
|
| 1864 |
+
},
|
| 1865 |
+
{
|
| 1866 |
+
"index":1,
|
| 1867 |
+
"a":"baz",
|
| 1868 |
+
"b":"qux"
|
| 1869 |
+
}
|
| 1870 |
+
]
|
| 1871 |
+
}""",
|
| 1872 |
+
),
|
| 1873 |
+
],
|
| 1874 |
+
)
|
| 1875 |
+
def test_json_indent_all_orients(self, orient, expected):
|
| 1876 |
+
# GH 12004
|
| 1877 |
+
df = DataFrame([["foo", "bar"], ["baz", "qux"]], columns=["a", "b"])
|
| 1878 |
+
result = df.to_json(orient=orient, indent=4)
|
| 1879 |
+
assert result == expected
|
| 1880 |
+
|
| 1881 |
+
def test_json_negative_indent_raises(self):
|
| 1882 |
+
with pytest.raises(ValueError, match="must be a nonnegative integer"):
|
| 1883 |
+
DataFrame().to_json(indent=-1)
|
| 1884 |
+
|
| 1885 |
+
def test_emca_262_nan_inf_support(self):
|
| 1886 |
+
# GH 12213
|
| 1887 |
+
data = StringIO(
|
| 1888 |
+
'["a", NaN, "NaN", Infinity, "Infinity", -Infinity, "-Infinity"]'
|
| 1889 |
+
)
|
| 1890 |
+
result = read_json(data)
|
| 1891 |
+
expected = DataFrame(
|
| 1892 |
+
["a", None, "NaN", np.inf, "Infinity", -np.inf, "-Infinity"]
|
| 1893 |
+
)
|
| 1894 |
+
tm.assert_frame_equal(result, expected)
|
| 1895 |
+
|
| 1896 |
+
def test_frame_int_overflow(self):
|
| 1897 |
+
# GH 30320
|
| 1898 |
+
encoded_json = json.dumps([{"col": "31900441201190696999"}, {"col": "Text"}])
|
| 1899 |
+
expected = DataFrame({"col": ["31900441201190696999", "Text"]})
|
| 1900 |
+
result = read_json(StringIO(encoded_json))
|
| 1901 |
+
tm.assert_frame_equal(result, expected)
|
| 1902 |
+
|
| 1903 |
+
@pytest.mark.parametrize(
|
| 1904 |
+
"dataframe,expected",
|
| 1905 |
+
[
|
| 1906 |
+
(
|
| 1907 |
+
DataFrame({"x": [1, 2, 3], "y": ["a", "b", "c"]}),
|
| 1908 |
+
'{"(0, \'x\')":1,"(0, \'y\')":"a","(1, \'x\')":2,'
|
| 1909 |
+
'"(1, \'y\')":"b","(2, \'x\')":3,"(2, \'y\')":"c"}',
|
| 1910 |
+
)
|
| 1911 |
+
],
|
| 1912 |
+
)
|
| 1913 |
+
def test_json_multiindex(self, dataframe, expected):
|
| 1914 |
+
series = dataframe.stack(future_stack=True)
|
| 1915 |
+
result = series.to_json(orient="index")
|
| 1916 |
+
assert result == expected
|
| 1917 |
+
|
| 1918 |
+
@pytest.mark.single_cpu
|
| 1919 |
+
def test_to_s3(self, s3_public_bucket, s3so):
|
| 1920 |
+
# GH 28375
|
| 1921 |
+
mock_bucket_name, target_file = s3_public_bucket.name, "test.json"
|
| 1922 |
+
df = DataFrame({"x": [1, 2, 3], "y": [2, 4, 6]})
|
| 1923 |
+
df.to_json(f"s3://{mock_bucket_name}/{target_file}", storage_options=s3so)
|
| 1924 |
+
timeout = 5
|
| 1925 |
+
while True:
|
| 1926 |
+
if target_file in (obj.key for obj in s3_public_bucket.objects.all()):
|
| 1927 |
+
break
|
| 1928 |
+
time.sleep(0.1)
|
| 1929 |
+
timeout -= 0.1
|
| 1930 |
+
assert timeout > 0, "Timed out waiting for file to appear on moto"
|
| 1931 |
+
|
| 1932 |
+
def test_json_pandas_nulls(self, nulls_fixture, request):
|
| 1933 |
+
# GH 31615
|
| 1934 |
+
if isinstance(nulls_fixture, Decimal):
|
| 1935 |
+
mark = pytest.mark.xfail(reason="not implemented")
|
| 1936 |
+
request.applymarker(mark)
|
| 1937 |
+
|
| 1938 |
+
result = DataFrame([[nulls_fixture]]).to_json()
|
| 1939 |
+
assert result == '{"0":{"0":null}}'
|
| 1940 |
+
|
| 1941 |
+
def test_readjson_bool_series(self):
|
| 1942 |
+
# GH31464
|
| 1943 |
+
result = read_json(StringIO("[true, true, false]"), typ="series")
|
| 1944 |
+
expected = Series([True, True, False])
|
| 1945 |
+
tm.assert_series_equal(result, expected)
|
| 1946 |
+
|
| 1947 |
+
def test_to_json_multiindex_escape(self):
|
| 1948 |
+
# GH 15273
|
| 1949 |
+
df = DataFrame(
|
| 1950 |
+
True,
|
| 1951 |
+
index=date_range("2017-01-20", "2017-01-23"),
|
| 1952 |
+
columns=["foo", "bar"],
|
| 1953 |
+
).stack(future_stack=True)
|
| 1954 |
+
result = df.to_json()
|
| 1955 |
+
expected = (
|
| 1956 |
+
"{\"(Timestamp('2017-01-20 00:00:00'), 'foo')\":true,"
|
| 1957 |
+
"\"(Timestamp('2017-01-20 00:00:00'), 'bar')\":true,"
|
| 1958 |
+
"\"(Timestamp('2017-01-21 00:00:00'), 'foo')\":true,"
|
| 1959 |
+
"\"(Timestamp('2017-01-21 00:00:00'), 'bar')\":true,"
|
| 1960 |
+
"\"(Timestamp('2017-01-22 00:00:00'), 'foo')\":true,"
|
| 1961 |
+
"\"(Timestamp('2017-01-22 00:00:00'), 'bar')\":true,"
|
| 1962 |
+
"\"(Timestamp('2017-01-23 00:00:00'), 'foo')\":true,"
|
| 1963 |
+
"\"(Timestamp('2017-01-23 00:00:00'), 'bar')\":true}"
|
| 1964 |
+
)
|
| 1965 |
+
assert result == expected
|
| 1966 |
+
|
| 1967 |
+
def test_to_json_series_of_objects(self):
|
| 1968 |
+
class _TestObject:
|
| 1969 |
+
def __init__(self, a, b, _c, d) -> None:
|
| 1970 |
+
self.a = a
|
| 1971 |
+
self.b = b
|
| 1972 |
+
self._c = _c
|
| 1973 |
+
self.d = d
|
| 1974 |
+
|
| 1975 |
+
def e(self):
|
| 1976 |
+
return 5
|
| 1977 |
+
|
| 1978 |
+
# JSON keys should be all non-callable non-underscore attributes, see GH-42768
|
| 1979 |
+
series = Series([_TestObject(a=1, b=2, _c=3, d=4)])
|
| 1980 |
+
assert json.loads(series.to_json()) == {"0": {"a": 1, "b": 2, "d": 4}}
|
| 1981 |
+
|
| 1982 |
+
@pytest.mark.parametrize(
|
| 1983 |
+
"data,expected",
|
| 1984 |
+
[
|
| 1985 |
+
(
|
| 1986 |
+
Series({0: -6 + 8j, 1: 0 + 1j, 2: 9 - 5j}),
|
| 1987 |
+
'{"0":{"imag":8.0,"real":-6.0},'
|
| 1988 |
+
'"1":{"imag":1.0,"real":0.0},'
|
| 1989 |
+
'"2":{"imag":-5.0,"real":9.0}}',
|
| 1990 |
+
),
|
| 1991 |
+
(
|
| 1992 |
+
Series({0: -9.39 + 0.66j, 1: 3.95 + 9.32j, 2: 4.03 - 0.17j}),
|
| 1993 |
+
'{"0":{"imag":0.66,"real":-9.39},'
|
| 1994 |
+
'"1":{"imag":9.32,"real":3.95},'
|
| 1995 |
+
'"2":{"imag":-0.17,"real":4.03}}',
|
| 1996 |
+
),
|
| 1997 |
+
(
|
| 1998 |
+
DataFrame([[-2 + 3j, -1 - 0j], [4 - 3j, -0 - 10j]]),
|
| 1999 |
+
'{"0":{"0":{"imag":3.0,"real":-2.0},'
|
| 2000 |
+
'"1":{"imag":-3.0,"real":4.0}},'
|
| 2001 |
+
'"1":{"0":{"imag":0.0,"real":-1.0},'
|
| 2002 |
+
'"1":{"imag":-10.0,"real":0.0}}}',
|
| 2003 |
+
),
|
| 2004 |
+
(
|
| 2005 |
+
DataFrame(
|
| 2006 |
+
[[-0.28 + 0.34j, -1.08 - 0.39j], [0.41 - 0.34j, -0.78 - 1.35j]]
|
| 2007 |
+
),
|
| 2008 |
+
'{"0":{"0":{"imag":0.34,"real":-0.28},'
|
| 2009 |
+
'"1":{"imag":-0.34,"real":0.41}},'
|
| 2010 |
+
'"1":{"0":{"imag":-0.39,"real":-1.08},'
|
| 2011 |
+
'"1":{"imag":-1.35,"real":-0.78}}}',
|
| 2012 |
+
),
|
| 2013 |
+
],
|
| 2014 |
+
)
|
| 2015 |
+
def test_complex_data_tojson(self, data, expected):
|
| 2016 |
+
# GH41174
|
| 2017 |
+
result = data.to_json()
|
| 2018 |
+
assert result == expected
|
| 2019 |
+
|
| 2020 |
+
def test_json_uint64(self):
|
| 2021 |
+
# GH21073
|
| 2022 |
+
expected = (
|
| 2023 |
+
'{"columns":["col1"],"index":[0,1],'
|
| 2024 |
+
'"data":[[13342205958987758245],[12388075603347835679]]}'
|
| 2025 |
+
)
|
| 2026 |
+
df = DataFrame(data={"col1": [13342205958987758245, 12388075603347835679]})
|
| 2027 |
+
result = df.to_json(orient="split")
|
| 2028 |
+
assert result == expected
|
| 2029 |
+
|
| 2030 |
+
@pytest.mark.parametrize(
|
| 2031 |
+
"orient", ["split", "records", "values", "index", "columns"]
|
| 2032 |
+
)
|
| 2033 |
+
def test_read_json_dtype_backend(
|
| 2034 |
+
self, string_storage, dtype_backend, orient, using_infer_string
|
| 2035 |
+
):
|
| 2036 |
+
# GH#50750
|
| 2037 |
+
pa = pytest.importorskip("pyarrow")
|
| 2038 |
+
df = DataFrame(
|
| 2039 |
+
{
|
| 2040 |
+
"a": Series([1, np.nan, 3], dtype="Int64"),
|
| 2041 |
+
"b": Series([1, 2, 3], dtype="Int64"),
|
| 2042 |
+
"c": Series([1.5, np.nan, 2.5], dtype="Float64"),
|
| 2043 |
+
"d": Series([1.5, 2.0, 2.5], dtype="Float64"),
|
| 2044 |
+
"e": [True, False, None],
|
| 2045 |
+
"f": [True, False, True],
|
| 2046 |
+
"g": ["a", "b", "c"],
|
| 2047 |
+
"h": ["a", "b", None],
|
| 2048 |
+
}
|
| 2049 |
+
)
|
| 2050 |
+
|
| 2051 |
+
if using_infer_string:
|
| 2052 |
+
string_array = ArrowStringArrayNumpySemantics(pa.array(["a", "b", "c"]))
|
| 2053 |
+
string_array_na = ArrowStringArrayNumpySemantics(pa.array(["a", "b", None]))
|
| 2054 |
+
elif string_storage == "python":
|
| 2055 |
+
string_array = StringArray(np.array(["a", "b", "c"], dtype=np.object_))
|
| 2056 |
+
string_array_na = StringArray(np.array(["a", "b", NA], dtype=np.object_))
|
| 2057 |
+
|
| 2058 |
+
elif dtype_backend == "pyarrow":
|
| 2059 |
+
pa = pytest.importorskip("pyarrow")
|
| 2060 |
+
from pandas.arrays import ArrowExtensionArray
|
| 2061 |
+
|
| 2062 |
+
string_array = ArrowExtensionArray(pa.array(["a", "b", "c"]))
|
| 2063 |
+
string_array_na = ArrowExtensionArray(pa.array(["a", "b", None]))
|
| 2064 |
+
|
| 2065 |
+
else:
|
| 2066 |
+
string_array = ArrowStringArray(pa.array(["a", "b", "c"]))
|
| 2067 |
+
string_array_na = ArrowStringArray(pa.array(["a", "b", None]))
|
| 2068 |
+
|
| 2069 |
+
out = df.to_json(orient=orient)
|
| 2070 |
+
with pd.option_context("mode.string_storage", string_storage):
|
| 2071 |
+
result = read_json(
|
| 2072 |
+
StringIO(out), dtype_backend=dtype_backend, orient=orient
|
| 2073 |
+
)
|
| 2074 |
+
|
| 2075 |
+
expected = DataFrame(
|
| 2076 |
+
{
|
| 2077 |
+
"a": Series([1, np.nan, 3], dtype="Int64"),
|
| 2078 |
+
"b": Series([1, 2, 3], dtype="Int64"),
|
| 2079 |
+
"c": Series([1.5, np.nan, 2.5], dtype="Float64"),
|
| 2080 |
+
"d": Series([1.5, 2.0, 2.5], dtype="Float64"),
|
| 2081 |
+
"e": Series([True, False, NA], dtype="boolean"),
|
| 2082 |
+
"f": Series([True, False, True], dtype="boolean"),
|
| 2083 |
+
"g": string_array,
|
| 2084 |
+
"h": string_array_na,
|
| 2085 |
+
}
|
| 2086 |
+
)
|
| 2087 |
+
|
| 2088 |
+
if dtype_backend == "pyarrow":
|
| 2089 |
+
from pandas.arrays import ArrowExtensionArray
|
| 2090 |
+
|
| 2091 |
+
expected = DataFrame(
|
| 2092 |
+
{
|
| 2093 |
+
col: ArrowExtensionArray(pa.array(expected[col], from_pandas=True))
|
| 2094 |
+
for col in expected.columns
|
| 2095 |
+
}
|
| 2096 |
+
)
|
| 2097 |
+
|
| 2098 |
+
if orient == "values":
|
| 2099 |
+
expected.columns = list(range(8))
|
| 2100 |
+
|
| 2101 |
+
tm.assert_frame_equal(result, expected)
|
| 2102 |
+
|
| 2103 |
+
@pytest.mark.parametrize("orient", ["split", "records", "index"])
|
| 2104 |
+
def test_read_json_nullable_series(self, string_storage, dtype_backend, orient):
|
| 2105 |
+
# GH#50750
|
| 2106 |
+
pa = pytest.importorskip("pyarrow")
|
| 2107 |
+
ser = Series([1, np.nan, 3], dtype="Int64")
|
| 2108 |
+
|
| 2109 |
+
out = ser.to_json(orient=orient)
|
| 2110 |
+
with pd.option_context("mode.string_storage", string_storage):
|
| 2111 |
+
result = read_json(
|
| 2112 |
+
StringIO(out), dtype_backend=dtype_backend, orient=orient, typ="series"
|
| 2113 |
+
)
|
| 2114 |
+
|
| 2115 |
+
expected = Series([1, np.nan, 3], dtype="Int64")
|
| 2116 |
+
|
| 2117 |
+
if dtype_backend == "pyarrow":
|
| 2118 |
+
from pandas.arrays import ArrowExtensionArray
|
| 2119 |
+
|
| 2120 |
+
expected = Series(ArrowExtensionArray(pa.array(expected, from_pandas=True)))
|
| 2121 |
+
|
| 2122 |
+
tm.assert_series_equal(result, expected)
|
| 2123 |
+
|
| 2124 |
+
def test_invalid_dtype_backend(self):
|
| 2125 |
+
msg = (
|
| 2126 |
+
"dtype_backend numpy is invalid, only 'numpy_nullable' and "
|
| 2127 |
+
"'pyarrow' are allowed."
|
| 2128 |
+
)
|
| 2129 |
+
with pytest.raises(ValueError, match=msg):
|
| 2130 |
+
read_json("test", dtype_backend="numpy")
|
| 2131 |
+
|
| 2132 |
+
|
| 2133 |
+
def test_invalid_engine():
|
| 2134 |
+
# GH 48893
|
| 2135 |
+
ser = Series(range(1))
|
| 2136 |
+
out = ser.to_json()
|
| 2137 |
+
with pytest.raises(ValueError, match="The engine type foo"):
|
| 2138 |
+
read_json(out, engine="foo")
|
| 2139 |
+
|
| 2140 |
+
|
| 2141 |
+
def test_pyarrow_engine_lines_false():
|
| 2142 |
+
# GH 48893
|
| 2143 |
+
ser = Series(range(1))
|
| 2144 |
+
out = ser.to_json()
|
| 2145 |
+
with pytest.raises(ValueError, match="currently pyarrow engine only supports"):
|
| 2146 |
+
read_json(out, engine="pyarrow", lines=False)
|
| 2147 |
+
|
| 2148 |
+
|
| 2149 |
+
def test_json_roundtrip_string_inference(orient):
|
| 2150 |
+
pytest.importorskip("pyarrow")
|
| 2151 |
+
df = DataFrame(
|
| 2152 |
+
[["a", "b"], ["c", "d"]], index=["row 1", "row 2"], columns=["col 1", "col 2"]
|
| 2153 |
+
)
|
| 2154 |
+
out = df.to_json()
|
| 2155 |
+
with pd.option_context("future.infer_string", True):
|
| 2156 |
+
result = read_json(StringIO(out))
|
| 2157 |
+
expected = DataFrame(
|
| 2158 |
+
[["a", "b"], ["c", "d"]],
|
| 2159 |
+
dtype="string[pyarrow_numpy]",
|
| 2160 |
+
index=Index(["row 1", "row 2"], dtype="string[pyarrow_numpy]"),
|
| 2161 |
+
columns=Index(["col 1", "col 2"], dtype="string[pyarrow_numpy]"),
|
| 2162 |
+
)
|
| 2163 |
+
tm.assert_frame_equal(result, expected)
|
| 2164 |
+
|
| 2165 |
+
|
| 2166 |
+
def test_json_pos_args_deprecation():
|
| 2167 |
+
# GH-54229
|
| 2168 |
+
df = DataFrame({"a": [1, 2, 3]})
|
| 2169 |
+
msg = (
|
| 2170 |
+
r"Starting with pandas version 3.0 all arguments of to_json except for the "
|
| 2171 |
+
r"argument 'path_or_buf' will be keyword-only."
|
| 2172 |
+
)
|
| 2173 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
| 2174 |
+
buf = BytesIO()
|
| 2175 |
+
df.to_json(buf, "split")
|
| 2176 |
+
|
| 2177 |
+
|
| 2178 |
+
@td.skip_if_no("pyarrow")
|
| 2179 |
+
def test_to_json_ea_null():
|
| 2180 |
+
# GH#57224
|
| 2181 |
+
df = DataFrame(
|
| 2182 |
+
{
|
| 2183 |
+
"a": Series([1, NA], dtype="int64[pyarrow]"),
|
| 2184 |
+
"b": Series([2, NA], dtype="Int64"),
|
| 2185 |
+
}
|
| 2186 |
+
)
|
| 2187 |
+
result = df.to_json(orient="records", lines=True)
|
| 2188 |
+
expected = """{"a":1,"b":2}
|
| 2189 |
+
{"a":null,"b":null}
|
| 2190 |
+
"""
|
| 2191 |
+
assert result == expected
|
| 2192 |
+
|
| 2193 |
+
|
| 2194 |
+
def test_read_json_lines_rangeindex():
|
| 2195 |
+
# GH 57429
|
| 2196 |
+
data = """
|
| 2197 |
+
{"a": 1, "b": 2}
|
| 2198 |
+
{"a": 3, "b": 4}
|
| 2199 |
+
"""
|
| 2200 |
+
result = read_json(StringIO(data), lines=True).index
|
| 2201 |
+
expected = RangeIndex(2)
|
| 2202 |
+
tm.assert_index_equal(result, expected, exact=True)
|