Skip to content

ENH: allow opt-in to inferring pyarrow strings #54342

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

Closed
wants to merge 1 commit into from
Closed
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
38 changes: 38 additions & 0 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,7 @@ cdef class Seen:
bint datetimetz_ # seen_datetimetz
bint period_ # seen_period
bint interval_ # seen_interval
bint str_ # seen_str

def __cinit__(self, bint coerce_numeric=False):
"""
Expand All @@ -1325,6 +1326,7 @@ cdef class Seen:
self.datetimetz_ = False
self.period_ = False
self.interval_ = False
self.str_ = False
self.coerce_numeric = coerce_numeric

cdef bint check_uint64_conflict(self) except -1:
Expand Down Expand Up @@ -2615,6 +2617,13 @@ def maybe_convert_objects(ndarray[object] objects,
else:
seen.object_ = True
break
elif isinstance(val, str):
if convert_non_numeric:
seen.str_ = True
break
else:
seen.object_ = True
break
else:
seen.object_ = True
break
Expand Down Expand Up @@ -2669,6 +2678,35 @@ def maybe_convert_objects(ndarray[object] objects,
return pi._data
seen.object_ = True

elif seen.str_:
if is_string_array(objects):
from pandas._config import get_option
opt = get_option("future.infer_string")
if opt is True:
import pyarrow as pa

from pandas.core.dtypes.dtypes import ArrowDtype

obj = pa.array(objects)
dtype = ArrowDtype(obj.type)
return dtype.construct_array_type()(obj)
# elif opt is False:
# # explicitly set to keep the old behavior and avoid the warning
# pass
# else:
# from pandas.util._exceptions import find_stack_level
# warnings.warn(
# "Pandas type inference with a sequence of `str` "
# "objects is deprecated. In a future version, this will give "
# "string[pyarrow] dtype, which will require pyarrow to be "
# "installed. To opt in to the new behavior immediately set "
# "`pd.set_option('future.infer_string', True)`. To keep the "
# "old behavior pass `dtype=object`.",
# FutureWarning,
# stacklevel=find_stack_level(),
# )

seen.object_ = True
elif seen.interval_:
if is_interval_array(objects):
from pandas import IntervalIndex
Expand Down
11 changes: 11 additions & 0 deletions pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,3 +889,14 @@ def register_converter_cb(key) -> None:
styler_environment,
validator=is_instance_factory([type(None), str]),
)


with cf.config_prefix("future"):
cf.register_option(
"future.infer_string",
None,
"Whether to infer sequence of str objects as pyarrow string "
"dtype, which will be the default in pandas 3.0 "
"(at which point this option will be deprecated).",
validator=is_one_of_factory([True, False, None]),
)
19 changes: 19 additions & 0 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import numpy as np

from pandas._config import get_option

from pandas._libs import lib
from pandas._libs.missing import (
NA,
Expand Down Expand Up @@ -796,6 +798,23 @@ def infer_dtype_from_scalar(val) -> tuple[DtypeObj, Any]:
# coming out as np.str_!

dtype = _dtype_obj
opt = get_option("future.infer_string")
if opt is True:
import pyarrow as pa

pa_dtype = pa.string()
dtype = ArrowDtype(pa_dtype)
# elif opt is None:
# warnings.warn(
# "Pandas type inference with a `str` "
# "object is deprecated. In a future version, this will give "
# "string[pyarrow] dtype, which will require pyarrow to be "
# "installed. To opt in to the new behavior immediately set "
# "`pd.set_option('future.infer_string', True)`. To keep the "
# "old behavior pass `dtype=object`.",
# FutureWarning,
# stacklevel=find_stack_level(),
# )

elif isinstance(val, (np.datetime64, dt.datetime)):
try:
Expand Down