read_json support for orient="table" · pandas-dev/pandas@4319335 (original) (raw)

`@@ -14,6 +14,7 @@

`

14

14

`build_table_schema,

`

15

15

`make_field,

`

16

16

`set_default_names)

`

``

17

`+

import pandas.util.testing as tm

`

17

18

``

18

19

``

19

20

`class TestBuildSchema(object):

`

`@@ -471,3 +472,126 @@ def test_mi_falsey_name(self):

`

471

472

` ('a', 'b')]))

`

472

473

`result = [x['name'] for x in build_table_schema(df)['fields']]

`

473

474

`assert result == ['level_0', 'level_1', 0, 1, 2, 3]

`

``

475

+

``

476

+

``

477

`+

class TestTableOrientReader(object):

`

``

478

+

``

479

`+

def test_integer(self):

`

``

480

`+

df = DataFrame(

`

``

481

`+

{'A': [1, 2, 3, 4],

`

``

482

`+

},

`

``

483

`+

index=pd.Index(range(4), name='idx'))

`

``

484

`+

out = df.to_json(orient="table")

`

``

485

`+

result = pd.read_json(out, orient="table")

`

``

486

`+

tm.assert_frame_equal(df, result)

`

``

487

+

``

488

`+

def test_object(self):

`

``

489

`+

df = DataFrame(

`

``

490

`+

{'B': ['a', 'b', 'c', 'c'],

`

``

491

`+

},

`

``

492

`+

index=pd.Index(range(4), name='idx'))

`

``

493

`+

out = df.to_json(orient="table")

`

``

494

`+

result = pd.read_json(out, orient="table")

`

``

495

`+

tm.assert_frame_equal(df, result)

`

``

496

+

``

497

`+

def test_date_range(self):

`

``

498

`+

df = DataFrame(

`

``

499

`+

{'C': pd.date_range('2016-01-01', freq='d', periods=4),

`

``

500

`+

},

`

``

501

`+

index=pd.Index(range(4), name='idx'))

`

``

502

+

``

503

`+

out = df.to_json(orient="table")

`

``

504

`+

result = pd.read_json(out, orient="table")

`

``

505

`+

tm.assert_frame_equal(df, result)

`

``

506

+

``

507

`+

def test_timedelta_raises(self):

`

``

508

`+

df = DataFrame(

`

``

509

`+

{'D': pd.timedelta_range('1H', periods=4, freq='T'),

`

``

510

`+

},

`

``

511

`+

index=pd.Index(range(4), name='idx'))

`

``

512

+

``

513

`+

out = df.to_json(orient="table")

`

``

514

`+

with tm.assert_raises_regex(NotImplementedError, 'can not yet read '

`

``

515

`+

'ISO-formatted Timedelta data'):

`

``

516

`+

pd.read_json(out, orient="table")

`

``

517

+

``

518

`+

def test_categorical(self):

`

``

519

`+

df = DataFrame(

`

``

520

`+

{'E': pd.Series(pd.Categorical(['a', 'b', 'c', 'c'])),

`

``

521

`+

'F': pd.Series(pd.Categorical(['a', 'b', 'c', 'c'],

`

``

522

`+

ordered=True)),

`

``

523

`+

},

`

``

524

`+

index=pd.Index(range(4), name='idx'))

`

``

525

+

``

526

`+

out = df.to_json(orient="table")

`

``

527

`+

result = pd.read_json(out, orient="table")

`

``

528

`+

tm.assert_frame_equal(df, result)

`

``

529

+

``

530

`+

@pytest.mark.parametrize("float_vals", [

`

``

531

`+

pytest.param([1., 2., 3., 4.], marks=pytest.mark.xfail),

`

``

532

`+

[1.1, 2.2, 3.3, 4.4]])

`

``

533

`+

def test_float(self, float_vals):

`

``

534

`+

df = DataFrame(

`

``

535

`+

{'G': float_vals,

`

``

536

`+

},

`

``

537

`+

index=pd.Index(range(4), name='idx'))

`

``

538

+

``

539

`+

out = df.to_json(orient="table")

`

``

540

`+

result = pd.read_json(out, orient="table", convert_axes=False)

`

``

541

`+

tm.assert_frame_equal(df, result)

`

``

542

+

``

543

`+

def test_timezone_raises(self):

`

``

544

`+

df = DataFrame(

`

``

545

`+

{'H': pd.date_range('2016-01-01', freq='d', periods=4,

`

``

546

`+

tz='US/Central'),

`

``

547

`+

},

`

``

548

`+

index=pd.Index(range(4), name='idx'))

`

``

549

+

``

550

`+

out = df.to_json(orient="table")

`

``

551

`+

with tm.assert_raises_regex(NotImplementedError, 'can not yet read '

`

``

552

`+

'timezone data'):

`

``

553

`+

pd.read_json(out, orient="table")

`

``

554

+

``

555

`+

def test_bool(self):

`

``

556

`+

df = DataFrame(

`

``

557

`+

{'I': [True, False, False, True],

`

``

558

`+

},

`

``

559

`+

index=pd.Index(range(4), name='idx'))

`

``

560

+

``

561

`+

out = df.to_json(orient="table")

`

``

562

`+

result = pd.read_json(out, orient="table")

`

``

563

`+

tm.assert_frame_equal(df, result)

`

``

564

+

``

565

`+

def test_comprehensive(self):

`

``

566

`+

df = DataFrame(

`

``

567

`+

{'A': [1, 2, 3, 4],

`

``

568

`+

'B': ['a', 'b', 'c', 'c'],

`

``

569

`+

'C': pd.date_range('2016-01-01', freq='d', periods=4),

`

``

570

`+

'D': pd.timedelta_range('1H', periods=4, freq='T'),

`

``

571

`+

'E': pd.Series(pd.Categorical(['a', 'b', 'c', 'c'])),

`

``

572

`+

'F': pd.Series(pd.Categorical(['a', 'b', 'c', 'c'],

`

``

573

`+

ordered=True)),

`

``

574

`+

'G': [1.1, 2.2, 3.3, 4.4],

`

``

575

`+

'H': pd.date_range('2016-01-01', freq='d', periods=4,

`

``

576

`+

tz='US/Central'),

`

``

577

`+

'I': [True, False, False, True],

`

``

578

`+

},

`

``

579

`+

index=pd.Index(range(4), name='idx'))

`

``

580

+

``

581

`+

out = df.to_json(orient="table")

`

``

582

`+

result = pd.read_json(out, orient="table")

`

``

583

`+

tm.assert_frame_equal(df, result)

`

``

584

+

``

585

`+

@pytest.mark.parametrize("index_names", [[None, None], ['foo', 'bar']])

`

``

586

`+

def test_multiindex(self, index_names):

`

``

587

`+

GH 18912

`

``

588

`+

df = pd.DataFrame(

`

``

589

`+

[["Arr", "alpha", [1, 2, 3, 4]],

`

``

590

`+

["Bee", "Beta", [10, 20, 30, 40]]],

`

``

591

`+

index=[["A", "B"], ["Null", "Eins"]],

`

``

592

`+

columns=["Aussprache", "Griechisch", "Args"]

`

``

593

`+

)

`

``

594

`+

df.index.names = index_names

`

``

595

`+

out = df.to_json(orient="table")

`

``

596

`+

result = pd.read_json(out, orient="table")

`

``

597

`+

tm.assert_frame_equal(df, result)

`