Skip to content

TST/REF: collect tests from test_api into method-specific files #37525

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 8 commits into from
Oct 31, 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
43 changes: 43 additions & 0 deletions pandas/tests/frame/methods/test_copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pytest

from pandas import DataFrame
import pandas._testing as tm


class TestCopy:
@pytest.mark.parametrize("attr", ["index", "columns"])
def test_copy_index_name_checking(self, float_frame, attr):
# don't want to be able to modify the index stored elsewhere after
# making a copy
ind = getattr(float_frame, attr)
ind.name = None
cp = float_frame.copy()
getattr(cp, attr).name = "foo"
assert getattr(float_frame, attr).name is None

def test_copy_cache(self):
# GH#31784 _item_cache not cleared on copy causes incorrect reads after updates
df = DataFrame({"a": [1]})

df["x"] = [0]
df["a"]

df.copy()

df["a"].values[0] = -1

tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0]}))

df["y"] = [0]

assert df["a"].values[0] == -1
tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0], "y": [0]}))

def test_copy(self, float_frame, float_string_frame):
cop = float_frame.copy()
cop["E"] = cop["A"]
assert "E" not in float_frame

# copy objects
copy = float_string_frame.copy()
assert copy._mgr is not float_string_frame._mgr
32 changes: 32 additions & 0 deletions pandas/tests/frame/methods/test_to_numpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import numpy as np
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't these be with test_values.py

Copy link
Member Author

Choose a reason for hiding this comment

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

these tests are for to_numpy. yes the methods are related, but this is the best way to keep the "where are tests for X" unambiguous

Copy link
Contributor

Choose a reason for hiding this comment

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

ok


from pandas import DataFrame, Timestamp
import pandas._testing as tm


class TestToNumpy:
def test_to_numpy(self):
df = DataFrame({"A": [1, 2], "B": [3, 4.5]})
expected = np.array([[1, 3], [2, 4.5]])
result = df.to_numpy()
tm.assert_numpy_array_equal(result, expected)

def test_to_numpy_dtype(self):
df = DataFrame({"A": [1, 2], "B": [3, 4.5]})
expected = np.array([[1, 3], [2, 4]], dtype="int64")
result = df.to_numpy(dtype="int64")
tm.assert_numpy_array_equal(result, expected)

def test_to_numpy_copy(self):
arr = np.random.randn(4, 3)
df = DataFrame(arr)
assert df.values.base is arr
assert df.to_numpy(copy=False).base is arr
assert df.to_numpy(copy=True).base is not arr

def test_to_numpy_mixed_dtype_to_str(self):
# https://github.com/pandas-dev/pandas/issues/35455
df = DataFrame([[Timestamp("2020-01-01 00:00:00"), 100.0]])
result = df.to_numpy(dtype=str)
expected = np.array([["2020-01-01 00:00:00", "100.0"]], dtype=str)
tm.assert_numpy_array_equal(result, expected)
68 changes: 2 additions & 66 deletions pandas/tests/frame/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@


class TestDataFrameMisc:
@pytest.mark.parametrize("attr", ["index", "columns"])
def test_copy_index_name_checking(self, float_frame, attr):
# don't want to be able to modify the index stored elsewhere after
# making a copy
ind = getattr(float_frame, attr)
ind.name = None
cp = float_frame.copy()
getattr(cp, attr).name = "foo"
assert getattr(float_frame, attr).name is None

def test_getitem_pop_assign_name(self, float_frame):
s = float_frame["A"]
assert s.name == "A"
Expand Down Expand Up @@ -86,8 +76,7 @@ def test_get_axis(self, float_frame):
f._get_axis_number(None)

def test_keys(self, float_frame):
getkeys = float_frame.keys
assert getkeys() is float_frame.columns
assert float_frame.keys() is float_frame.columns

def test_column_contains_raises(self, float_frame):
with pytest.raises(TypeError, match="unhashable type: 'Index'"):
Expand Down Expand Up @@ -136,15 +125,6 @@ def test_new_empty_index(self):
df1.index.name = "foo"
assert df2.index.name is None

def test_array_interface(self, float_frame):
with np.errstate(all="ignore"):
result = np.sqrt(float_frame)
assert isinstance(result, type(float_frame))
assert result.index is float_frame.index
assert result.columns is float_frame.columns

tm.assert_frame_equal(result, float_frame.apply(np.sqrt))

def test_get_agg_axis(self, float_frame):
cols = float_frame._get_agg_axis(0)
assert cols is float_frame.columns
Expand All @@ -156,7 +136,7 @@ def test_get_agg_axis(self, float_frame):
with pytest.raises(ValueError, match=msg):
float_frame._get_agg_axis(2)

def test_nonzero(self, float_frame, float_string_frame):
def test_empty(self, float_frame, float_string_frame):
empty_frame = DataFrame()
assert empty_frame.empty

Expand Down Expand Up @@ -313,32 +293,6 @@ def test_len(self, float_frame):
expected = float_frame.reindex(columns=["A", "B"]).values
tm.assert_almost_equal(arr, expected)

def test_to_numpy(self):
df = DataFrame({"A": [1, 2], "B": [3, 4.5]})
expected = np.array([[1, 3], [2, 4.5]])
result = df.to_numpy()
tm.assert_numpy_array_equal(result, expected)

def test_to_numpy_dtype(self):
df = DataFrame({"A": [1, 2], "B": [3, 4.5]})
expected = np.array([[1, 3], [2, 4]], dtype="int64")
result = df.to_numpy(dtype="int64")
tm.assert_numpy_array_equal(result, expected)

def test_to_numpy_copy(self):
arr = np.random.randn(4, 3)
df = DataFrame(arr)
assert df.values.base is arr
assert df.to_numpy(copy=False).base is arr
assert df.to_numpy(copy=True).base is not arr

def test_to_numpy_mixed_dtype_to_str(self):
# https://github.com/pandas-dev/pandas/issues/35455
df = DataFrame([[pd.Timestamp("2020-01-01 00:00:00"), 100.0]])
result = df.to_numpy(dtype=str)
expected = np.array([["2020-01-01 00:00:00", "100.0"]], dtype=str)
tm.assert_numpy_array_equal(result, expected)

def test_swapaxes(self):
df = DataFrame(np.random.randn(10, 5))
tm.assert_frame_equal(df.T, df.swapaxes(0, 1))
Expand Down Expand Up @@ -537,24 +491,6 @@ def test_set_flags(self, allows_duplicate_labels):
result.iloc[0, 0] = 10
assert df.iloc[0, 0] == 0

def test_cache_on_copy(self):
# GH 31784 _item_cache not cleared on copy causes incorrect reads after updates
df = DataFrame({"a": [1]})

df["x"] = [0]
df["a"]

df.copy()

df["a"].values[0] = -1

tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0]}))

df["y"] = [0]

assert df["a"].values[0] == -1
tm.assert_frame_equal(df, DataFrame({"a": [-1], "x": [0], "y": [0]}))

@skip_if_no("jinja2")
def test_constructor_expanddim_lookup(self):
# GH#33628 accessing _constructor_expanddim should not
Expand Down
9 changes: 0 additions & 9 deletions pandas/tests/frame/test_block_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,6 @@ def f(dtype):
if not compat.is_platform_windows():
f("M8[ns]")

def test_copy(self, float_frame, float_string_frame):
cop = float_frame.copy()
cop["E"] = cop["A"]
assert "E" not in float_frame

# copy objects
copy = float_string_frame.copy()
assert copy._mgr is not float_string_frame._mgr

def test_pickle(self, float_string_frame, timezone_frame):
empty_frame = DataFrame()

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/frame/test_npfuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ def test_asarray_homogenous(self):
# may change from object in the future
expected = np.array([[1, 1], [2, 2]], dtype="object")
tm.assert_numpy_array_equal(result, expected)

def test_np_sqrt(self, float_frame):
with np.errstate(all="ignore"):
result = np.sqrt(float_frame)
assert isinstance(result, type(float_frame))
assert result.index is float_frame.index
assert result.columns is float_frame.columns

tm.assert_frame_equal(result, float_frame.apply(np.sqrt))
15 changes: 0 additions & 15 deletions pandas/tests/frame/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,3 @@ def test_boolean_compare_transpose_tzindex_with_dst(self, tz):
result = df.T == df.T
expected = DataFrame(True, index=list("ab"), columns=idx)
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("copy", [True, False])
@pytest.mark.parametrize(
"method, tz", [["tz_localize", None], ["tz_convert", "Europe/Berlin"]]
)
def test_tz_localize_convert_copy_inplace_mutate(self, copy, method, tz):
# GH 6326
result = DataFrame(
np.arange(0, 5), index=date_range("20131027", periods=5, freq="1H", tz=tz)
)
getattr(result, method)("UTC", copy=copy)
expected = DataFrame(
np.arange(0, 5), index=date_range("20131027", periods=5, freq="1H", tz=tz)
)
tm.assert_frame_equal(result, expected)
4 changes: 4 additions & 0 deletions pandas/tests/series/methods/test_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@


class TestSeriesAppend:
def test_append_preserve_name(self, datetime_series):
result = datetime_series[:5].append(datetime_series[5:])
assert result.name == datetime_series.name

def test_append(self, datetime_series, string_series, object_series):
appended_series = string_series.append(object_series)
for idx, value in appended_series.items():
Expand Down
13 changes: 1 addition & 12 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@


class TestSeriesMisc:
def test_append_preserve_name(self, datetime_series):
result = datetime_series[:5].append(datetime_series[5:])
assert result.name == datetime_series.name

def test_getitem_preserve_name(self, datetime_series):
result = datetime_series[datetime_series > 0]
assert result.name == datetime_series.name
Expand Down Expand Up @@ -163,10 +159,7 @@ def test_iter_strings(self, string_series):
assert val == string_series[i]

def test_keys(self, datetime_series):
# HACK: By doing this in two stages, we avoid 2to3 wrapping the call
# to .keys() in a list()
getkeys = datetime_series.keys
assert getkeys() is datetime_series.index
assert datetime_series.keys() is datetime_series.index

def test_values(self, datetime_series):
tm.assert_almost_equal(
Expand Down Expand Up @@ -213,10 +206,6 @@ def test_class_axis(self):
# no exception and no empty docstring
assert pydoc.getdoc(Series.index)

def test_numpy_unique(self, datetime_series):
# it works!
np.unique(datetime_series)

def test_item(self):
s = Series([1])
result = s.item()
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/series/test_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def test_nunique():
assert s.nunique() == 0


def test_numpy_unique(datetime_series):
# it works!
np.unique(datetime_series)


def test_unique():
# GH714 also, dtype=float
s = Series([1.2345] * 100)
Expand Down