Skip to content

Commit

Permalink
Adjust tests in dtypes folder for arrow string option (#56125)
Browse files Browse the repository at this point in the history
Adjust tests in base folder for arrow string option
  • Loading branch information
phofl authored Nov 26, 2023
1 parent 8ba1ab0 commit 2ed994f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1832,7 +1832,7 @@ def test_bool_ops_fails_on_scalars(lhs, cmp, rhs, engine, parser):
],
)
def test_equals_various(other):
df = DataFrame({"A": ["a", "b", "c"]})
df = DataFrame({"A": ["a", "b", "c"]}, dtype=object)
result = df.eval(f"A == {other}")
expected = Series([False, False, False], name="A")
if USE_NUMEXPR:
Expand Down
10 changes: 8 additions & 2 deletions pandas/tests/dtypes/cast/test_construct_ndarray.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import pytest

import pandas as pd
import pandas._testing as tm
from pandas.core.construction import sanitize_array

Expand All @@ -15,9 +16,14 @@
([1, 2, None], np.dtype("str"), np.array(["1", "2", None])),
],
)
def test_construct_1d_ndarray_preserving_na(values, dtype, expected):
def test_construct_1d_ndarray_preserving_na(
values, dtype, expected, using_infer_string
):
result = sanitize_array(values, index=None, dtype=dtype)
tm.assert_numpy_array_equal(result, expected)
if using_infer_string and expected.dtype == object and dtype is None:
tm.assert_extension_array_equal(result, pd.array(expected))
else:
tm.assert_numpy_array_equal(result, expected)


@pytest.mark.parametrize("dtype", ["m8[ns]", "M8[ns]"])
Expand Down
12 changes: 10 additions & 2 deletions pandas/tests/dtypes/cast/test_infer_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ def test_infer_dtype_from_scalar_errors():
(Timestamp("20160101", tz="UTC"), "datetime64[s, UTC]"),
],
)
def test_infer_dtype_from_scalar(value, expected):
def test_infer_dtype_from_scalar(value, expected, using_infer_string):
dtype, _ = infer_dtype_from_scalar(value)
if using_infer_string and value == "foo":
expected = "string"
assert is_dtype_equal(dtype, expected)

with pytest.raises(TypeError, match="must be list-like"):
Expand Down Expand Up @@ -189,8 +191,14 @@ def test_infer_dtype_from_scalar(value, expected):
),
],
)
def test_infer_dtype_from_array(arr, expected):
def test_infer_dtype_from_array(arr, expected, using_infer_string):
dtype, _ = infer_dtype_from_array(arr)
if (
using_infer_string
and isinstance(arr, Series)
and arr.tolist() == ["a", "b", "c"]
):
expected = "string"
assert is_dtype_equal(dtype, expected)


Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,9 +676,9 @@ def test_is_complex_dtype():
(np.dtype("float64"), np.dtype("float64")),
(str, np.dtype(str)),
(pd.Series([1, 2], dtype=np.dtype("int16")), np.dtype("int16")),
(pd.Series(["a", "b"]), np.dtype(object)),
(pd.Series(["a", "b"], dtype=object), np.dtype(object)),
(pd.Index([1, 2]), np.dtype("int64")),
(pd.Index(["a", "b"]), np.dtype(object)),
(pd.Index(["a", "b"], dtype=object), np.dtype(object)),
("category", "category"),
(pd.Categorical(["a", "b"]).dtype, CategoricalDtype(["a", "b"])),
(pd.Categorical(["a", "b"]), CategoricalDtype(["a", "b"])),
Expand Down Expand Up @@ -727,9 +727,9 @@ def test_get_dtype_fails(input_param, expected_error_message):
(np.dtype("float64"), np.float64),
(str, np.dtype(str).type),
(pd.Series([1, 2], dtype=np.dtype("int16")), np.int16),
(pd.Series(["a", "b"]), np.object_),
(pd.Series(["a", "b"], dtype=object), np.object_),
(pd.Index([1, 2], dtype="int64"), np.int64),
(pd.Index(["a", "b"]), np.object_),
(pd.Index(["a", "b"], dtype=object), np.object_),
("category", CategoricalDtypeType),
(pd.Categorical(["a", "b"]).dtype, CategoricalDtypeType),
(pd.Categorical(["a", "b"]), CategoricalDtypeType),
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,13 +1055,14 @@ def test_from_categorical_dtype_both(self):
)
assert result == CategoricalDtype([1, 2], ordered=False)

def test_str_vs_repr(self, ordered):
def test_str_vs_repr(self, ordered, using_infer_string):
c1 = CategoricalDtype(["a", "b"], ordered=ordered)
assert str(c1) == "category"
# Py2 will have unicode prefixes
dtype = "string" if using_infer_string else "object"
pat = (
r"CategoricalDtype\(categories=\[.*\], ordered={ordered}, "
r"categories_dtype=object\)"
rf"categories_dtype={dtype}\)"
)
assert re.match(pat.format(ordered=ordered), repr(c1))

Expand Down

0 comments on commit 2ed994f

Please sign in to comment.