@@ -42,22 +42,9 @@ def assert_json_roundtrip_equal(result, expected, orient):
42
42
43
43
@pytest .mark .filterwarnings ("ignore:the 'numpy' keyword is deprecated:FutureWarning" )
44
44
class TestPandasContainer :
45
- @pytest .fixture (scope = "function" , autouse = True )
46
- def setup (self , datapath ):
47
- self .dirpath = datapath ("io" , "json" , "data" )
48
-
49
- self .ts = tm .makeTimeSeries ()
50
- self .ts .name = "ts"
51
-
52
- self .series = tm .makeStringSeries ()
53
- self .series .name = "series"
54
-
55
- self .objSeries = tm .makeObjectSeries ()
56
- self .objSeries .name = "objects"
57
-
58
- self .empty_series = Series ([], index = [], dtype = np .float64 )
45
+ @pytest .fixture (autouse = True )
46
+ def setup (self ):
59
47
self .empty_frame = DataFrame ()
60
-
61
48
self .frame = _frame .copy ()
62
49
self .frame2 = _frame2 .copy ()
63
50
self .intframe = _intframe .copy ()
@@ -67,15 +54,6 @@ def setup(self, datapath):
67
54
68
55
yield
69
56
70
- del self .dirpath
71
-
72
- del self .ts
73
-
74
- del self .series
75
-
76
- del self .objSeries
77
-
78
- del self .empty_series
79
57
del self .empty_frame
80
58
81
59
del self .frame
@@ -457,7 +435,7 @@ def test_frame_mixedtype_orient(self): # GH10289
457
435
left = read_json (inp , orient = "values" , convert_axes = False )
458
436
tm .assert_frame_equal (left , right )
459
437
460
- def test_v12_compat (self ):
438
+ def test_v12_compat (self , datapath ):
461
439
df = DataFrame (
462
440
[
463
441
[1.56808523 , 0.65727391 , 1.81021139 , - 0.17251653 ],
@@ -474,12 +452,13 @@ def test_v12_compat(self):
474
452
df ["modified" ] = df ["date" ]
475
453
df .iloc [1 , df .columns .get_loc ("modified" )] = pd .NaT
476
454
477
- v12_json = os .path .join (self .dirpath , "tsframe_v012.json" )
455
+ dirpath = datapath ("io" , "json" , "data" )
456
+ v12_json = os .path .join (dirpath , "tsframe_v012.json" )
478
457
df_unser = pd .read_json (v12_json )
479
458
tm .assert_frame_equal (df , df_unser )
480
459
481
460
df_iso = df .drop (["modified" ], axis = 1 )
482
- v12_iso_json = os .path .join (self . dirpath , "tsframe_iso_v012.json" )
461
+ v12_iso_json = os .path .join (dirpath , "tsframe_iso_v012.json" )
483
462
df_unser_iso = pd .read_json (v12_iso_json )
484
463
tm .assert_frame_equal (df_iso , df_unser_iso )
485
464
@@ -633,15 +612,15 @@ def test_series_non_unique_index(self):
633
612
unser = read_json (s .to_json (orient = "records" ), orient = "records" , typ = "series" )
634
613
tm .assert_numpy_array_equal (s .values , unser .values )
635
614
636
- def test_series_default_orient (self ):
637
- assert self . series . to_json () == self . series .to_json (orient = "index" )
615
+ def test_series_default_orient (self , string_series ):
616
+ assert string_series . to_json () == string_series .to_json (orient = "index" )
638
617
639
618
@pytest .mark .parametrize ("numpy" , [True , False ])
640
- def test_series_roundtrip_simple (self , orient , numpy ):
641
- data = self . series .to_json (orient = orient )
619
+ def test_series_roundtrip_simple (self , orient , numpy , string_series ):
620
+ data = string_series .to_json (orient = orient )
642
621
result = pd .read_json (data , typ = "series" , orient = orient , numpy = numpy )
643
- expected = self .series .copy ()
644
622
623
+ expected = string_series
645
624
if orient in ("values" , "records" ):
646
625
expected = expected .reset_index (drop = True )
647
626
if orient != "split" :
@@ -651,13 +630,13 @@ def test_series_roundtrip_simple(self, orient, numpy):
651
630
652
631
@pytest .mark .parametrize ("dtype" , [False , None ])
653
632
@pytest .mark .parametrize ("numpy" , [True , False ])
654
- def test_series_roundtrip_object (self , orient , numpy , dtype ):
655
- data = self . objSeries .to_json (orient = orient )
633
+ def test_series_roundtrip_object (self , orient , numpy , dtype , object_series ):
634
+ data = object_series .to_json (orient = orient )
656
635
result = pd .read_json (
657
636
data , typ = "series" , orient = orient , numpy = numpy , dtype = dtype
658
637
)
659
- expected = self .objSeries .copy ()
660
638
639
+ expected = object_series
661
640
if orient in ("values" , "records" ):
662
641
expected = expected .reset_index (drop = True )
663
642
if orient != "split" :
@@ -666,12 +645,11 @@ def test_series_roundtrip_object(self, orient, numpy, dtype):
666
645
tm .assert_series_equal (result , expected )
667
646
668
647
@pytest .mark .parametrize ("numpy" , [True , False ])
669
- def test_series_roundtrip_empty (self , orient , numpy ):
670
- data = self . empty_series .to_json (orient = orient )
648
+ def test_series_roundtrip_empty (self , orient , numpy , empty_series ):
649
+ data = empty_series .to_json (orient = orient )
671
650
result = pd .read_json (data , typ = "series" , orient = orient , numpy = numpy )
672
- expected = self .empty_series .copy ()
673
651
674
- # TODO: see what causes inconsistency
652
+ expected = empty_series
675
653
if orient in ("values" , "records" ):
676
654
expected = expected .reset_index (drop = True )
677
655
else :
@@ -680,11 +658,11 @@ def test_series_roundtrip_empty(self, orient, numpy):
680
658
tm .assert_series_equal (result , expected )
681
659
682
660
@pytest .mark .parametrize ("numpy" , [True , False ])
683
- def test_series_roundtrip_timeseries (self , orient , numpy ):
684
- data = self . ts .to_json (orient = orient )
661
+ def test_series_roundtrip_timeseries (self , orient , numpy , datetime_series ):
662
+ data = datetime_series .to_json (orient = orient )
685
663
result = pd .read_json (data , typ = "series" , orient = orient , numpy = numpy )
686
- expected = self .ts .copy ()
687
664
665
+ expected = datetime_series
688
666
if orient in ("values" , "records" ):
689
667
expected = expected .reset_index (drop = True )
690
668
if orient != "split" :
@@ -772,20 +750,20 @@ def test_path(self):
772
750
df .to_json (path )
773
751
read_json (path )
774
752
775
- def test_axis_dates (self ):
753
+ def test_axis_dates (self , datetime_series ):
776
754
777
755
# frame
778
756
json = self .tsframe .to_json ()
779
757
result = read_json (json )
780
758
tm .assert_frame_equal (result , self .tsframe )
781
759
782
760
# series
783
- json = self . ts .to_json ()
761
+ json = datetime_series .to_json ()
784
762
result = read_json (json , typ = "series" )
785
- tm .assert_series_equal (result , self . ts , check_names = False )
763
+ tm .assert_series_equal (result , datetime_series , check_names = False )
786
764
assert result .name is None
787
765
788
- def test_convert_dates (self ):
766
+ def test_convert_dates (self , datetime_series ):
789
767
790
768
# frame
791
769
df = self .tsframe .copy ()
@@ -805,7 +783,7 @@ def test_convert_dates(self):
805
783
tm .assert_frame_equal (result , expected )
806
784
807
785
# series
808
- ts = Series (Timestamp ("20130101" ), index = self . ts .index )
786
+ ts = Series (Timestamp ("20130101" ), index = datetime_series .index )
809
787
json = ts .to_json ()
810
788
result = read_json (json , typ = "series" )
811
789
tm .assert_series_equal (result , ts )
@@ -900,8 +878,8 @@ def test_date_format_frame_raises(self):
900
878
("20130101 20:43:42.123456789" , "ns" ),
901
879
],
902
880
)
903
- def test_date_format_series (self , date , date_unit ):
904
- ts = Series (Timestamp (date ), index = self . ts .index )
881
+ def test_date_format_series (self , date , date_unit , datetime_series ):
882
+ ts = Series (Timestamp (date ), index = datetime_series .index )
905
883
ts .iloc [1 ] = pd .NaT
906
884
ts .iloc [5 ] = pd .NaT
907
885
if date_unit :
@@ -914,8 +892,8 @@ def test_date_format_series(self, date, date_unit):
914
892
expected = expected .dt .tz_localize ("UTC" )
915
893
tm .assert_series_equal (result , expected )
916
894
917
- def test_date_format_series_raises (self ):
918
- ts = Series (Timestamp ("20130101 20:43:42.123" ), index = self . ts .index )
895
+ def test_date_format_series_raises (self , datetime_series ):
896
+ ts = Series (Timestamp ("20130101 20:43:42.123" ), index = datetime_series .index )
919
897
msg = "Invalid value 'foo' for option 'date_unit'"
920
898
with pytest .raises (ValueError , match = msg ):
921
899
ts .to_json (date_format = "iso" , date_unit = "foo" )
0 commit comments