Skip to content

TST: [ArrowStringArray] more parameterised testing - part 1 #40679

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
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ _TYPE_MAP = {
"complex64": "complex",
"complex128": "complex",
"c": "complex",
"string": "string",
str: "string",
Copy link
Member Author

@simonjayhawkins simonjayhawkins Mar 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could add this instead of changing.

both StringDtype and ArrowStringDtype have type attrribute as str

at the moment the inference matches on name so this would need to change for 'string[python]' if we wanted to match on name

edit: kind -> type

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, this doesn't break anything? cause any perf issues?

"S": "bytes",
"U": "string",
"bool": "boolean",
Expand Down
18 changes: 18 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,24 @@ def string_dtype(request):
return request.param


@pytest.fixture(
params=[
"string",
pytest.param(
"arrow_string", marks=td.skip_if_no("pyarrow", min_version="1.0.0")
),
]
)
def nullable_string_dtype(request):
"""
Parametrized fixture for string dtypes.

* 'string'
* 'arrow_string'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will change in #39908, but getting the fixture in play as a pre-cursor should still be an advantage.

"""
return request.param


@pytest.fixture(params=tm.BYTES_DTYPES)
def bytes_dtype(request):
"""
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@ def test_is_string_dtype():
assert com.is_string_dtype(object)
assert com.is_string_dtype(np.array(["a", "b"]))
assert com.is_string_dtype(pd.StringDtype())
assert com.is_string_dtype(pd.array(["a", "b"], dtype="string"))


def test_is_string_dtype_nullable(nullable_string_dtype):
assert com.is_string_dtype(pd.array(["a", "b"], dtype=nullable_string_dtype))


integer_dtypes: List = []
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -1267,9 +1267,9 @@ def test_interval(self):
@pytest.mark.parametrize("klass", [pd.array, Series])
@pytest.mark.parametrize("skipna", [True, False])
@pytest.mark.parametrize("data", [["a", "b", "c"], ["a", "b", pd.NA]])
def test_string_dtype(self, data, skipna, klass):
def test_string_dtype(self, data, skipna, klass, nullable_string_dtype):
# StringArray
val = klass(data, dtype="string")
val = klass(data, dtype=nullable_string_dtype)
inferred = lib.infer_dtype(val, skipna=skipna)
assert inferred == "string"

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/extension/json/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
ExtensionDtype,
)
from pandas.api.types import is_bool_dtype
from pandas.core.arrays.string_arrow import ArrowStringDtype


class JSONDtype(ExtensionDtype):
Expand Down Expand Up @@ -193,7 +194,7 @@ def astype(self, dtype, copy=True):
if copy:
return self.copy()
return self
elif isinstance(dtype, StringDtype):
elif isinstance(dtype, (StringDtype, ArrowStringDtype)):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArrowStringDtype is removed in #39908 and therefore this change will be reverted.

value = self.astype(str) # numpy doesn'y like nested dicts
return dtype.construct_array_type()._from_sequence(value, copy=False)

Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/frame/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import numpy as np
import pytest

import pandas.util._test_decorators as td

import pandas as pd
from pandas import (
Categorical,
Expand Down Expand Up @@ -567,6 +569,10 @@ def test_astype_empty_dtype_dict(self):
"df",
[
DataFrame(Series(["x", "y", "z"], dtype="string")),
pytest.param(
DataFrame(Series(["x", "y", "z"], dtype="arrow_string")),
marks=td.skip_if_no("pyarrow", min_version="1.0.0"),
),
DataFrame(Series(["x", "y", "z"], dtype="category")),
DataFrame(Series(3 * [Timestamp("2020-01-01", tz="UTC")])),
DataFrame(Series(3 * [Interval(0, 1)])),
Expand Down
10 changes: 6 additions & 4 deletions pandas/tests/frame/methods/test_combine_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,17 @@ def test_combine_first_with_asymmetric_other(self, val):

tm.assert_frame_equal(res, exp)

def test_combine_first_string_dtype_only_na(self):
def test_combine_first_string_dtype_only_na(self, nullable_string_dtype):
# GH: 37519
df = DataFrame({"a": ["962", "85"], "b": [pd.NA] * 2}, dtype="string")
df2 = DataFrame({"a": ["85"], "b": [pd.NA]}, dtype="string")
df = DataFrame(
{"a": ["962", "85"], "b": [pd.NA] * 2}, dtype=nullable_string_dtype
)
df2 = DataFrame({"a": ["85"], "b": [pd.NA]}, dtype=nullable_string_dtype)
df.set_index(["a", "b"], inplace=True)
df2.set_index(["a", "b"], inplace=True)
result = df.combine_first(df2)
expected = DataFrame(
{"a": ["962", "85"], "b": [pd.NA] * 2}, dtype="string"
{"a": ["962", "85"], "b": [pd.NA] * 2}, dtype=nullable_string_dtype
).set_index(["a", "b"])
tm.assert_frame_equal(result, expected)

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1649,10 +1649,10 @@ def test_constructor_empty_with_string_dtype(self):
df = DataFrame(index=[0, 1], columns=[0, 1], dtype="U5")
tm.assert_frame_equal(df, expected)

def test_constructor_empty_with_string_extension(self):
def test_constructor_empty_with_string_extension(self, nullable_string_dtype):
# GH 34915
expected = DataFrame(index=[], columns=["c1"], dtype="string")
df = DataFrame(columns=["c1"], dtype="string")
expected = DataFrame(index=[], columns=["c1"], dtype=nullable_string_dtype)
df = DataFrame(columns=["c1"], dtype=nullable_string_dtype)
tm.assert_frame_equal(df, expected)

def test_constructor_single_value(self):
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/tools/test_to_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,9 +725,9 @@ def test_precision_float_conversion(strrep):
(["1", "2", "3.5"], Series([1, 2, 3.5])),
],
)
def test_to_numeric_from_nullable_string(values, expected):
def test_to_numeric_from_nullable_string(values, nullable_string_dtype, expected):
# https://github.com/pandas-dev/pandas/issues/37262
s = Series(values, dtype="string")
s = Series(values, dtype=nullable_string_dtype)
result = to_numeric(s)
tm.assert_series_equal(result, expected)

Expand Down