Skip to content

Infer extensiondtype from scalar #59767

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 5 commits 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
6 changes: 6 additions & 0 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ from pandas._libs.tslibs.period cimport is_period_object
from pandas._libs.tslibs.timedeltas cimport convert_to_timedelta64
from pandas._libs.tslibs.timezones cimport tz_compare

from pandas.core.dtypes.base import _registry

# constants that will be compared to potentially arbitrarily large
# python int
cdef:
Expand Down Expand Up @@ -1693,6 +1695,10 @@ def infer_dtype(value: object, skipna: bool = True) -> str:
if is_interval_array(values):
return "interval"

reg_dtype = _registry.match_scalar(val)
if reg_dtype:
return str(reg_dtype)

cnp.PyArray_ITER_RESET(it)
for i in range(n):
val = PyArray_GETITEM(values, PyArray_ITER_DATA(it))
Expand Down
20 changes: 20 additions & 0 deletions pandas/core/dtypes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,18 @@ def _can_fast_transpose(self) -> bool:
"""
return False

@classmethod
def is_unambiguous_scalar(cls, scalar):
return False

@classmethod
def construct_from_scalar(cls, scalar):
return cls()

@property
def is_external_dtype(self) -> bool:
return self.__module__[:8] == "pandas.c"


class StorageExtensionDtype(ExtensionDtype):
"""ExtensionDtype that may be backed by more than one implementation."""
Expand Down Expand Up @@ -582,5 +594,13 @@ def find(

return None

def match_scalar(
self, scalar: Any
) -> type_t[ExtensionDtype] | ExtensionDtype | None:
for dtype in self.dtypes:
if dtype.is_unambiguous_scalar(scalar):
return dtype.construct_from_scalar(scalar)
return None


_registry = Registry()
11 changes: 11 additions & 0 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
LossySetitemError,
)

from pandas.core.dtypes.base import _registry
from pandas.core.dtypes.common import (
ensure_int8,
ensure_int16,
Expand Down Expand Up @@ -857,6 +858,10 @@ def infer_dtype_from_scalar(val) -> tuple[DtypeObj, Any]:
subtype = infer_dtype_from_scalar(val.left)[0]
dtype = IntervalDtype(subtype=subtype, closed=val.closed)

reg_dtype = _registry.match_scalar(val)
if reg_dtype:
dtype = reg_dtype

return dtype, val


Expand Down Expand Up @@ -913,6 +918,12 @@ def infer_dtype_from_array(arr) -> tuple[DtypeObj, ArrayLike]:
inferred = lib.infer_dtype(arr, skipna=False)
if inferred in ["string", "bytes", "mixed", "mixed-integer"]:
return (np.dtype(np.object_), arr)
elif inferred in ["empty", "integer", "floating", "integer-na", "mixed-integer-float", "datetime", "period", "timedelta", "time", "date"]:
pass
else:
arr_dtype = pandas_dtype_func(inferred)
if isinstance(arr_dtype, ExtensionDtype):
return arr_dtype, arr

arr = np.asarray(arr)
return arr.dtype, arr
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,11 @@ def __init__(
elif copy:
data = data.copy()
else:
if dtype is None:
inferred_dtype = infer_dtype_from(data)[0]
if isinstance(inferred_dtype, ExtensionDtype) and inferred_dtype.is_external_dtype:
dtype = inferred_dtype
# import pdb; pdb.set_trace()
data = sanitize_array(data, index, dtype, copy)
data = SingleBlockManager.from_array(data, index, refs=refs)

Expand Down
30 changes: 29 additions & 1 deletion pandas/tests/dtypes/cast/test_infer_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,33 @@
Timestamp,
date_range,
)

from pandas.core.dtypes.dtypes import ExtensionDtype, register_extension_dtype


class MockScalar:
pass

@register_extension_dtype
class MockDtype(ExtensionDtype):
@property
def name(self):
return "MockDtype"
def is_unambiguous_scalar(scalar):
if isinstance(scalar, MockScalar):
return True
return False

@classmethod
def construct_from_string(cls, string: str):
if not isinstance(string, str):
raise TypeError(
f"'construct_from_string' expects a string, got {type(string)}"
)

if string == cls.__name__:
return cls()
else:
raise TypeError(f"Cannot construct a '{cls.__name__}' from '{string}'")

def test_infer_dtype_from_int_scalar(any_int_numpy_dtype):
# Test that infer_dtype_from_scalar is
Expand Down Expand Up @@ -157,6 +183,7 @@ def test_infer_dtype_from_scalar_errors():
(np.datetime64("2016-01-01"), np.dtype("M8[s]")),
(Timestamp("20160101"), np.dtype("M8[s]")),
(Timestamp("20160101", tz="UTC"), "datetime64[s, UTC]"),
(MockScalar(), MockDtype())
],
)
def test_infer_dtype_from_scalar(value, expected, using_infer_string):
Expand Down Expand Up @@ -189,6 +216,7 @@ def test_infer_dtype_from_scalar(value, expected, using_infer_string):
Series(date_range("20160101", periods=3, tz="US/Eastern")),
"datetime64[ns, US/Eastern]",
),
([MockScalar()], MockDtype())
],
)
def test_infer_dtype_from_array(arr, expected, using_infer_string):
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
FloatingArray,
IntegerArray,
)
from pandas.core.dtypes.dtypes import ExtensionDtype, register_extension_dtype


@pytest.fixture(params=[True, False], ids=str)
Expand Down Expand Up @@ -2025,3 +2026,19 @@ def test_find_result_type_int_int(right, result):
def test_find_result_type_floats(right, result):
left_dtype = np.dtype("float16")
assert find_result_type(left_dtype, right) == result

def test_infer_dtype_extensiondtype():
class MockScalar:
pass

@register_extension_dtype
class MockDtype(ExtensionDtype):
@property
def name(self):
return "MockDtype"
def is_unambiguous_scalar(scalar):
if isinstance(scalar, MockScalar):
return True
return False
arr = [MockScalar()]
assert lib.infer_dtype(arr, skipna=True) == "MockDtype"
61 changes: 60 additions & 1 deletion pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,62 @@
from pandas.core.arrays import (
IntegerArray,
IntervalArray,
period_array,
period_array,ExtensionArray
)
from pandas.core.internals.blocks import NumpyBlock

from pandas.core.dtypes.dtypes import ExtensionDtype, register_extension_dtype

class MockScalar:
pass

@register_extension_dtype
class MockDtype(ExtensionDtype):
type = MockScalar
@property
def name(self):
return "MockDtype"
def is_unambiguous_scalar(scalar):
if isinstance(scalar, MockScalar):
return True
return False

@classmethod
def construct_from_string(cls, string: str):
if not isinstance(string, str):
raise TypeError(
f"'construct_from_string' expects a string, got {type(string)}"
)

if string == cls.__name__:
return cls()
else:
raise TypeError(f"Cannot construct a '{cls.__name__}' from '{string}'")

@classmethod
def construct_array_type(cls):
"""
Return the array type associated with this dtype.

Returns
-------
type
"""
return MockArray

@property
def is_external_dtype(self):
return True


from pandas.core.arrays._mixins import NDArrayBackedExtensionArray
class MockArray(NDArrayBackedExtensionArray):
dtype = MockDtype()
@classmethod
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
scalars = np.ndarray([0 for i in scalars])
return cls(scalars, "O")


class TestSeriesConstructors:
def test_from_ints_with_non_nano_dt64_dtype(self, index_or_series):
Expand Down Expand Up @@ -152,6 +204,13 @@ def test_scalar_extension_dtype(self, ea_scalar_and_dtype):
assert ser.dtype == ea_dtype
tm.assert_series_equal(ser, expected)


def test_unambiguous_scalar(self):
ea_scalar, ea_dtype = MockScalar(), MockDtype()

ser = Series(ea_scalar, index=range(3))
assert ser.dtype == ea_dtype

def test_constructor(self, datetime_series, using_infer_string):
empty_series = Series()
assert datetime_series.index._is_all_dates
Expand Down
Loading