Skip to content

Commit 857572d

Browse files
committed
TST/REF: misplaced tests in frame.test_dtypes (pandas-dev#37424)
1 parent 39afdfd commit 857572d

File tree

6 files changed

+113
-103
lines changed

6 files changed

+113
-103
lines changed

pandas/tests/frame/test_dtypes.py renamed to pandas/tests/frame/methods/test_dtypes.py

+1-103
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from datetime import timedelta
22

33
import numpy as np
4-
import pytest
54

65
from pandas.core.dtypes.dtypes import DatetimeTZDtype
76

@@ -89,16 +88,7 @@ def test_dtypes_gh8722(self, float_string_frame):
8988
result = df.dtypes
9089
tm.assert_series_equal(result, Series({0: np.dtype("int64")}))
9190

92-
def test_singlerow_slice_categoricaldtype_gives_series(self):
93-
# GH29521
94-
df = DataFrame({"x": pd.Categorical("a b c d e".split())})
95-
result = df.iloc[0]
96-
raw_cat = pd.Categorical(["a"], categories=["a", "b", "c", "d", "e"])
97-
expected = Series(raw_cat, index=["x"], name=0, dtype="category")
98-
99-
tm.assert_series_equal(result, expected)
100-
101-
def test_timedeltas(self):
91+
def test_dtypes_timedeltas(self):
10292
df = DataFrame(
10393
dict(
10494
A=Series(date_range("2012-1-1", periods=3, freq="D")),
@@ -136,95 +126,3 @@ def test_timedeltas(self):
136126
index=list("ABCD"),
137127
)
138128
tm.assert_series_equal(result, expected)
139-
140-
@pytest.mark.parametrize(
141-
"input_vals",
142-
[
143-
([1, 2]),
144-
(["1", "2"]),
145-
(list(pd.date_range("1/1/2011", periods=2, freq="H"))),
146-
(list(pd.date_range("1/1/2011", periods=2, freq="H", tz="US/Eastern"))),
147-
([pd.Interval(left=0, right=5)]),
148-
],
149-
)
150-
def test_constructor_list_str(self, input_vals, string_dtype):
151-
# GH 16605
152-
# Ensure that data elements are converted to strings when
153-
# dtype is str, 'str', or 'U'
154-
155-
result = DataFrame({"A": input_vals}, dtype=string_dtype)
156-
expected = DataFrame({"A": input_vals}).astype({"A": string_dtype})
157-
tm.assert_frame_equal(result, expected)
158-
159-
def test_constructor_list_str_na(self, string_dtype):
160-
161-
result = DataFrame({"A": [1.0, 2.0, None]}, dtype=string_dtype)
162-
expected = DataFrame({"A": ["1.0", "2.0", None]}, dtype=object)
163-
tm.assert_frame_equal(result, expected)
164-
165-
@pytest.mark.parametrize(
166-
"data, expected",
167-
[
168-
# empty
169-
(DataFrame(), True),
170-
# multi-same
171-
(DataFrame({"A": [1, 2], "B": [1, 2]}), True),
172-
# multi-object
173-
(
174-
DataFrame(
175-
{
176-
"A": np.array([1, 2], dtype=object),
177-
"B": np.array(["a", "b"], dtype=object),
178-
}
179-
),
180-
True,
181-
),
182-
# multi-extension
183-
(
184-
DataFrame(
185-
{"A": pd.Categorical(["a", "b"]), "B": pd.Categorical(["a", "b"])}
186-
),
187-
True,
188-
),
189-
# differ types
190-
(DataFrame({"A": [1, 2], "B": [1.0, 2.0]}), False),
191-
# differ sizes
192-
(
193-
DataFrame(
194-
{
195-
"A": np.array([1, 2], dtype=np.int32),
196-
"B": np.array([1, 2], dtype=np.int64),
197-
}
198-
),
199-
False,
200-
),
201-
# multi-extension differ
202-
(
203-
DataFrame(
204-
{"A": pd.Categorical(["a", "b"]), "B": pd.Categorical(["b", "c"])}
205-
),
206-
False,
207-
),
208-
],
209-
)
210-
def test_is_homogeneous_type(self, data, expected):
211-
assert data._is_homogeneous_type is expected
212-
213-
def test_asarray_homogenous(self):
214-
df = DataFrame({"A": pd.Categorical([1, 2]), "B": pd.Categorical([1, 2])})
215-
result = np.asarray(df)
216-
# may change from object in the future
217-
expected = np.array([[1, 1], [2, 2]], dtype="object")
218-
tm.assert_numpy_array_equal(result, expected)
219-
220-
def test_str_to_small_float_conversion_type(self):
221-
# GH 20388
222-
np.random.seed(13)
223-
col_data = [str(np.random.random() * 1e-12) for _ in range(5)]
224-
result = DataFrame(col_data, columns=["A"])
225-
expected = DataFrame(col_data, columns=["A"], dtype=object)
226-
tm.assert_frame_equal(result, expected)
227-
# change the dtype of the elements from object to float one by one
228-
result.loc[result.index, "A"] = [float(x) for x in col_data]
229-
expected = DataFrame(col_data, columns=["A"], dtype=float)
230-
tm.assert_frame_equal(result, expected)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import Categorical, DataFrame
5+
6+
7+
@pytest.mark.parametrize(
8+
"data, expected",
9+
[
10+
# empty
11+
(DataFrame(), True),
12+
# multi-same
13+
(DataFrame({"A": [1, 2], "B": [1, 2]}), True),
14+
# multi-object
15+
(
16+
DataFrame(
17+
{
18+
"A": np.array([1, 2], dtype=object),
19+
"B": np.array(["a", "b"], dtype=object),
20+
}
21+
),
22+
True,
23+
),
24+
# multi-extension
25+
(
26+
DataFrame({"A": Categorical(["a", "b"]), "B": Categorical(["a", "b"])}),
27+
True,
28+
),
29+
# differ types
30+
(DataFrame({"A": [1, 2], "B": [1.0, 2.0]}), False),
31+
# differ sizes
32+
(
33+
DataFrame(
34+
{
35+
"A": np.array([1, 2], dtype=np.int32),
36+
"B": np.array([1, 2], dtype=np.int64),
37+
}
38+
),
39+
False,
40+
),
41+
# multi-extension differ
42+
(
43+
DataFrame({"A": Categorical(["a", "b"]), "B": Categorical(["b", "c"])}),
44+
False,
45+
),
46+
],
47+
)
48+
def test_is_homogeneous_type(data, expected):
49+
assert data._is_homogeneous_type is expected

pandas/tests/frame/test_constructors.py

+25
Original file line numberDiff line numberDiff line change
@@ -2697,6 +2697,31 @@ def test_frame_ctor_datetime64_column(self):
26972697
df = DataFrame({"A": np.random.randn(len(rng)), "B": dates})
26982698
assert np.issubdtype(df["B"].dtype, np.dtype("M8[ns]"))
26992699

2700+
@pytest.mark.parametrize(
2701+
"input_vals",
2702+
[
2703+
([1, 2]),
2704+
(["1", "2"]),
2705+
(list(date_range("1/1/2011", periods=2, freq="H"))),
2706+
(list(date_range("1/1/2011", periods=2, freq="H", tz="US/Eastern"))),
2707+
([pd.Interval(left=0, right=5)]),
2708+
],
2709+
)
2710+
def test_constructor_list_str(self, input_vals, string_dtype):
2711+
# GH#16605
2712+
# Ensure that data elements are converted to strings when
2713+
# dtype is str, 'str', or 'U'
2714+
2715+
result = DataFrame({"A": input_vals}, dtype=string_dtype)
2716+
expected = DataFrame({"A": input_vals}).astype({"A": string_dtype})
2717+
tm.assert_frame_equal(result, expected)
2718+
2719+
def test_constructor_list_str_na(self, string_dtype):
2720+
2721+
result = DataFrame({"A": [1.0, 2.0, None]}, dtype=string_dtype)
2722+
expected = DataFrame({"A": ["1.0", "2.0", None]}, dtype=object)
2723+
tm.assert_frame_equal(result, expected)
2724+
27002725

27012726
class TestDataFrameConstructorWithDatetimeTZ:
27022727
def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture):

pandas/tests/frame/test_npfuncs.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Tests for np.foo applied to DataFrame, not necessarily ufuncs.
3+
"""
4+
import numpy as np
5+
6+
from pandas import Categorical, DataFrame
7+
import pandas._testing as tm
8+
9+
10+
class TestAsArray:
11+
def test_asarray_homogenous(self):
12+
df = DataFrame({"A": Categorical([1, 2]), "B": Categorical([1, 2])})
13+
result = np.asarray(df)
14+
# may change from object in the future
15+
expected = np.array([[1, 1], [2, 2]], dtype="object")
16+
tm.assert_numpy_array_equal(result, expected)

pandas/tests/indexing/test_iloc.py

+9
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,15 @@ def test_iloc_with_boolean_operation(self):
739739
expected = DataFrame([[0.0, 4.0], [8.0, 12.0], [4.0, 5.0], [6.0, np.nan]])
740740
tm.assert_frame_equal(result, expected)
741741

742+
def test_iloc_getitem_singlerow_slice_categoricaldtype_gives_series(self):
743+
# GH#29521
744+
df = DataFrame({"x": pd.Categorical("a b c d e".split())})
745+
result = df.iloc[0]
746+
raw_cat = pd.Categorical(["a"], categories=["a", "b", "c", "d", "e"])
747+
expected = Series(raw_cat, index=["x"], name=0, dtype="category")
748+
749+
tm.assert_series_equal(result, expected)
750+
742751

743752
class TestILocSetItemDuplicateColumns:
744753
def test_iloc_setitem_scalar_duplicate_columns(self):

pandas/tests/indexing/test_loc.py

+13
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,19 @@ def test_loc_reverse_assignment(self):
978978

979979
tm.assert_series_equal(result, expected)
980980

981+
def test_loc_setitem_str_to_small_float_conversion_type(self):
982+
# GH#20388
983+
np.random.seed(13)
984+
col_data = [str(np.random.random() * 1e-12) for _ in range(5)]
985+
result = DataFrame(col_data, columns=["A"])
986+
expected = DataFrame(col_data, columns=["A"], dtype=object)
987+
tm.assert_frame_equal(result, expected)
988+
989+
# change the dtype of the elements from object to float one by one
990+
result.loc[result.index, "A"] = [float(x) for x in col_data]
991+
expected = DataFrame(col_data, columns=["A"], dtype=float)
992+
tm.assert_frame_equal(result, expected)
993+
981994

982995
class TestLocWithMultiIndex:
983996
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)