Skip to content

Commit 8985801

Browse files
authored
TST/REF: collect tests by method from tests.internals (#37420)
1 parent 3c8c4c9 commit 8985801

8 files changed

+327
-305
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from pandas import DataFrame
2+
import pandas._testing as tm
3+
4+
5+
class TestEquals:
6+
def test_dataframe_not_equal(self):
7+
# see GH#28839
8+
df1 = DataFrame({"a": [1, 2], "b": ["s", "d"]})
9+
df2 = DataFrame({"a": ["s", "d"], "b": [1, 2]})
10+
assert df1.equals(df2) is False
11+
12+
def test_equals_different_blocks(self):
13+
# GH#9330
14+
df0 = DataFrame({"A": ["x", "y"], "B": [1, 2], "C": ["w", "z"]})
15+
df1 = df0.reset_index()[["A", "B", "C"]]
16+
# this assert verifies that the above operations have
17+
# induced a block rearrangement
18+
assert df0._mgr.blocks[0].dtype != df1._mgr.blocks[0].dtype
19+
20+
# do the real tests
21+
tm.assert_frame_equal(df0, df1)
22+
assert df0.equals(df1)
23+
assert df1.equals(df0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from datetime import datetime
2+
3+
from pandas import DataFrame
4+
import pandas._testing as tm
5+
6+
7+
class TestInferObjects:
8+
def test_infer_objects(self):
9+
# GH#11221
10+
df = DataFrame(
11+
{
12+
"a": ["a", 1, 2, 3],
13+
"b": ["b", 2.0, 3.0, 4.1],
14+
"c": [
15+
"c",
16+
datetime(2016, 1, 1),
17+
datetime(2016, 1, 2),
18+
datetime(2016, 1, 3),
19+
],
20+
"d": [1, 2, 3, "d"],
21+
},
22+
columns=["a", "b", "c", "d"],
23+
)
24+
df = df.iloc[1:].infer_objects()
25+
26+
assert df["a"].dtype == "int64"
27+
assert df["b"].dtype == "float64"
28+
assert df["c"].dtype == "M8[ns]"
29+
assert df["d"].dtype == "object"
30+
31+
expected = DataFrame(
32+
{
33+
"a": [1, 2, 3],
34+
"b": [2.0, 3.0, 4.1],
35+
"c": [datetime(2016, 1, 1), datetime(2016, 1, 2), datetime(2016, 1, 3)],
36+
"d": [2, 3, "d"],
37+
},
38+
columns=["a", "b", "c", "d"],
39+
)
40+
# reconstruct frame to verify inference is same
41+
result = df.reset_index(drop=True)
42+
tm.assert_frame_equal(result, expected)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import numpy as np
2+
3+
from pandas import DataFrame
4+
from pandas.core.arrays import PandasArray
5+
6+
7+
class TestToDictOfBlocks:
8+
def test_copy_blocks(self, float_frame):
9+
# GH#9607
10+
df = DataFrame(float_frame, copy=True)
11+
column = df.columns[0]
12+
13+
# use the default copy=True, change a column
14+
blocks = df._to_dict_of_blocks(copy=True)
15+
for dtype, _df in blocks.items():
16+
if column in _df:
17+
_df.loc[:, column] = _df[column] + 1
18+
19+
# make sure we did not change the original DataFrame
20+
assert not _df[column].equals(df[column])
21+
22+
def test_no_copy_blocks(self, float_frame):
23+
# GH#9607
24+
df = DataFrame(float_frame, copy=True)
25+
column = df.columns[0]
26+
27+
# use the copy=False, change a column
28+
blocks = df._to_dict_of_blocks(copy=False)
29+
for dtype, _df in blocks.items():
30+
if column in _df:
31+
_df.loc[:, column] = _df[column] + 1
32+
33+
# make sure we did change the original DataFrame
34+
assert _df[column].equals(df[column])
35+
36+
37+
def test_to_dict_of_blocks_item_cache():
38+
# Calling to_dict_of_blocks should not poison item_cache
39+
df = DataFrame({"a": [1, 2, 3, 4], "b": ["a", "b", "c", "d"]})
40+
df["c"] = PandasArray(np.array([1, 2, None, 3], dtype=object))
41+
mgr = df._mgr
42+
assert len(mgr.blocks) == 3 # i.e. not consolidated
43+
44+
ser = df["b"] # populations item_cache["b"]
45+
46+
df._to_dict_of_blocks()
47+
48+
# Check that the to_dict_of_blocks didnt break link between ser and df
49+
ser.values[0] = "foo"
50+
assert df.loc[0, "b"] == "foo"
51+
52+
assert df["b"] is ser

pandas/tests/frame/methods/test_values.py

+59
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,62 @@ def test_interleave_with_tzaware(self, timezone_frame):
102102
dtype=object,
103103
).T
104104
tm.assert_numpy_array_equal(result, expected)
105+
106+
def test_values_interleave_non_unique_cols(self):
107+
df = DataFrame(
108+
[[Timestamp("20130101"), 3.5], [Timestamp("20130102"), 4.5]],
109+
columns=["x", "x"],
110+
index=[1, 2],
111+
)
112+
113+
df_unique = df.copy()
114+
df_unique.columns = ["x", "y"]
115+
assert df_unique.values.shape == df.values.shape
116+
tm.assert_numpy_array_equal(df_unique.values[0], df.values[0])
117+
tm.assert_numpy_array_equal(df_unique.values[1], df.values[1])
118+
119+
def test_values_numeric_cols(self, float_frame):
120+
float_frame["foo"] = "bar"
121+
122+
values = float_frame[["A", "B", "C", "D"]].values
123+
assert values.dtype == np.float64
124+
125+
def test_values_lcd(self, mixed_float_frame, mixed_int_frame):
126+
127+
# mixed lcd
128+
values = mixed_float_frame[["A", "B", "C", "D"]].values
129+
assert values.dtype == np.float64
130+
131+
values = mixed_float_frame[["A", "B", "C"]].values
132+
assert values.dtype == np.float32
133+
134+
values = mixed_float_frame[["C"]].values
135+
assert values.dtype == np.float16
136+
137+
# GH#10364
138+
# B uint64 forces float because there are other signed int types
139+
values = mixed_int_frame[["A", "B", "C", "D"]].values
140+
assert values.dtype == np.float64
141+
142+
values = mixed_int_frame[["A", "D"]].values
143+
assert values.dtype == np.int64
144+
145+
# B uint64 forces float because there are other signed int types
146+
values = mixed_int_frame[["A", "B", "C"]].values
147+
assert values.dtype == np.float64
148+
149+
# as B and C are both unsigned, no forcing to float is needed
150+
values = mixed_int_frame[["B", "C"]].values
151+
assert values.dtype == np.uint64
152+
153+
values = mixed_int_frame[["A", "C"]].values
154+
assert values.dtype == np.int32
155+
156+
values = mixed_int_frame[["C", "D"]].values
157+
assert values.dtype == np.int64
158+
159+
values = mixed_int_frame[["A"]].values
160+
assert values.dtype == np.int32
161+
162+
values = mixed_int_frame[["C"]].values
163+
assert values.dtype == np.uint8

pandas/tests/frame/test_arithmetic.py

+97
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@
1414
from pandas.core.computation.expressions import _MIN_ELEMENTS, NUMEXPR_INSTALLED
1515
from pandas.tests.frame.common import _check_mixed_float, _check_mixed_int
1616

17+
18+
class DummyElement:
19+
def __init__(self, value, dtype):
20+
self.value = value
21+
self.dtype = np.dtype(dtype)
22+
23+
def __array__(self):
24+
return np.array(self.value, dtype=self.dtype)
25+
26+
def __str__(self) -> str:
27+
return f"DummyElement({self.value}, {self.dtype})"
28+
29+
def __repr__(self) -> str:
30+
return str(self)
31+
32+
def astype(self, dtype, copy=False):
33+
self.dtype = dtype
34+
return self
35+
36+
def view(self, dtype):
37+
return type(self)(self.value.view(dtype), dtype)
38+
39+
def any(self, axis=None):
40+
return bool(self.value)
41+
42+
1743
# -------------------------------------------------------------------
1844
# Comparisons
1945

@@ -782,6 +808,77 @@ def test_frame_with_frame_reindex(self):
782808
)
783809
tm.assert_frame_equal(result, expected)
784810

811+
@pytest.mark.parametrize(
812+
"value, dtype",
813+
[
814+
(1, "i8"),
815+
(1.0, "f8"),
816+
(2 ** 63, "f8"),
817+
(1j, "complex128"),
818+
(2 ** 63, "complex128"),
819+
(True, "bool"),
820+
(np.timedelta64(20, "ns"), "<m8[ns]"),
821+
(np.datetime64(20, "ns"), "<M8[ns]"),
822+
],
823+
)
824+
@pytest.mark.parametrize(
825+
"op",
826+
[
827+
operator.add,
828+
operator.sub,
829+
operator.mul,
830+
operator.truediv,
831+
operator.mod,
832+
operator.pow,
833+
],
834+
ids=lambda x: x.__name__,
835+
)
836+
def test_binop_other(self, op, value, dtype):
837+
skip = {
838+
(operator.add, "bool"),
839+
(operator.sub, "bool"),
840+
(operator.mul, "bool"),
841+
(operator.truediv, "bool"),
842+
(operator.mod, "i8"),
843+
(operator.mod, "complex128"),
844+
(operator.pow, "bool"),
845+
}
846+
if (op, dtype) in skip:
847+
pytest.skip(f"Invalid combination {op},{dtype}")
848+
849+
e = DummyElement(value, dtype)
850+
s = DataFrame({"A": [e.value, e.value]}, dtype=e.dtype)
851+
852+
invalid = {
853+
(operator.pow, "<M8[ns]"),
854+
(operator.mod, "<M8[ns]"),
855+
(operator.truediv, "<M8[ns]"),
856+
(operator.mul, "<M8[ns]"),
857+
(operator.add, "<M8[ns]"),
858+
(operator.pow, "<m8[ns]"),
859+
(operator.mul, "<m8[ns]"),
860+
}
861+
862+
if (op, dtype) in invalid:
863+
msg = (
864+
None
865+
if (dtype == "<M8[ns]" and op == operator.add)
866+
or (dtype == "<m8[ns]" and op == operator.mul)
867+
else (
868+
f"cannot perform __{op.__name__}__ with this "
869+
"index type: (DatetimeArray|TimedeltaArray)"
870+
)
871+
)
872+
873+
with pytest.raises(TypeError, match=msg):
874+
op(s, e.value)
875+
else:
876+
# FIXME: Since dispatching to Series, this test no longer
877+
# asserts anything meaningful
878+
result = op(s, e.value).dtypes
879+
expected = op(s, value).dtypes
880+
tm.assert_series_equal(result, expected)
881+
785882

786883
def test_frame_with_zero_len_series_corner_cases():
787884
# GH#28600

0 commit comments

Comments
 (0)