Skip to content

TST/REF: collect tests by method from tests.internals #37420

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 5 commits into from
Oct 26, 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
23 changes: 23 additions & 0 deletions pandas/tests/frame/methods/test_equals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pandas import DataFrame
import pandas._testing as tm


class TestEquals:
def test_dataframe_not_equal(self):
# see GH#28839
df1 = DataFrame({"a": [1, 2], "b": ["s", "d"]})
df2 = DataFrame({"a": ["s", "d"], "b": [1, 2]})
assert df1.equals(df2) is False

def test_equals_different_blocks(self):
# GH#9330
df0 = DataFrame({"A": ["x", "y"], "B": [1, 2], "C": ["w", "z"]})
df1 = df0.reset_index()[["A", "B", "C"]]
# this assert verifies that the above operations have
# induced a block rearrangement
assert df0._mgr.blocks[0].dtype != df1._mgr.blocks[0].dtype

# do the real tests
tm.assert_frame_equal(df0, df1)
assert df0.equals(df1)
assert df1.equals(df0)
42 changes: 42 additions & 0 deletions pandas/tests/frame/methods/test_infer_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from datetime import datetime

from pandas import DataFrame
import pandas._testing as tm


class TestInferObjects:
def test_infer_objects(self):
# GH#11221
df = DataFrame(
{
"a": ["a", 1, 2, 3],
"b": ["b", 2.0, 3.0, 4.1],
"c": [
"c",
datetime(2016, 1, 1),
datetime(2016, 1, 2),
datetime(2016, 1, 3),
],
"d": [1, 2, 3, "d"],
},
columns=["a", "b", "c", "d"],
)
df = df.iloc[1:].infer_objects()

assert df["a"].dtype == "int64"
assert df["b"].dtype == "float64"
assert df["c"].dtype == "M8[ns]"
assert df["d"].dtype == "object"

expected = DataFrame(
{
"a": [1, 2, 3],
"b": [2.0, 3.0, 4.1],
"c": [datetime(2016, 1, 1), datetime(2016, 1, 2), datetime(2016, 1, 3)],
"d": [2, 3, "d"],
},
columns=["a", "b", "c", "d"],
)
# reconstruct frame to verify inference is same
result = df.reset_index(drop=True)
tm.assert_frame_equal(result, expected)
52 changes: 52 additions & 0 deletions pandas/tests/frame/methods/test_to_dict_of_blocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import numpy as np

from pandas import DataFrame
from pandas.core.arrays import PandasArray


class TestToDictOfBlocks:
def test_copy_blocks(self, float_frame):
# GH#9607
df = DataFrame(float_frame, copy=True)
column = df.columns[0]

# use the default copy=True, change a column
blocks = df._to_dict_of_blocks(copy=True)
for dtype, _df in blocks.items():
if column in _df:
_df.loc[:, column] = _df[column] + 1

# make sure we did not change the original DataFrame
assert not _df[column].equals(df[column])

def test_no_copy_blocks(self, float_frame):
# GH#9607
df = DataFrame(float_frame, copy=True)
column = df.columns[0]

# use the copy=False, change a column
blocks = df._to_dict_of_blocks(copy=False)
for dtype, _df in blocks.items():
if column in _df:
_df.loc[:, column] = _df[column] + 1

# make sure we did change the original DataFrame
assert _df[column].equals(df[column])


def test_to_dict_of_blocks_item_cache():
# Calling to_dict_of_blocks should not poison item_cache
df = DataFrame({"a": [1, 2, 3, 4], "b": ["a", "b", "c", "d"]})
df["c"] = PandasArray(np.array([1, 2, None, 3], dtype=object))
mgr = df._mgr
assert len(mgr.blocks) == 3 # i.e. not consolidated

ser = df["b"] # populations item_cache["b"]

df._to_dict_of_blocks()

# Check that the to_dict_of_blocks didnt break link between ser and df
ser.values[0] = "foo"
assert df.loc[0, "b"] == "foo"

assert df["b"] is ser
59 changes: 59 additions & 0 deletions pandas/tests/frame/methods/test_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,62 @@ def test_interleave_with_tzaware(self, timezone_frame):
dtype=object,
).T
tm.assert_numpy_array_equal(result, expected)

def test_values_interleave_non_unique_cols(self):
df = DataFrame(
[[Timestamp("20130101"), 3.5], [Timestamp("20130102"), 4.5]],
columns=["x", "x"],
index=[1, 2],
)

df_unique = df.copy()
df_unique.columns = ["x", "y"]
assert df_unique.values.shape == df.values.shape
tm.assert_numpy_array_equal(df_unique.values[0], df.values[0])
tm.assert_numpy_array_equal(df_unique.values[1], df.values[1])

def test_values_numeric_cols(self, float_frame):
float_frame["foo"] = "bar"

values = float_frame[["A", "B", "C", "D"]].values
assert values.dtype == np.float64

def test_values_lcd(self, mixed_float_frame, mixed_int_frame):

# mixed lcd
values = mixed_float_frame[["A", "B", "C", "D"]].values
assert values.dtype == np.float64

values = mixed_float_frame[["A", "B", "C"]].values
assert values.dtype == np.float32

values = mixed_float_frame[["C"]].values
assert values.dtype == np.float16

# GH#10364
# B uint64 forces float because there are other signed int types
values = mixed_int_frame[["A", "B", "C", "D"]].values
assert values.dtype == np.float64

values = mixed_int_frame[["A", "D"]].values
assert values.dtype == np.int64

# B uint64 forces float because there are other signed int types
values = mixed_int_frame[["A", "B", "C"]].values
assert values.dtype == np.float64

# as B and C are both unsigned, no forcing to float is needed
values = mixed_int_frame[["B", "C"]].values
assert values.dtype == np.uint64

values = mixed_int_frame[["A", "C"]].values
assert values.dtype == np.int32

values = mixed_int_frame[["C", "D"]].values
assert values.dtype == np.int64

values = mixed_int_frame[["A"]].values
assert values.dtype == np.int32

values = mixed_int_frame[["C"]].values
assert values.dtype == np.uint8
97 changes: 97 additions & 0 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@
from pandas.core.computation.expressions import _MIN_ELEMENTS, NUMEXPR_INSTALLED
from pandas.tests.frame.common import _check_mixed_float, _check_mixed_int


class DummyElement:
def __init__(self, value, dtype):
self.value = value
self.dtype = np.dtype(dtype)

def __array__(self):
return np.array(self.value, dtype=self.dtype)

def __str__(self) -> str:
return f"DummyElement({self.value}, {self.dtype})"

def __repr__(self) -> str:
return str(self)

def astype(self, dtype, copy=False):
self.dtype = dtype
return self

def view(self, dtype):
return type(self)(self.value.view(dtype), dtype)

def any(self, axis=None):
return bool(self.value)


# -------------------------------------------------------------------
# Comparisons

Expand Down Expand Up @@ -782,6 +808,77 @@ def test_frame_with_frame_reindex(self):
)
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize(
"value, dtype",
[
(1, "i8"),
(1.0, "f8"),
(2 ** 63, "f8"),
(1j, "complex128"),
(2 ** 63, "complex128"),
(True, "bool"),
(np.timedelta64(20, "ns"), "<m8[ns]"),
(np.datetime64(20, "ns"), "<M8[ns]"),
],
)
@pytest.mark.parametrize(
"op",
[
operator.add,
operator.sub,
operator.mul,
operator.truediv,
operator.mod,
operator.pow,
],
ids=lambda x: x.__name__,
)
def test_binop_other(self, op, value, dtype):
skip = {
(operator.add, "bool"),
(operator.sub, "bool"),
(operator.mul, "bool"),
(operator.truediv, "bool"),
(operator.mod, "i8"),
(operator.mod, "complex128"),
(operator.pow, "bool"),
}
if (op, dtype) in skip:
pytest.skip(f"Invalid combination {op},{dtype}")

e = DummyElement(value, dtype)
s = DataFrame({"A": [e.value, e.value]}, dtype=e.dtype)

invalid = {
(operator.pow, "<M8[ns]"),
(operator.mod, "<M8[ns]"),
(operator.truediv, "<M8[ns]"),
(operator.mul, "<M8[ns]"),
(operator.add, "<M8[ns]"),
(operator.pow, "<m8[ns]"),
(operator.mul, "<m8[ns]"),
}

if (op, dtype) in invalid:
msg = (
None
if (dtype == "<M8[ns]" and op == operator.add)
or (dtype == "<m8[ns]" and op == operator.mul)
else (
f"cannot perform __{op.__name__}__ with this "
"index type: (DatetimeArray|TimedeltaArray)"
)
)

with pytest.raises(TypeError, match=msg):
op(s, e.value)
else:
# FIXME: Since dispatching to Series, this test no longer
# asserts anything meaningful
result = op(s, e.value).dtypes
expected = op(s, value).dtypes
tm.assert_series_equal(result, expected)


def test_frame_with_zero_len_series_corner_cases():
# GH#28600
Expand Down
Loading