Skip to content

TYP: make dtype required in _from_sequence_of_strings #56519

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 13 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ See :ref:`install.dependencies` and :ref:`install.optional_dependencies` for mor
Other API changes
^^^^^^^^^^^^^^^^^
- 3rd party ``py.path`` objects are no longer explicitly supported in IO methods. Use :py:class:`pathlib.Path` objects instead (:issue:`57091`)
- Made ``dtype`` a required argument in :meth:`ExtensionArray._from_sequence_of_strings` (:issue:`56519`)
-

.. ---------------------------------------------------------------------------
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ def floordiv_compat(
npt,
)

from pandas.core.dtypes.dtypes import ExtensionDtype

from pandas import Series
from pandas.core.arrays.datetimes import DatetimeArray
from pandas.core.arrays.timedeltas import TimedeltaArray
Expand Down Expand Up @@ -316,7 +318,7 @@ def _from_sequence(

@classmethod
def _from_sequence_of_strings(
cls, strings, *, dtype: Dtype | None = None, copy: bool = False
cls, strings, *, dtype: ExtensionDtype, copy: bool = False
Copy link
Member

@mroeschke mroeschke Feb 3, 2024

Choose a reason for hiding this comment

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

At least for arrow, dtype=None was supposed to mean "let pa.array infer the type", and now that can't be done?

) -> Self:
"""
Construct a new ExtensionArray from a sequence of strings.
Expand Down Expand Up @@ -533,8 +535,9 @@ def _box_pa_array(
):
# TODO: Move logic in _from_sequence_of_strings into
# _box_pa_array
dtype = ArrowDtype(pa_type)
return cls._from_sequence_of_strings(
value, dtype=pa_type
value, dtype=dtype
)._pa_array
else:
raise
Expand Down
7 changes: 4 additions & 3 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def _from_scalars(cls, scalars, *, dtype: DtypeObj) -> Self:

@classmethod
def _from_sequence_of_strings(
cls, strings, *, dtype: Dtype | None = None, copy: bool = False
cls, strings, *, dtype: ExtensionDtype, copy: bool = False
) -> Self:
"""
Construct a new ExtensionArray from a sequence of strings.
Expand All @@ -342,7 +342,7 @@ def _from_sequence_of_strings(
strings : Sequence
Each element will be an instance of the scalar type for this
array, ``cls.dtype.type``.
dtype : dtype, optional
dtype : ExtensionDtype
Construct for this particular dtype. This should be a Dtype
compatible with the ExtensionArray.
copy : bool, default False
Expand All @@ -354,7 +354,8 @@ def _from_sequence_of_strings(

Examples
--------
>>> pd.arrays.IntegerArray._from_sequence_of_strings(["1", "2", "3"])
>>> pd.arrays.IntegerArray._from_sequence_of_strings(
... ["1", "2", "3"], dtype=pd.Int64Dtype())
<IntegerArray>
[1, 2, 3]
Length: 3, dtype: Int64
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
import pyarrow

from pandas._typing import (
Dtype,
DtypeObj,
Self,
npt,
type_t,
)

from pandas.core.dtypes.dtypes import ExtensionDtype


@register_extension_dtype
class BooleanDtype(BaseMaskedDtype):
Expand Down Expand Up @@ -324,7 +325,7 @@ def _from_sequence_of_strings(
cls,
strings: list[str],
*,
dtype: Dtype | None = None,
dtype: ExtensionDtype,
copy: bool = False,
true_values: list[str] | None = None,
false_values: list[str] | None = None,
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@
import pyarrow

from pandas._typing import (
Dtype,
DtypeObj,
Self,
npt,
)

from pandas.core.dtypes.dtypes import ExtensionDtype


class NumericDtype(BaseMaskedDtype):
_default_np_dtype: np.dtype
Expand Down Expand Up @@ -270,7 +271,7 @@ def _coerce_to_array(

@classmethod
def _from_sequence_of_strings(
cls, strings, *, dtype: Dtype | None = None, copy: bool = False
cls, strings, *, dtype: ExtensionDtype, copy: bool = False
) -> Self:
from pandas.core.tools.numeric import to_numeric

Expand Down
4 changes: 3 additions & 1 deletion pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
npt,
)

from pandas.core.dtypes.dtypes import ExtensionDtype

from pandas.core.arrays import (
DatetimeArray,
TimedeltaArray,
Expand Down Expand Up @@ -304,7 +306,7 @@ def _from_sequence(

@classmethod
def _from_sequence_of_strings(
cls, strings, *, dtype: Dtype | None = None, copy: bool = False
cls, strings, *, dtype: ExtensionDtype, copy: bool = False
) -> Self:
return cls._from_sequence(strings, dtype=dtype, copy=copy)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def _from_sequence(

@classmethod
def _from_sequence_of_strings(
cls, strings, *, dtype: Dtype | None = None, copy: bool = False
cls, strings, *, dtype: ExtensionDtype, copy: bool = False
) -> Self:
return cls._from_sequence(strings, dtype=dtype, copy=copy)

Expand Down
4 changes: 3 additions & 1 deletion pandas/core/arrays/string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
npt,
)

from pandas.core.dtypes.dtypes import ExtensionDtype

from pandas import Series


Expand Down Expand Up @@ -204,7 +206,7 @@ def _from_sequence(

@classmethod
def _from_sequence_of_strings(
cls, strings, dtype: Dtype | None = None, copy: bool = False
cls, strings, *, dtype: ExtensionDtype, copy: bool = False
) -> Self:
return cls._from_sequence(strings, dtype=dtype, copy=copy)

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/arrays/boolean/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def test_coerce_to_numpy_array():
def test_to_boolean_array_from_strings():
result = BooleanArray._from_sequence_of_strings(
np.array(["True", "False", "1", "1.0", "0", "0.0", np.nan], dtype=object),
dtype="boolean",
dtype=pd.BooleanDtype(),
)
expected = BooleanArray(
np.array([True, False, True, True, False, False, False]),
Expand All @@ -255,7 +255,7 @@ def test_to_boolean_array_from_strings():

def test_to_boolean_array_from_strings_invalid_string():
with pytest.raises(ValueError, match="cannot be cast"):
BooleanArray._from_sequence_of_strings(["donkey"], dtype="boolean")
BooleanArray._from_sequence_of_strings(["donkey"], dtype=pd.BooleanDtype())


@pytest.mark.parametrize("box", [True, False], ids=["series", "array"])
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/decimal/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def _from_sequence(cls, scalars, *, dtype=None, copy=False):
return cls(scalars)

@classmethod
def _from_sequence_of_strings(cls, strings, dtype=None, copy=False):
def _from_sequence_of_strings(cls, strings, *, dtype: ExtensionDtype, copy=False):
return cls._from_sequence(
[decimal.Decimal(x) for x in strings], dtype=dtype, copy=copy
)
Expand Down
13 changes: 7 additions & 6 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,9 @@ def test_from_sequence_pa_array(self, data):
assert isinstance(result._pa_array, pa.ChunkedArray)

def test_from_sequence_pa_array_notimplemented(self, request):
dtype = ArrowDtype(pa.month_day_nano_interval())
with pytest.raises(NotImplementedError, match="Converting strings to"):
ArrowExtensionArray._from_sequence_of_strings(
["12-1"], dtype=pa.month_day_nano_interval()
)
ArrowExtensionArray._from_sequence_of_strings(["12-1"], dtype=dtype)

def test_from_sequence_of_strings_pa_array(self, data, request):
pa_dtype = data.dtype.pyarrow_dtype
Expand Down Expand Up @@ -2335,7 +2334,8 @@ def test_duration_from_strings_with_nat(unit):
# GH51175
strings = ["1000", "NaT"]
pa_type = pa.duration(unit)
result = ArrowExtensionArray._from_sequence_of_strings(strings, dtype=pa_type)
dtype = ArrowDtype(pa_type)
result = ArrowExtensionArray._from_sequence_of_strings(strings, dtype=dtype)
expected = ArrowExtensionArray(pa.array([1000, None], type=pa_type))
tm.assert_extension_array_equal(result, expected)

Expand Down Expand Up @@ -2854,13 +2854,14 @@ def test_from_sequence_of_strings_boolean():
[True] * len(true_strings) + [False] * len(false_strings) + [None] * len(nulls)
)

result = ArrowExtensionArray._from_sequence_of_strings(strings, dtype=pa.bool_())
dtype = ArrowDtype(pa.bool_())
result = ArrowExtensionArray._from_sequence_of_strings(strings, dtype=dtype)
expected = pd.array(bools, dtype="boolean[pyarrow]")
tm.assert_extension_array_equal(result, expected)

strings = ["True", "foo"]
with pytest.raises(pa.ArrowInvalid, match="Failed to parse"):
ArrowExtensionArray._from_sequence_of_strings(strings, dtype=pa.bool_())
ArrowExtensionArray._from_sequence_of_strings(strings, dtype=dtype)


def test_concat_empty_arrow_backed_series(dtype):
Expand Down