Skip to content

Commit 92cf475

Browse files
WillAydjbrockmendel
authored andcommitted
Fixturize JSON tests (pandas-dev#31191)
1 parent 628513e commit 92cf475

File tree

4 files changed

+65
-62
lines changed

4 files changed

+65
-62
lines changed

pandas/conftest.py

+25
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,11 @@ def tick_classes(request):
878878
)
879879

880880

881+
@pytest.fixture
882+
def empty_series():
883+
return pd.Series([], index=[], dtype=np.float64)
884+
885+
881886
@pytest.fixture
882887
def datetime_series():
883888
"""
@@ -888,6 +893,26 @@ def datetime_series():
888893
return s
889894

890895

896+
@pytest.fixture
897+
def string_series():
898+
"""
899+
Fixture for Series of floats with Index of unique strings
900+
"""
901+
s = tm.makeStringSeries()
902+
s.name = "series"
903+
return s
904+
905+
906+
@pytest.fixture
907+
def object_series():
908+
"""
909+
Fixture for Series of dtype object with Index of unique strings
910+
"""
911+
s = tm.makeObjectSeries()
912+
s.name = "objects"
913+
return s
914+
915+
891916
@pytest.fixture
892917
def float_frame():
893918
"""

pandas/tests/io/json/test_pandas.py

+29-51
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,9 @@ def assert_json_roundtrip_equal(result, expected, orient):
4242

4343
@pytest.mark.filterwarnings("ignore:the 'numpy' keyword is deprecated:FutureWarning")
4444
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):
5947
self.empty_frame = DataFrame()
60-
6148
self.frame = _frame.copy()
6249
self.frame2 = _frame2.copy()
6350
self.intframe = _intframe.copy()
@@ -67,15 +54,6 @@ def setup(self, datapath):
6754

6855
yield
6956

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
7957
del self.empty_frame
8058

8159
del self.frame
@@ -457,7 +435,7 @@ def test_frame_mixedtype_orient(self): # GH10289
457435
left = read_json(inp, orient="values", convert_axes=False)
458436
tm.assert_frame_equal(left, right)
459437

460-
def test_v12_compat(self):
438+
def test_v12_compat(self, datapath):
461439
df = DataFrame(
462440
[
463441
[1.56808523, 0.65727391, 1.81021139, -0.17251653],
@@ -474,12 +452,13 @@ def test_v12_compat(self):
474452
df["modified"] = df["date"]
475453
df.iloc[1, df.columns.get_loc("modified")] = pd.NaT
476454

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")
478457
df_unser = pd.read_json(v12_json)
479458
tm.assert_frame_equal(df, df_unser)
480459

481460
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")
483462
df_unser_iso = pd.read_json(v12_iso_json)
484463
tm.assert_frame_equal(df_iso, df_unser_iso)
485464

@@ -633,15 +612,15 @@ def test_series_non_unique_index(self):
633612
unser = read_json(s.to_json(orient="records"), orient="records", typ="series")
634613
tm.assert_numpy_array_equal(s.values, unser.values)
635614

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")
638617

639618
@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)
642621
result = pd.read_json(data, typ="series", orient=orient, numpy=numpy)
643-
expected = self.series.copy()
644622

623+
expected = string_series
645624
if orient in ("values", "records"):
646625
expected = expected.reset_index(drop=True)
647626
if orient != "split":
@@ -651,13 +630,13 @@ def test_series_roundtrip_simple(self, orient, numpy):
651630

652631
@pytest.mark.parametrize("dtype", [False, None])
653632
@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)
656635
result = pd.read_json(
657636
data, typ="series", orient=orient, numpy=numpy, dtype=dtype
658637
)
659-
expected = self.objSeries.copy()
660638

639+
expected = object_series
661640
if orient in ("values", "records"):
662641
expected = expected.reset_index(drop=True)
663642
if orient != "split":
@@ -666,12 +645,11 @@ def test_series_roundtrip_object(self, orient, numpy, dtype):
666645
tm.assert_series_equal(result, expected)
667646

668647
@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)
671650
result = pd.read_json(data, typ="series", orient=orient, numpy=numpy)
672-
expected = self.empty_series.copy()
673651

674-
# TODO: see what causes inconsistency
652+
expected = empty_series
675653
if orient in ("values", "records"):
676654
expected = expected.reset_index(drop=True)
677655
else:
@@ -680,11 +658,11 @@ def test_series_roundtrip_empty(self, orient, numpy):
680658
tm.assert_series_equal(result, expected)
681659

682660
@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)
685663
result = pd.read_json(data, typ="series", orient=orient, numpy=numpy)
686-
expected = self.ts.copy()
687664

665+
expected = datetime_series
688666
if orient in ("values", "records"):
689667
expected = expected.reset_index(drop=True)
690668
if orient != "split":
@@ -772,20 +750,20 @@ def test_path(self):
772750
df.to_json(path)
773751
read_json(path)
774752

775-
def test_axis_dates(self):
753+
def test_axis_dates(self, datetime_series):
776754

777755
# frame
778756
json = self.tsframe.to_json()
779757
result = read_json(json)
780758
tm.assert_frame_equal(result, self.tsframe)
781759

782760
# series
783-
json = self.ts.to_json()
761+
json = datetime_series.to_json()
784762
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)
786764
assert result.name is None
787765

788-
def test_convert_dates(self):
766+
def test_convert_dates(self, datetime_series):
789767

790768
# frame
791769
df = self.tsframe.copy()
@@ -805,7 +783,7 @@ def test_convert_dates(self):
805783
tm.assert_frame_equal(result, expected)
806784

807785
# series
808-
ts = Series(Timestamp("20130101"), index=self.ts.index)
786+
ts = Series(Timestamp("20130101"), index=datetime_series.index)
809787
json = ts.to_json()
810788
result = read_json(json, typ="series")
811789
tm.assert_series_equal(result, ts)
@@ -900,8 +878,8 @@ def test_date_format_frame_raises(self):
900878
("20130101 20:43:42.123456789", "ns"),
901879
],
902880
)
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)
905883
ts.iloc[1] = pd.NaT
906884
ts.iloc[5] = pd.NaT
907885
if date_unit:
@@ -914,8 +892,8 @@ def test_date_format_series(self, date, date_unit):
914892
expected = expected.dt.tz_localize("UTC")
915893
tm.assert_series_equal(result, expected)
916894

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)
919897
msg = "Invalid value 'foo' for option 'date_unit'"
920898
with pytest.raises(ValueError, match=msg):
921899
ts.to_json(date_format="iso", date_unit="foo")

pandas/tests/resample/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def series(index, _series_name, _static_values):
134134

135135

136136
@pytest.fixture
137-
def empty_series(series):
137+
def empty_series_dti(series):
138138
"""
139139
Fixture for parametrization of empty Series with date_range,
140140
period_range and timedelta_range indexes

pandas/tests/resample/test_base.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ def test_raises_on_non_datetimelike_index():
9494

9595
@all_ts
9696
@pytest.mark.parametrize("freq", ["M", "D", "H"])
97-
def test_resample_empty_series(freq, empty_series, resample_method):
97+
def test_resample_empty_series(freq, empty_series_dti, resample_method):
9898
# GH12771 & GH12868
9999

100100
if resample_method == "ohlc":
101101
pytest.skip("need to test for ohlc from GH13083")
102102

103-
s = empty_series
103+
s = empty_series_dti
104104
result = getattr(s.resample(freq), resample_method)()
105105

106106
expected = s.copy()
@@ -114,13 +114,13 @@ def test_resample_empty_series(freq, empty_series, resample_method):
114114
@all_ts
115115
@pytest.mark.parametrize("freq", ["M", "D", "H"])
116116
@pytest.mark.parametrize("resample_method", ["count", "size"])
117-
def test_resample_count_empty_series(freq, empty_series, resample_method):
117+
def test_resample_count_empty_series(freq, empty_series_dti, resample_method):
118118
# GH28427
119-
result = getattr(empty_series.resample(freq), resample_method)()
119+
result = getattr(empty_series_dti.resample(freq), resample_method)()
120120

121-
index = _asfreq_compat(empty_series.index, freq)
121+
index = _asfreq_compat(empty_series_dti.index, freq)
122122

123-
expected = pd.Series([], dtype="int64", index=index, name=empty_series.name)
123+
expected = pd.Series([], dtype="int64", index=index, name=empty_series_dti.name)
124124

125125
tm.assert_series_equal(result, expected)
126126

@@ -188,9 +188,9 @@ def test_resample_empty_dtypes(index, dtype, resample_method):
188188
# Empty series were sometimes causing a segfault (for the functions
189189
# with Cython bounds-checking disabled) or an IndexError. We just run
190190
# them to ensure they no longer do. (GH #10228)
191-
empty_series = Series([], index, dtype)
191+
empty_series_dti = Series([], index, dtype)
192192
try:
193-
getattr(empty_series.resample("d"), resample_method)()
193+
getattr(empty_series_dti.resample("d"), resample_method)()
194194
except DataError:
195195
# Ignore these since some combinations are invalid
196196
# (ex: doing mean with dtype of np.object)
@@ -227,9 +227,9 @@ def test_resample_loffset_arg_type(frame, create_index, arg):
227227

228228

229229
@all_ts
230-
def test_apply_to_empty_series(empty_series):
230+
def test_apply_to_empty_series(empty_series_dti):
231231
# GH 14313
232-
s = empty_series
232+
s = empty_series_dti
233233
for freq in ["M", "D", "H"]:
234234
result = s.resample(freq).apply(lambda x: 1)
235235
expected = s.resample(freq).apply(np.sum)

0 commit comments

Comments
 (0)