Skip to content

REF: move misplaced tests #54429

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 2 commits into from
Aug 5, 2023
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
56 changes: 56 additions & 0 deletions pandas/tests/arrays/categorical/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
from pandas import (
Categorical,
CategoricalDtype,
CategoricalIndex,
DatetimeIndex,
Interval,
NaT,
Period,
Timestamp,
array,
to_datetime,
Expand All @@ -13,6 +17,50 @@


class TestAstype:
@pytest.mark.parametrize("cls", [Categorical, CategoricalIndex])
@pytest.mark.parametrize("values", [[1, np.nan], [Timestamp("2000"), NaT]])
def test_astype_nan_to_int(self, cls, values):
# GH#28406
obj = cls(values)

msg = "Cannot (cast|convert)"
with pytest.raises((ValueError, TypeError), match=msg):
obj.astype(int)

@pytest.mark.parametrize(
"expected",
[
array(["2019", "2020"], dtype="datetime64[ns, UTC]"),
array([0, 0], dtype="timedelta64[ns]"),
array([Period("2019"), Period("2020")], dtype="period[A-DEC]"),
array([Interval(0, 1), Interval(1, 2)], dtype="interval"),
array([1, np.nan], dtype="Int64"),
],
)
def test_astype_category_to_extension_dtype(self, expected):
# GH#28668
result = expected.astype("category").astype(expected.dtype)

tm.assert_extension_array_equal(result, expected)

@pytest.mark.parametrize(
"dtype, expected",
[
(
"datetime64[ns]",
np.array(["2015-01-01T00:00:00.000000000"], dtype="datetime64[ns]"),
),
(
"datetime64[ns, MET]",
DatetimeIndex([Timestamp("2015-01-01 00:00:00+0100", tz="MET")]).array,
),
],
)
def test_astype_to_datetime64(self, dtype, expected):
# GH#28448
result = Categorical(["2015-01-01"]).astype(dtype)
assert result == expected

def test_astype_str_int_categories_to_nullable_int(self):
# GH#39616
dtype = CategoricalDtype([str(i) for i in range(5)])
Expand Down Expand Up @@ -97,3 +145,11 @@ def test_astype_object_timestamp_categories(self):
result = cat.astype(object)
expected = np.array([Timestamp("2014-01-01 00:00:00")], dtype="object")
tm.assert_numpy_array_equal(result, expected)

def test_astype_category_readonly_mask_values(self):
# GH#53658
arr = array([0, 1, 2], dtype="Int64")
arr._mask.flags["WRITEABLE"] = False
result = arr.astype("category")
expected = array([0, 1, 2], dtype="Int64").astype("category")
tm.assert_extension_array_equal(result, expected)
14 changes: 14 additions & 0 deletions pandas/tests/arrays/categorical/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Categorical,
DataFrame,
Series,
Timestamp,
date_range,
)
import pandas._testing as tm
Expand Down Expand Up @@ -128,6 +129,19 @@ def test_comparisons(self, factor):


class TestCategoricalOps:
@pytest.mark.parametrize(
"categories",
[["a", "b"], [0, 1], [Timestamp("2019"), Timestamp("2020")]],
)
def test_not_equal_with_na(self, categories):
# https://github.com/pandas-dev/pandas/issues/32276
c1 = Categorical.from_codes([-1, 0], categories=categories)
c2 = Categorical.from_codes([0, 1], categories=categories)

result = c1 != c2

assert result.all()

def test_compare_frame(self):
# GH#24282 check that Categorical.__cmp__(DataFrame) defers to frame
data = ["a", "b", 2, "a"]
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/arrays/string_/test_string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ def test_constructor_valid_string_type_value_dictionary(chunked):
assert pa.types.is_string(arr._pa_array.type.value_type)


def test_constructor_from_list():
# GH#27673
pytest.importorskip("pyarrow", minversion="1.0.0")
result = pd.Series(["E"], dtype=StringDtype(storage="pyarrow"))
assert isinstance(result.dtype, StringDtype)
assert result.dtype.storage == "pyarrow"


@skip_if_no_pyarrow
def test_from_sequence_wrong_dtype_raises():
with pd.option_context("string_storage", "python"):
Expand Down
76 changes: 3 additions & 73 deletions pandas/tests/extension/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@
import pytest

import pandas as pd
from pandas import (
Categorical,
CategoricalIndex,
Timestamp,
)
from pandas import Categorical
import pandas._testing as tm
from pandas.api.types import CategoricalDtype
from pandas.tests.extension import base
Expand Down Expand Up @@ -93,7 +89,7 @@ class TestDtype(base.BaseDtypeTests):
class TestInterface(base.BaseInterfaceTests):
@pytest.mark.xfail(reason="Memory usage doesn't match")
def test_memory_usage(self, data):
# Is this deliberate?
# TODO: Is this deliberate?
super().test_memory_usage(data)

def test_contains(self, data, data_missing):
Expand Down Expand Up @@ -194,51 +190,7 @@ def test_map(self, data, na_action):


class TestCasting(base.BaseCastingTests):
@pytest.mark.parametrize("cls", [Categorical, CategoricalIndex])
@pytest.mark.parametrize("values", [[1, np.nan], [Timestamp("2000"), pd.NaT]])
def test_cast_nan_to_int(self, cls, values):
# GH 28406
s = cls(values)

msg = "Cannot (cast|convert)"
with pytest.raises((ValueError, TypeError), match=msg):
s.astype(int)

@pytest.mark.parametrize(
"expected",
[
pd.Series(["2019", "2020"], dtype="datetime64[ns, UTC]"),
pd.Series([0, 0], dtype="timedelta64[ns]"),
pd.Series([pd.Period("2019"), pd.Period("2020")], dtype="period[A-DEC]"),
pd.Series([pd.Interval(0, 1), pd.Interval(1, 2)], dtype="interval"),
pd.Series([1, np.nan], dtype="Int64"),
],
)
def test_cast_category_to_extension_dtype(self, expected):
# GH 28668
result = expected.astype("category").astype(expected.dtype)

tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
"dtype, expected",
[
(
"datetime64[ns]",
np.array(["2015-01-01T00:00:00.000000000"], dtype="datetime64[ns]"),
),
(
"datetime64[ns, MET]",
pd.DatetimeIndex(
[Timestamp("2015-01-01 00:00:00+0100", tz="MET")]
).array,
),
],
)
def test_consistent_casting(self, dtype, expected):
# GH 28448
result = Categorical(["2015-01-01"]).astype(dtype)
assert result == expected
pass


class TestArithmeticOps(base.BaseArithmeticOpsTests):
Expand Down Expand Up @@ -287,19 +239,6 @@ def _compare_other(self, s, data, op, other):
with pytest.raises(TypeError, match=msg):
op(data, other)

@pytest.mark.parametrize(
"categories",
[["a", "b"], [0, 1], [Timestamp("2019"), Timestamp("2020")]],
)
def test_not_equal_with_na(self, categories):
# https://github.com/pandas-dev/pandas/issues/32276
c1 = Categorical.from_codes([-1, 0], categories=categories)
c2 = Categorical.from_codes([0, 1], categories=categories)

result = c1 != c2

assert result.all()


class TestParsing(base.BaseParsingTests):
pass
Expand All @@ -314,12 +253,3 @@ def test_repr_2d(self, data):

res = repr(data.reshape(-1, 1))
assert res.count("\nCategories") == 1


def test_astype_category_readonly_mask_values():
# GH 53658
df = pd.DataFrame([0, 1, 2], dtype="Int64")
df._mgr.arrays[0]._mask.flags["WRITEABLE"] = False
result = df.astype("category")
expected = pd.DataFrame([0, 1, 2], dtype="Int64").astype("category")
tm.assert_frame_equal(result, expected)
7 changes: 0 additions & 7 deletions pandas/tests/extension/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,6 @@ def test_map_raises(self, data, na_action):


class TestCasting(BaseSparseTests, base.BaseCastingTests):
def test_astype_str(self, data):
# pre-2.0 this would give a SparseDtype even if the user asked
# for a non-sparse dtype.
result = pd.Series(data[:5]).astype(str)
expected = pd.Series([str(x) for x in data[:5]], dtype=object)
tm.assert_series_equal(result, expected)

@pytest.mark.xfail(raises=TypeError, reason="no sparse StringDtype")
def test_astype_string(self, data):
super().test_astype_string(data)
Expand Down
7 changes: 0 additions & 7 deletions pandas/tests/extension/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,6 @@ def test_from_dtype(self, data):
# base test uses string representation of dtype
pass

def test_constructor_from_list(self):
# GH 27673
pytest.importorskip("pyarrow", minversion="1.0.0")
result = pd.Series(["E"], dtype=StringDtype(storage="pyarrow"))
assert isinstance(result.dtype, StringDtype)
assert result.dtype.storage == "pyarrow"


class TestReshaping(base.BaseReshapingTests):
def test_transpose(self, data, request):
Expand Down