Skip to content

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

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 7 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
23 changes: 23 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,20 @@ def maybe_convert_objects(ndarray[object] objects,
return pi._data
seen.object_ = True

elif seen.str_:
if is_string_array(objects, skipna=True):
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, from_pandas=True)
dtype = ArrowDtype(obj.type)
return dtype.construct_array_type()(obj)

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(
"infer_string",
False,
"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]),
)
8 changes: 8 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,12 @@ 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)
Copy link
Member

Choose a reason for hiding this comment

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

are there any tests that hit this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added one


elif isinstance(val, (np.datetime64, dt.datetime)):
try:
Expand Down
35 changes: 35 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2670,6 +2670,41 @@ def test_construct_with_strings_and_none(self):
expected = DataFrame({"a": ["1", "2", None]}, dtype="str")
tm.assert_frame_equal(df, expected)

def test_frame_string_inference(self):
# GH#54430
pa = pytest.importorskip("pyarrow")
dtype = pd.ArrowDtype(pa.string())
expected = DataFrame(
{"a": ["a", "b"]}, dtype=dtype, columns=Index(["a"], dtype=dtype)
)
with pd.option_context("future.infer_string", True):
df = DataFrame({"a": ["a", "b"]})
tm.assert_frame_equal(df, expected)

expected = DataFrame(
{"a": ["a", "b"]},
dtype=dtype,
columns=Index(["a"], dtype=dtype),
index=Index(["x", "y"], dtype=dtype),
)
with pd.option_context("future.infer_string", True):
df = DataFrame({"a": ["a", "b"]}, index=["x", "y"])
tm.assert_frame_equal(df, expected)

expected = DataFrame(
{"a": ["a", 1]}, dtype="object", columns=Index(["a"], dtype=dtype)
)
with pd.option_context("future.infer_string", True):
df = DataFrame({"a": ["a", 1]})
tm.assert_frame_equal(df, expected)

expected = DataFrame(
{"a": ["a", "b"]}, dtype="object", columns=Index(["a"], dtype=dtype)
)
with pd.option_context("future.infer_string", True):
df = DataFrame({"a": ["a", "b"]}, dtype="object")
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a case with null/nan/None in it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added, needed a fix...

tm.assert_frame_equal(df, expected)


class TestDataFrameConstructorIndexInference:
def test_frame_from_dict_of_series_overlapping_monthly_period_indexes(self):
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/indexes/base_class/test_constructors.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
from pandas import (
Index,
MultiIndex,
Expand Down Expand Up @@ -42,3 +43,17 @@ def test_construct_empty_tuples(self, tuple_list):
expected = MultiIndex.from_tuples(tuple_list)

tm.assert_index_equal(result, expected)

def test_index_string_inference(self):
# GH#54430
pa = pytest.importorskip("pyarrow")
dtype = pd.ArrowDtype(pa.string())
expected = Index(["a", "b"], dtype=dtype)
with pd.option_context("future.infer_string", True):
ser = Index(["a", "b"])
tm.assert_index_equal(ser, expected)

expected = Index(["a", 1], dtype="object")
with pd.option_context("future.infer_string", True):
ser = Index(["a", 1])
tm.assert_index_equal(ser, expected)
17 changes: 17 additions & 0 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2094,3 +2094,20 @@ def test_pyarrow_engine_lines_false():
out = ser.to_json()
with pytest.raises(ValueError, match="currently pyarrow engine only supports"):
read_json(out, engine="pyarrow", lines=False)


def test_json_roundtrip_string_inference(orient):
pa = pytest.importorskip("pyarrow")
df = DataFrame(
[["a", "b"], ["c", "d"]], index=["row 1", "row 2"], columns=["col 1", "col 2"]
)
out = df.to_json()
with pd.option_context("future.infer_string", True):
result = read_json(StringIO(out))
expected = DataFrame(
[["a", "b"], ["c", "d"]],
dtype=pd.ArrowDtype(pa.string()),
index=pd.Index(["row 1", "row 2"], dtype=pd.ArrowDtype(pa.string())),
columns=pd.Index(["col 1", "col 2"], dtype=pd.ArrowDtype(pa.string())),
)
tm.assert_frame_equal(result, expected)
21 changes: 21 additions & 0 deletions pandas/tests/io/parser/dtypes/test_dtypes_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,3 +538,24 @@ def test_ea_int_avoid_overflow(all_parsers):
}
)
tm.assert_frame_equal(result, expected)


def test_string_inference(all_parsers):
# GH#54430
pa = pytest.importorskip("pyarrow")
dtype = pd.ArrowDtype(pa.string())

data = """a,b
x,1
y,2"""
parser = all_parsers
if parser.engine == "pyarrow":
pytest.skip("TODO: Follow up")
Copy link
Member

Choose a reason for hiding this comment

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

Did we ever discuss pyarrow backed engines returning pyarrow types by default? I think from a user perspective it is less than ideal to have to specify both this option and the dtype backend for any pyarrow backed IO methods

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's move this discussion to Basel, but there is an issue about this: #51846

Copy link
Member

Choose a reason for hiding this comment

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

Could you link the GH issue in this skip message?

Copy link
Member Author

Choose a reason for hiding this comment

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

#54431 already addresses this

with pd.option_context("future.infer_string", True):
result = parser.read_csv(StringIO(data))

expected = DataFrame(
{"a": pd.Series(["x", "y"], dtype=dtype), "b": [1, 2]},
columns=pd.Index(["a", "b"], dtype=dtype),
)
tm.assert_frame_equal(result, expected)
17 changes: 17 additions & 0 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2920,6 +2920,23 @@ def test_read_sql_dtype_backend_table(self, string_storage, func):
# GH#50048 Not supported for sqlite
pass

def test_read_sql_string_inference(self):
# GH#54430
pa = pytest.importorskip("pyarrow")
table = "test"
df = DataFrame({"a": ["x", "y"]})
df.to_sql(table, self.conn, index=False, if_exists="replace")

with pd.option_context("future.infer_string", True):
result = read_sql_table(table, self.conn)

dtype = pd.ArrowDtype(pa.string())
expected = DataFrame(
{"a": ["x", "y"]}, dtype=dtype, columns=Index(["a"], dtype=dtype)
)

tm.assert_frame_equal(result, expected)


@pytest.mark.db
class TestMySQLAlchemy(_TestSQLAlchemy):
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2070,6 +2070,30 @@ def test_series_from_index_dtype_equal_does_not_copy(self):
ser.iloc[0] = 100
tm.assert_index_equal(idx, expected)

def test_series_string_inference(self):
# GH#54430
pa = pytest.importorskip("pyarrow")
dtype = pd.ArrowDtype(pa.string())
expected = Series(["a", "b"], dtype=dtype)
with pd.option_context("future.infer_string", True):
ser = Series(["a", "b"])
tm.assert_series_equal(ser, expected)

expected = Series(["a", 1], dtype="object")
with pd.option_context("future.infer_string", True):
ser = Series(["a", 1])
tm.assert_series_equal(ser, expected)

@pytest.mark.parametrize("na_value", [None, np.nan, pd.NA])
def test_series_string_with_na_inference(self, na_value):
# GH#54430
pa = pytest.importorskip("pyarrow")
dtype = pd.ArrowDtype(pa.string())
expected = Series(["a", na_value], dtype=dtype)
with pd.option_context("future.infer_string", True):
ser = Series(["a", na_value])
tm.assert_series_equal(ser, expected)


class TestSeriesConstructorIndexCoercion:
def test_series_constructor_datetimelike_index_coercion(self):
Expand Down