Skip to content

REF: share astype exception messages #49655

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
Nov 16, 2022
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
6 changes: 4 additions & 2 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,10 +641,12 @@ def astype(self, dtype, copy: bool = True):

elif self.tz is None and isinstance(dtype, DatetimeTZDtype):
# pre-2.0 this did self.tz_localize(dtype.tz), which did not match
# the Series behavior
# the Series behavior which did
# values.tz_localize("UTC").tz_convert(dtype.tz)
raise TypeError(
"Cannot use .astype to convert from timezone-naive dtype to "
"timezone-aware dtype. Use obj.tz_localize instead."
"timezone-aware dtype. Use obj.tz_localize instead or "
"series.dt.tz_localize instead"
)

elif self.tz is not None and is_datetime64_dtype(dtype):
Expand Down
11 changes: 0 additions & 11 deletions pandas/core/dtypes/astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

from pandas.core.dtypes.common import (
is_datetime64_dtype,
is_datetime64tz_dtype,
is_dtype_equal,
is_integer_dtype,
is_object_dtype,
Expand Down Expand Up @@ -211,16 +210,6 @@ def astype_array(values: ArrayLike, dtype: DtypeObj, copy: bool = False) -> Arra
msg = rf"cannot astype a datetimelike from [{values.dtype}] to [{dtype}]"
raise TypeError(msg)

if is_datetime64tz_dtype(dtype) and is_datetime64_dtype(values.dtype):
# Series.astype behavior pre-2.0 did
# values.tz_localize("UTC").tz_convert(dtype.tz)
# which did not match the DTA/DTI behavior.
# We special-case here to give a Series-specific exception message.
raise TypeError(
"Cannot use .astype to convert from timezone-naive dtype to "
"timezone-aware dtype. Use ser.dt.tz_localize instead."
)

if is_dtype_equal(values.dtype, dtype):
if copy:
return values.copy()
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ def astype(self, dtype, copy: bool = True):
# NB: this must come before the ExtensionDtype check below
# TODO: this differs from Series behavior; can/should we align them?
raise TypeError(
f"Cannot convert Float64Index to dtype {dtype}; integer "
f"Cannot convert dtype={self.dtype} to dtype {dtype}; integer "
"values are required for conversion"
)

Expand Down
45 changes: 24 additions & 21 deletions pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from pandas._typing import (
ArrayLike,
DtypeArg,
DtypeObj,
Scalar,
)
from pandas.errors import (
Expand All @@ -61,14 +62,18 @@
is_string_dtype,
pandas_dtype,
)
from pandas.core.dtypes.dtypes import CategoricalDtype
from pandas.core.dtypes.dtypes import (
CategoricalDtype,
ExtensionDtype,
)
from pandas.core.dtypes.missing import isna

from pandas import StringDtype
from pandas.core import algorithms
from pandas.core.arrays import (
BooleanArray,
Categorical,
ExtensionArray,
FloatingArray,
IntegerArray,
)
Expand Down Expand Up @@ -599,14 +604,8 @@ def _convert_to_ndarrays(
# type specified in dtype param or cast_type is an EA
if cast_type and (not is_dtype_equal(cvals, cast_type) or is_ea):
if not is_ea and na_count > 0:
try:
if is_bool_dtype(cast_type):
raise ValueError(
f"Bool column has NA values in column {c}"
)
except (AttributeError, TypeError):
# invalid input to is_bool_dtype
pass
if is_bool_dtype(cast_type):
raise ValueError(f"Bool column has NA values in column {c}")
Copy link
Member

Choose a reason for hiding this comment

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

So are there new user-facing paths where this is raised now?

Copy link
Member Author

Choose a reason for hiding this comment

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

not AFAICT. the except (AttributeError, TypeError): this removes should be un-reachable

cast_type = pandas_dtype(cast_type)
cvals = self._cast_types(cvals, cast_type, c)

Expand Down Expand Up @@ -686,7 +685,7 @@ def _set(x) -> int:

def _infer_types(
self, values, na_values, no_dtype_specified, try_num_bool: bool = True
):
) -> tuple[ArrayLike, int]:
"""
Infer types of values, possibly casting

Expand All @@ -700,7 +699,7 @@ def _infer_types(

Returns
-------
converted : ndarray
converted : ndarray or ExtensionArray
na_count : int
"""
na_count = 0
Expand Down Expand Up @@ -777,48 +776,50 @@ def _infer_types(

return result, na_count

def _cast_types(self, values, cast_type, column):
def _cast_types(self, values: ArrayLike, cast_type: DtypeObj, column) -> ArrayLike:
"""
Cast values to specified type

Parameters
----------
values : ndarray
cast_type : string or np.dtype
values : ndarray or ExtensionArray
cast_type : np.dtype or ExtensionDtype
dtype to cast values to
column : string
column name - used only for error reporting

Returns
-------
converted : ndarray
converted : ndarray or ExtensionArray
"""
if is_categorical_dtype(cast_type):
known_cats = (
isinstance(cast_type, CategoricalDtype)
and cast_type.categories is not None
)

if not is_object_dtype(values) and not known_cats:
if not is_object_dtype(values.dtype) and not known_cats:
# TODO: this is for consistency with
# c-parser which parses all categories
# as strings

values = astype_nansafe(values, np.dtype(str))
values = lib.ensure_string_array(
values, skipna=False, convert_na_value=False
)

cats = Index(values).unique().dropna()
values = Categorical._from_inferred_categories(
cats, cats.get_indexer(values), cast_type, true_values=self.true_values
)

# use the EA's implementation of casting
elif is_extension_array_dtype(cast_type):
# ensure cast_type is an actual dtype and not a string
cast_type = pandas_dtype(cast_type)
elif isinstance(cast_type, ExtensionDtype):
array_type = cast_type.construct_array_type()
try:
if is_bool_dtype(cast_type):
return array_type._from_sequence_of_strings(
# error: Unexpected keyword argument "true_values" for
# "_from_sequence_of_strings" of "ExtensionArray"
return array_type._from_sequence_of_strings( # type: ignore[call-arg] # noqa:E501
values,
dtype=cast_type,
true_values=self.true_values,
Expand All @@ -832,6 +833,8 @@ def _cast_types(self, values, cast_type, column):
"_from_sequence_of_strings in order to be used in parser methods"
) from err

elif isinstance(values, ExtensionArray):
values = values.astype(cast_type, copy=False)
else:
try:
values = astype_nansafe(values, cast_type, copy=True, skipna=True)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def test_astype_copies(self, dtype, other):

if err:
if dtype == "datetime64[ns]":
msg = "Use ser.dt.tz_localize instead"
msg = "Use obj.tz_localize instead or series.dt.tz_localize instead"
else:
msg = "from timezone-aware dtype to timezone-naive dtype"
with pytest.raises(TypeError, match=msg):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/numeric/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_cannot_cast_to_datetimelike(self, dtype):
idx = Float64Index([0, 1.1, 2])

msg = (
f"Cannot convert Float64Index to dtype {pandas_dtype(dtype)}; "
f"Cannot convert dtype=float64 to dtype {pandas_dtype(dtype)}; "
f"integer values are required for conversion"
)
with pytest.raises(TypeError, match=re.escape(msg)):
Expand Down