Skip to content
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

TST/CLN: Remove makeDataFrame #56210

Merged
merged 3 commits into from
Nov 28, 2023
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
6 changes: 5 additions & 1 deletion pandas/tests/frame/methods/test_set_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ def test_set_index(self, float_string_frame):
df.set_index(idx[::2])

def test_set_index_names(self):
df = tm.makeDataFrame()
df = DataFrame(
np.ones((10, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(10)], dtype=object),
)
df.index.name = "name"

assert df.set_index(df.index).index.names == ["name"]
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,10 @@ def test_strings_to_numbers_comparisons_raises(self, compare_operators_no_eq_ne)
f(df, 0)

def test_comparison_protected_from_errstate(self):
missing_df = tm.makeDataFrame()
missing_df = DataFrame(
np.ones((10, 4), dtype=np.float64),
columns=Index(list("ABCD"), dtype=object),
)
missing_df.loc[missing_df.index[0], "A"] = np.nan
with np.errstate(invalid="ignore"):
expected = missing_df.values < 0
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/indexing/test_chaining_and_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import pandas as pd
from pandas import (
DataFrame,
Index,
Series,
Timestamp,
date_range,
Expand Down Expand Up @@ -627,7 +628,10 @@ def test_chained_getitem_with_lists(self):
def test_cache_updating(self):
# GH 4939, make sure to update the cache on setitem

df = tm.makeDataFrame()
df = DataFrame(
np.zeros((10, 4)),
columns=Index(list("ABCD"), dtype=object),
)
df["A"] # cache series
df.loc["Hello Friend"] = df.iloc[0]
assert "Hello Friend" in df["A"].index
Expand Down
12 changes: 10 additions & 2 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1234,15 +1234,23 @@ def test_freeze_panes(self, path):
tm.assert_frame_equal(result, expected)

def test_path_path_lib(self, engine, ext):
df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)
writer = partial(df.to_excel, engine=engine)

reader = partial(pd.read_excel, index_col=0)
result = tm.round_trip_pathlib(writer, reader, path=f"foo{ext}")
tm.assert_frame_equal(result, df)

def test_path_local_path(self, engine, ext):
df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)
writer = partial(df.to_excel, engine=engine)

reader = partial(pd.read_excel, index_col=0)
Expand Down
21 changes: 17 additions & 4 deletions pandas/tests/io/formats/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pandas as pd
from pandas import (
DataFrame,
Index,
compat,
)
import pandas._testing as tm
Expand Down Expand Up @@ -665,7 +666,7 @@ def test_na_rep_truncated(self):
def test_to_csv_errors(self, errors):
# GH 22610
data = ["\ud800foo"]
ser = pd.Series(data, index=pd.Index(data))
ser = pd.Series(data, index=Index(data))
with tm.ensure_clean("test.csv") as path:
ser.to_csv(path, errors=errors)
# No use in reading back the data as it is not the same anymore
Expand All @@ -679,7 +680,11 @@ def test_to_csv_binary_handle(self, mode):

GH 35058 and GH 19827
"""
df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)
with tm.ensure_clean() as path:
with open(path, mode="w+b") as handle:
df.to_csv(handle, mode=mode)
Expand Down Expand Up @@ -713,7 +718,11 @@ def test_to_csv_encoding_binary_handle(self, mode):

def test_to_csv_iterative_compression_name(compression):
# GH 38714
df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)
with tm.ensure_clean() as path:
df.to_csv(path, compression=compression, chunksize=1)
tm.assert_frame_equal(
Expand All @@ -723,7 +732,11 @@ def test_to_csv_iterative_compression_name(compression):

def test_to_csv_iterative_compression_buffer(compression):
# GH 38714
df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)
with io.BytesIO() as buffer:
df.to_csv(buffer, compression=compression, chunksize=1)
buffer.seek(0)
Expand Down
18 changes: 15 additions & 3 deletions pandas/tests/io/parser/common/test_file_buffer_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from urllib.error import URLError
import uuid

import numpy as np
import pytest

from pandas.errors import (
Expand All @@ -19,7 +20,10 @@
)
import pandas.util._test_decorators as td

from pandas import DataFrame
from pandas import (
DataFrame,
Index,
)
import pandas._testing as tm

pytestmark = pytest.mark.filterwarnings(
Expand Down Expand Up @@ -66,15 +70,23 @@ def test_local_file(all_parsers, csv_dir_path):
@xfail_pyarrow # AssertionError: DataFrame.index are different
def test_path_path_lib(all_parsers):
parser = all_parsers
df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)
result = tm.round_trip_pathlib(df.to_csv, lambda p: parser.read_csv(p, index_col=0))
tm.assert_frame_equal(df, result)


@xfail_pyarrow # AssertionError: DataFrame.index are different
def test_path_local_path(all_parsers):
parser = all_parsers
df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)
result = tm.round_trip_localpath(
df.to_csv, lambda p: parser.read_csv(p, index_col=0)
)
Expand Down
33 changes: 27 additions & 6 deletions pandas/tests/io/pytables/test_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pandas as pd
from pandas import (
DataFrame,
Index,
Series,
_testing as tm,
concat,
Expand Down Expand Up @@ -401,7 +402,7 @@ def check_col(key, name, size):
{
"A": [0.0, 1.0, 2.0, 3.0, 4.0],
"B": [0.0, 1.0, 0.0, 1.0, 0.0],
"C": pd.Index(["foo1", "foo2", "foo3", "foo4", "foo5"], dtype=object),
"C": Index(["foo1", "foo2", "foo3", "foo4", "foo5"], dtype=object),
"D": date_range("20130101", periods=5),
}
).set_index("C")
Expand Down Expand Up @@ -658,7 +659,11 @@ def test_append_hierarchical(tmp_path, setup_path, multiindex_dataframe_random_d

def test_append_misc(setup_path):
with ensure_clean_store(setup_path) as store:
df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)
store.append("df", df, chunksize=1)
result = store.select("df")
tm.assert_frame_equal(result, df)
Expand All @@ -671,7 +676,11 @@ def test_append_misc(setup_path):
@pytest.mark.parametrize("chunksize", [10, 200, 1000])
def test_append_misc_chunksize(setup_path, chunksize):
# more chunksize in append tests
df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)
df["string"] = "foo"
df["float322"] = 1.0
df["float322"] = df["float322"].astype("float32")
Expand Down Expand Up @@ -715,7 +724,11 @@ def test_append_raise(setup_path):
# test append with invalid input to get good error messages

# list in column
df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)
df["invalid"] = [["a"]] * len(df)
assert df.dtypes["invalid"] == np.object_
msg = re.escape(
Expand All @@ -732,7 +745,11 @@ def test_append_raise(setup_path):
store.append("df", df)

# datetime with embedded nans as object
df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)
s = Series(datetime.datetime(2001, 1, 2), index=df.index)
s = s.astype(object)
s[0:5] = np.nan
Expand All @@ -756,7 +773,11 @@ def test_append_raise(setup_path):
store.append("df", Series(np.arange(10)))

# appending an incompatible table
df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)
store.append("df", df)

df["foo"] = "foo"
Expand Down
19 changes: 16 additions & 3 deletions pandas/tests/io/pytables/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
CategoricalIndex,
DataFrame,
HDFStore,
Index,
MultiIndex,
_testing as tm,
date_range,
Expand All @@ -25,7 +26,11 @@


def test_pass_spec_to_storer(setup_path):
df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)

with ensure_clean_store(setup_path) as store:
store.put("df", df)
Expand Down Expand Up @@ -60,14 +65,22 @@ def test_unimplemented_dtypes_table_columns(setup_path):

# currently not supported dtypes ####
for n, f in dtypes:
df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)
df[n] = f
msg = re.escape(f"[{n}] is not implemented as a table column")
with pytest.raises(TypeError, match=msg):
store.append(f"df1_{n}", df)

# frame
df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)
df["obj1"] = "foo"
df["obj2"] = "bar"
df["datetime1"] = datetime.date(2001, 1, 2)
Expand Down
31 changes: 26 additions & 5 deletions pandas/tests/io/pytables/test_file_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from pandas import (
DataFrame,
HDFStore,
Index,
Series,
_testing as tm,
read_hdf,
Expand Down Expand Up @@ -145,7 +146,11 @@ def test_reopen_handle(tmp_path, setup_path):

def test_open_args(setup_path):
with tm.ensure_clean(setup_path) as path:
df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)

# create an in memory store
store = HDFStore(
Expand All @@ -172,7 +177,11 @@ def test_flush(setup_path):

def test_complibs_default_settings(tmp_path, setup_path):
# GH15943
df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)

# Set complevel and check if complib is automatically set to
# default value
Expand Down Expand Up @@ -211,7 +220,11 @@ def test_complibs_default_settings(tmp_path, setup_path):

def test_complibs_default_settings_override(tmp_path, setup_path):
# Check if file-defaults can be overridden on a per table basis
df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)
tmpfile = tmp_path / setup_path
store = HDFStore(tmpfile)
store.append("dfc", df, complevel=9, complib="blosc")
Expand Down Expand Up @@ -325,7 +338,11 @@ def test_multiple_open_close(tmp_path, setup_path):

path = tmp_path / setup_path

df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)
df.to_hdf(path, key="df", mode="w", format="table")

# single
Expand Down Expand Up @@ -402,7 +419,11 @@ def test_multiple_open_close(tmp_path, setup_path):
# ops on a closed store
path = tmp_path / setup_path

df = tm.makeDataFrame()
df = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)
df.to_hdf(path, key="df", mode="w", format="table")

store = HDFStore(path)
Expand Down
8 changes: 7 additions & 1 deletion pandas/tests/io/pytables/test_keys.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import numpy as np
import pytest

from pandas import (
DataFrame,
HDFStore,
Index,
Series,
_testing as tm,
)
Expand All @@ -20,7 +22,11 @@ def test_keys(setup_path):
store["b"] = Series(
range(10), dtype="float64", index=[f"i_{i}" for i in range(10)]
)
store["c"] = tm.makeDataFrame()
store["c"] = DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
columns=Index(list("ABCD"), dtype=object),
index=Index([f"i-{i}" for i in range(30)], dtype=object),
)

assert len(store) == 3
expected = {"/a", "/b", "/c"}
Expand Down
Loading
Loading