Skip to content

More Json parametrize #33148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,11 @@ def index_or_series_obj(request):
# ----------------------------------------------------------------
# DataFrames
# ----------------------------------------------------------------
@pytest.fixture
def empty_frame():
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is scattered across a few modules right now that I didn't touch here; can use further here or in a follow up

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

followup is fine.

return DataFrame()


@pytest.fixture
def float_frame():
"""
Expand Down
29 changes: 10 additions & 19 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
_tsd = tm.getTimeSeriesData()

_frame = DataFrame(_seriesd)
_frame2 = DataFrame(_seriesd, columns=["D", "C", "B", "A"])
_intframe = DataFrame({k: v.astype(np.int64) for k, v in _seriesd.items()})

_tsframe = DataFrame(_tsd)
Expand All @@ -44,20 +43,13 @@ def assert_json_roundtrip_equal(result, expected, orient):
class TestPandasContainer:
@pytest.fixture(autouse=True)
def setup(self):
self.empty_frame = DataFrame()
self.frame = _frame.copy()
self.frame2 = _frame2.copy()
self.intframe = _intframe.copy()
self.tsframe = _tsframe.copy()
self.mixed_frame = _mixed_frame.copy()
self.categorical = _cat_frame.copy()

yield

del self.empty_frame

del self.frame
del self.frame2
del self.intframe
del self.tsframe
del self.mixed_frame
Expand Down Expand Up @@ -126,19 +118,19 @@ def test_frame_non_unique_columns_raises(self, orient):
with pytest.raises(ValueError, match=msg):
df.to_json(orient=orient)

def test_frame_default_orient(self):
assert self.frame.to_json() == self.frame.to_json(orient="columns")
def test_frame_default_orient(self, float_frame):
assert float_frame.to_json() == float_frame.to_json(orient="columns")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that float_frame and self.frame aren't 100% equivalent because the former has the columns labeled ABCD whereas the latter didn't provide labels. I don't think that distinction matters for these tests though


@pytest.mark.parametrize("dtype", [False, float])
@pytest.mark.parametrize("convert_axes", [True, False])
@pytest.mark.parametrize("numpy", [True, False])
def test_roundtrip_simple(self, orient, convert_axes, numpy, dtype):
data = self.frame.to_json(orient=orient)
def test_roundtrip_simple(self, orient, convert_axes, numpy, dtype, float_frame):
data = float_frame.to_json(orient=orient)
result = pd.read_json(
data, orient=orient, convert_axes=convert_axes, numpy=numpy, dtype=dtype
)

expected = self.frame.copy()
expected = float_frame

assert_json_roundtrip_equal(result, expected, orient)

Expand Down Expand Up @@ -226,12 +218,12 @@ def test_roundtrip_categorical(self, orient, convert_axes, numpy):

@pytest.mark.parametrize("convert_axes", [True, False])
@pytest.mark.parametrize("numpy", [True, False])
def test_roundtrip_empty(self, orient, convert_axes, numpy):
data = self.empty_frame.to_json(orient=orient)
def test_roundtrip_empty(self, orient, convert_axes, numpy, empty_frame):
data = empty_frame.to_json(orient=orient)
result = pd.read_json(
data, orient=orient, convert_axes=convert_axes, numpy=numpy
)
expected = self.empty_frame.copy()
expected = empty_frame.copy()

# TODO: both conditions below are probably bugs
if convert_axes:
Expand Down Expand Up @@ -738,11 +730,10 @@ def test_reconstruction_index(self):
result = read_json(df.to_json())
tm.assert_frame_equal(result, df)

def test_path(self):
def test_path(self, float_frame):
with tm.ensure_clean("test.json") as path:
for df in [
self.frame,
self.frame2,
float_frame,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to above comment, self.frame and self.frame2 are actually different in that the former uses default column labels whereas the latter used ABCD; I didn't think that distinction was worth creating a separate fixture or object within this test

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, but this is a historical accident, having a smaller set of common fixtures is better

self.intframe,
self.tsframe,
self.mixed_frame,
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/resample/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def frame(index, _series_name, _static_values):


@pytest.fixture
def empty_frame(series):
def empty_frame_dti(series):
"""
Fixture for parametrization of empty DataFrame with date_range,
period_range and timedelta_range indexes
Expand Down
22 changes: 10 additions & 12 deletions pandas/tests/resample/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ def test_resample_count_empty_series(freq, empty_series_dti, resample_method):

@all_ts
@pytest.mark.parametrize("freq", ["M", "D", "H"])
def test_resample_empty_dataframe(empty_frame, freq, resample_method):
def test_resample_empty_dataframe(empty_frame_dti, freq, resample_method):
# GH13212
df = empty_frame
df = empty_frame_dti
# count retains dimensions too
result = getattr(df.resample(freq), resample_method)()
if resample_method != "size":
Expand All @@ -149,15 +149,14 @@ def test_resample_empty_dataframe(empty_frame, freq, resample_method):

@all_ts
@pytest.mark.parametrize("freq", ["M", "D", "H"])
def test_resample_count_empty_dataframe(freq, empty_frame):
def test_resample_count_empty_dataframe(freq, empty_frame_dti):
# GH28427

empty_frame = empty_frame.copy()
empty_frame["a"] = []
empty_frame_dti["a"] = []

result = empty_frame.resample(freq).count()
result = empty_frame_dti.resample(freq).count()

index = _asfreq_compat(empty_frame.index, freq)
index = _asfreq_compat(empty_frame_dti.index, freq)

expected = pd.DataFrame({"a": []}, dtype="int64", index=index)

Expand All @@ -166,15 +165,14 @@ def test_resample_count_empty_dataframe(freq, empty_frame):

@all_ts
@pytest.mark.parametrize("freq", ["M", "D", "H"])
def test_resample_size_empty_dataframe(freq, empty_frame):
def test_resample_size_empty_dataframe(freq, empty_frame_dti):
# GH28427

empty_frame = empty_frame.copy()
empty_frame["a"] = []
empty_frame_dti["a"] = []

result = empty_frame.resample(freq).size()
result = empty_frame_dti.resample(freq).size()

index = _asfreq_compat(empty_frame.index, freq)
index = _asfreq_compat(empty_frame_dti.index, freq)

expected = pd.Series([], dtype="int64", index=index)

Expand Down