Skip to content

Commit 315797c

Browse files
authored
REF: move misplaced tests (#54429)
* REF: move misplaced extension tests * remove unnecesssary override
1 parent 3cc0679 commit 315797c

File tree

6 files changed

+81
-87
lines changed

6 files changed

+81
-87
lines changed

pandas/tests/arrays/categorical/test_astype.py

+56
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
from pandas import (
55
Categorical,
66
CategoricalDtype,
7+
CategoricalIndex,
8+
DatetimeIndex,
9+
Interval,
710
NaT,
11+
Period,
812
Timestamp,
913
array,
1014
to_datetime,
@@ -13,6 +17,50 @@
1317

1418

1519
class TestAstype:
20+
@pytest.mark.parametrize("cls", [Categorical, CategoricalIndex])
21+
@pytest.mark.parametrize("values", [[1, np.nan], [Timestamp("2000"), NaT]])
22+
def test_astype_nan_to_int(self, cls, values):
23+
# GH#28406
24+
obj = cls(values)
25+
26+
msg = "Cannot (cast|convert)"
27+
with pytest.raises((ValueError, TypeError), match=msg):
28+
obj.astype(int)
29+
30+
@pytest.mark.parametrize(
31+
"expected",
32+
[
33+
array(["2019", "2020"], dtype="datetime64[ns, UTC]"),
34+
array([0, 0], dtype="timedelta64[ns]"),
35+
array([Period("2019"), Period("2020")], dtype="period[A-DEC]"),
36+
array([Interval(0, 1), Interval(1, 2)], dtype="interval"),
37+
array([1, np.nan], dtype="Int64"),
38+
],
39+
)
40+
def test_astype_category_to_extension_dtype(self, expected):
41+
# GH#28668
42+
result = expected.astype("category").astype(expected.dtype)
43+
44+
tm.assert_extension_array_equal(result, expected)
45+
46+
@pytest.mark.parametrize(
47+
"dtype, expected",
48+
[
49+
(
50+
"datetime64[ns]",
51+
np.array(["2015-01-01T00:00:00.000000000"], dtype="datetime64[ns]"),
52+
),
53+
(
54+
"datetime64[ns, MET]",
55+
DatetimeIndex([Timestamp("2015-01-01 00:00:00+0100", tz="MET")]).array,
56+
),
57+
],
58+
)
59+
def test_astype_to_datetime64(self, dtype, expected):
60+
# GH#28448
61+
result = Categorical(["2015-01-01"]).astype(dtype)
62+
assert result == expected
63+
1664
def test_astype_str_int_categories_to_nullable_int(self):
1765
# GH#39616
1866
dtype = CategoricalDtype([str(i) for i in range(5)])
@@ -97,3 +145,11 @@ def test_astype_object_timestamp_categories(self):
97145
result = cat.astype(object)
98146
expected = np.array([Timestamp("2014-01-01 00:00:00")], dtype="object")
99147
tm.assert_numpy_array_equal(result, expected)
148+
149+
def test_astype_category_readonly_mask_values(self):
150+
# GH#53658
151+
arr = array([0, 1, 2], dtype="Int64")
152+
arr._mask.flags["WRITEABLE"] = False
153+
result = arr.astype("category")
154+
expected = array([0, 1, 2], dtype="Int64").astype("category")
155+
tm.assert_extension_array_equal(result, expected)

pandas/tests/arrays/categorical/test_operators.py

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Categorical,
99
DataFrame,
1010
Series,
11+
Timestamp,
1112
date_range,
1213
)
1314
import pandas._testing as tm
@@ -128,6 +129,19 @@ def test_comparisons(self, factor):
128129

129130

130131
class TestCategoricalOps:
132+
@pytest.mark.parametrize(
133+
"categories",
134+
[["a", "b"], [0, 1], [Timestamp("2019"), Timestamp("2020")]],
135+
)
136+
def test_not_equal_with_na(self, categories):
137+
# https://github.com/pandas-dev/pandas/issues/32276
138+
c1 = Categorical.from_codes([-1, 0], categories=categories)
139+
c2 = Categorical.from_codes([0, 1], categories=categories)
140+
141+
result = c1 != c2
142+
143+
assert result.all()
144+
131145
def test_compare_frame(self):
132146
# GH#24282 check that Categorical.__cmp__(DataFrame) defers to frame
133147
data = ["a", "b", 2, "a"]

pandas/tests/arrays/string_/test_string_arrow.py

+8
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ def test_constructor_valid_string_type_value_dictionary(chunked):
9696
assert pa.types.is_string(arr._pa_array.type.value_type)
9797

9898

99+
def test_constructor_from_list():
100+
# GH#27673
101+
pytest.importorskip("pyarrow", minversion="1.0.0")
102+
result = pd.Series(["E"], dtype=StringDtype(storage="pyarrow"))
103+
assert isinstance(result.dtype, StringDtype)
104+
assert result.dtype.storage == "pyarrow"
105+
106+
99107
@skip_if_no_pyarrow
100108
def test_from_sequence_wrong_dtype_raises():
101109
with pd.option_context("string_storage", "python"):

pandas/tests/extension/test_categorical.py

+3-73
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919
import pytest
2020

2121
import pandas as pd
22-
from pandas import (
23-
Categorical,
24-
CategoricalIndex,
25-
Timestamp,
26-
)
22+
from pandas import Categorical
2723
import pandas._testing as tm
2824
from pandas.api.types import CategoricalDtype
2925
from pandas.tests.extension import base
@@ -93,7 +89,7 @@ class TestDtype(base.BaseDtypeTests):
9389
class TestInterface(base.BaseInterfaceTests):
9490
@pytest.mark.xfail(reason="Memory usage doesn't match")
9591
def test_memory_usage(self, data):
96-
# Is this deliberate?
92+
# TODO: Is this deliberate?
9793
super().test_memory_usage(data)
9894

9995
def test_contains(self, data, data_missing):
@@ -194,51 +190,7 @@ def test_map(self, data, na_action):
194190

195191

196192
class TestCasting(base.BaseCastingTests):
197-
@pytest.mark.parametrize("cls", [Categorical, CategoricalIndex])
198-
@pytest.mark.parametrize("values", [[1, np.nan], [Timestamp("2000"), pd.NaT]])
199-
def test_cast_nan_to_int(self, cls, values):
200-
# GH 28406
201-
s = cls(values)
202-
203-
msg = "Cannot (cast|convert)"
204-
with pytest.raises((ValueError, TypeError), match=msg):
205-
s.astype(int)
206-
207-
@pytest.mark.parametrize(
208-
"expected",
209-
[
210-
pd.Series(["2019", "2020"], dtype="datetime64[ns, UTC]"),
211-
pd.Series([0, 0], dtype="timedelta64[ns]"),
212-
pd.Series([pd.Period("2019"), pd.Period("2020")], dtype="period[A-DEC]"),
213-
pd.Series([pd.Interval(0, 1), pd.Interval(1, 2)], dtype="interval"),
214-
pd.Series([1, np.nan], dtype="Int64"),
215-
],
216-
)
217-
def test_cast_category_to_extension_dtype(self, expected):
218-
# GH 28668
219-
result = expected.astype("category").astype(expected.dtype)
220-
221-
tm.assert_series_equal(result, expected)
222-
223-
@pytest.mark.parametrize(
224-
"dtype, expected",
225-
[
226-
(
227-
"datetime64[ns]",
228-
np.array(["2015-01-01T00:00:00.000000000"], dtype="datetime64[ns]"),
229-
),
230-
(
231-
"datetime64[ns, MET]",
232-
pd.DatetimeIndex(
233-
[Timestamp("2015-01-01 00:00:00+0100", tz="MET")]
234-
).array,
235-
),
236-
],
237-
)
238-
def test_consistent_casting(self, dtype, expected):
239-
# GH 28448
240-
result = Categorical(["2015-01-01"]).astype(dtype)
241-
assert result == expected
193+
pass
242194

243195

244196
class TestArithmeticOps(base.BaseArithmeticOpsTests):
@@ -274,19 +226,6 @@ def _compare_other(self, s, data, op, other):
274226
else:
275227
return super()._compare_other(s, data, op, other)
276228

277-
@pytest.mark.parametrize(
278-
"categories",
279-
[["a", "b"], [0, 1], [Timestamp("2019"), Timestamp("2020")]],
280-
)
281-
def test_not_equal_with_na(self, categories):
282-
# https://github.com/pandas-dev/pandas/issues/32276
283-
c1 = Categorical.from_codes([-1, 0], categories=categories)
284-
c2 = Categorical.from_codes([0, 1], categories=categories)
285-
286-
result = c1 != c2
287-
288-
assert result.all()
289-
290229

291230
class TestParsing(base.BaseParsingTests):
292231
pass
@@ -301,12 +240,3 @@ def test_repr_2d(self, data):
301240

302241
res = repr(data.reshape(-1, 1))
303242
assert res.count("\nCategories") == 1
304-
305-
306-
def test_astype_category_readonly_mask_values():
307-
# GH 53658
308-
df = pd.DataFrame([0, 1, 2], dtype="Int64")
309-
df._mgr.arrays[0]._mask.flags["WRITEABLE"] = False
310-
result = df.astype("category")
311-
expected = pd.DataFrame([0, 1, 2], dtype="Int64").astype("category")
312-
tm.assert_frame_equal(result, expected)

pandas/tests/extension/test_sparse.py

-7
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,6 @@ def test_map_raises(self, data, na_action):
366366

367367

368368
class TestCasting(BaseSparseTests, base.BaseCastingTests):
369-
def test_astype_str(self, data):
370-
# pre-2.0 this would give a SparseDtype even if the user asked
371-
# for a non-sparse dtype.
372-
result = pd.Series(data[:5]).astype(str)
373-
expected = pd.Series([str(x) for x in data[:5]], dtype=object)
374-
tm.assert_series_equal(result, expected)
375-
376369
@pytest.mark.xfail(raises=TypeError, reason="no sparse StringDtype")
377370
def test_astype_string(self, data):
378371
super().test_astype_string(data)

pandas/tests/extension/test_string.py

-7
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,6 @@ def test_from_dtype(self, data):
119119
# base test uses string representation of dtype
120120
pass
121121

122-
def test_constructor_from_list(self):
123-
# GH 27673
124-
pytest.importorskip("pyarrow", minversion="1.0.0")
125-
result = pd.Series(["E"], dtype=StringDtype(storage="pyarrow"))
126-
assert isinstance(result.dtype, StringDtype)
127-
assert result.dtype.storage == "pyarrow"
128-
129122

130123
class TestReshaping(base.BaseReshapingTests):
131124
def test_transpose(self, data, request):

0 commit comments

Comments
 (0)