Skip to content

Commit 47352ea

Browse files
authored
TST: collect tests by method (#39976)
1 parent 1c309c4 commit 47352ea

File tree

6 files changed

+118
-129
lines changed

6 files changed

+118
-129
lines changed

pandas/tests/frame/methods/test_astype.py

+29
Original file line numberDiff line numberDiff line change
@@ -648,3 +648,32 @@ def test_astype_bytes(self):
648648
# GH#39474
649649
result = DataFrame(["foo", "bar", "baz"]).astype(bytes)
650650
assert result.dtypes[0] == np.dtype("S3")
651+
652+
653+
class TestAstypeCategorical:
654+
def test_astype_from_categorical3(self):
655+
df = DataFrame({"cats": [1, 2, 3, 4, 5, 6], "vals": [1, 2, 3, 4, 5, 6]})
656+
cats = Categorical([1, 2, 3, 4, 5, 6])
657+
exp_df = DataFrame({"cats": cats, "vals": [1, 2, 3, 4, 5, 6]})
658+
df["cats"] = df["cats"].astype("category")
659+
tm.assert_frame_equal(exp_df, df)
660+
661+
def test_astype_from_categorical4(self):
662+
df = DataFrame(
663+
{"cats": ["a", "b", "b", "a", "a", "d"], "vals": [1, 2, 3, 4, 5, 6]}
664+
)
665+
cats = Categorical(["a", "b", "b", "a", "a", "d"])
666+
exp_df = DataFrame({"cats": cats, "vals": [1, 2, 3, 4, 5, 6]})
667+
df["cats"] = df["cats"].astype("category")
668+
tm.assert_frame_equal(exp_df, df)
669+
670+
def test_categorical_astype_to_int(self, any_int_or_nullable_int_dtype):
671+
# GH#39402
672+
673+
df = DataFrame(data={"col1": pd.array([2.0, 1.0, 3.0])})
674+
df.col1 = df.col1.astype("category")
675+
df.col1 = df.col1.astype(any_int_or_nullable_int_dtype)
676+
expected = DataFrame(
677+
{"col1": pd.array([2, 1, 3], dtype=any_int_or_nullable_int_dtype)}
678+
)
679+
tm.assert_frame_equal(df, expected)

pandas/tests/series/methods/test_astype.py

+61
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,48 @@ def test_astype_bytes(self):
351351

352352

353353
class TestAstypeCategorical:
354+
def test_astype_categorical_to_other(self):
355+
cat = Categorical([f"{i} - {i + 499}" for i in range(0, 10000, 500)])
356+
ser = Series(np.random.RandomState(0).randint(0, 10000, 100)).sort_values()
357+
ser = cut(ser, range(0, 10500, 500), right=False, labels=cat)
358+
359+
expected = ser
360+
tm.assert_series_equal(ser.astype("category"), expected)
361+
tm.assert_series_equal(ser.astype(CategoricalDtype()), expected)
362+
msg = r"Cannot cast object dtype to float64"
363+
with pytest.raises(ValueError, match=msg):
364+
ser.astype("float64")
365+
366+
cat = Series(Categorical(["a", "b", "b", "a", "a", "c", "c", "c"]))
367+
exp = Series(["a", "b", "b", "a", "a", "c", "c", "c"])
368+
tm.assert_series_equal(cat.astype("str"), exp)
369+
s2 = Series(Categorical(["1", "2", "3", "4"]))
370+
exp2 = Series([1, 2, 3, 4]).astype("int")
371+
tm.assert_series_equal(s2.astype("int"), exp2)
372+
373+
# object don't sort correctly, so just compare that we have the same
374+
# values
375+
def cmp(a, b):
376+
tm.assert_almost_equal(np.sort(np.unique(a)), np.sort(np.unique(b)))
377+
378+
expected = Series(np.array(ser.values), name="value_group")
379+
cmp(ser.astype("object"), expected)
380+
cmp(ser.astype(np.object_), expected)
381+
382+
# array conversion
383+
tm.assert_almost_equal(np.array(ser), np.array(ser.values))
384+
385+
tm.assert_series_equal(ser.astype("category"), ser)
386+
tm.assert_series_equal(ser.astype(CategoricalDtype()), ser)
387+
388+
roundtrip_expected = ser.cat.set_categories(
389+
ser.cat.categories.sort_values()
390+
).cat.remove_unused_categories()
391+
result = ser.astype("object").astype("category")
392+
tm.assert_series_equal(result, roundtrip_expected)
393+
result = ser.astype("object").astype(CategoricalDtype())
394+
tm.assert_series_equal(result, roundtrip_expected)
395+
354396
def test_astype_categorical_invalid_conversions(self):
355397
# invalid conversion (these are NOT a dtype)
356398
cat = Categorical([f"{i} - {i + 499}" for i in range(0, 10000, 500)])
@@ -427,3 +469,22 @@ def test_astype_categories_raises(self):
427469
s = Series(["a", "b", "a"])
428470
with pytest.raises(TypeError, match="got an unexpected"):
429471
s.astype("category", categories=["a", "b"], ordered=True)
472+
473+
@pytest.mark.parametrize("items", [["a", "b", "c", "a"], [1, 2, 3, 1]])
474+
def test_astype_from_categorical(self, items):
475+
ser = Series(items)
476+
exp = Series(Categorical(items))
477+
res = ser.astype("category")
478+
tm.assert_series_equal(res, exp)
479+
480+
def test_astype_from_categorical_with_keywords(self):
481+
# with keywords
482+
lst = ["a", "b", "c", "a"]
483+
ser = Series(lst)
484+
exp = Series(Categorical(lst, ordered=True))
485+
res = ser.astype(CategoricalDtype(None, ordered=True))
486+
tm.assert_series_equal(res, exp)
487+
488+
exp = Series(Categorical(lst, categories=list("abcdef"), ordered=True))
489+
res = ser.astype(CategoricalDtype(list("abcdef"), ordered=True))
490+
tm.assert_series_equal(res, exp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import numpy as np
2+
3+
4+
class TestSeriesDtypes:
5+
def test_dtype(self, datetime_series):
6+
7+
assert datetime_series.dtype == np.dtype("float64")
8+
assert datetime_series.dtypes == np.dtype("float64")

pandas/tests/series/methods/test_reindex.py

+11
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,17 @@ def test_reindex_categorical():
237237
tm.assert_series_equal(result, expected)
238238

239239

240+
def test_reindex_astype_order_consistency():
241+
# GH#17444
242+
ser = Series([1, 2, 3], index=[2, 0, 1])
243+
new_index = [0, 1, 2]
244+
temp_dtype = "category"
245+
new_dtype = str
246+
result = ser.reindex(new_index).astype(temp_dtype).astype(new_dtype)
247+
expected = ser.astype(temp_dtype).reindex(new_index).astype(new_dtype)
248+
tm.assert_series_equal(result, expected)
249+
250+
240251
def test_reindex_fill_value():
241252
# -----------------------------------------------------------
242253
# floats

pandas/tests/series/test_constructors.py

+9
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,15 @@ def test_constructor_categorical_with_coercion(self):
403403
result = x.person_name.loc[0]
404404
assert result == expected
405405

406+
def test_constructor_series_to_categorical(self):
407+
# see GH#16524: test conversion of Series to Categorical
408+
series = Series(["a", "b", "c"])
409+
410+
result = Series(series, dtype="category")
411+
expected = Series(["a", "b", "c"], dtype="category")
412+
413+
tm.assert_series_equal(result, expected)
414+
406415
def test_constructor_categorical_dtype(self):
407416
result = Series(
408417
["a", "b"], dtype=CategoricalDtype(["a", "b", "c"], ordered=True)

pandas/tests/series/test_dtypes.py

-129
This file was deleted.

0 commit comments

Comments
 (0)