Skip to content

Commit 2f992fd

Browse files
committed
test
1 parent 5878fab commit 2f992fd

File tree

4 files changed

+106
-5
lines changed

4 files changed

+106
-5
lines changed

pandas/core/dtypes/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ def construct_from_scalar(cls, scalar):
454454

455455
@property
456456
def is_external_dtype(self) -> bool:
457-
return not self.__module__.split(".")[0] == "pandas"
457+
return self.__module__[:8] == "pandas.c"
458458

459459

460460
class StorageExtensionDtype(ExtensionDtype):

pandas/tests/dtypes/cast/test_infer_dtype.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,33 @@
2323
Timestamp,
2424
date_range,
2525
)
26-
26+
from pandas.core.dtypes.dtypes import ExtensionDtype, register_extension_dtype
27+
28+
29+
class MockScalar:
30+
pass
31+
32+
@register_extension_dtype
33+
class MockDtype(ExtensionDtype):
34+
@property
35+
def name(self):
36+
return "MockDtype"
37+
def is_unambiguous_scalar(scalar):
38+
if isinstance(scalar, MockScalar):
39+
return True
40+
return False
41+
42+
@classmethod
43+
def construct_from_string(cls, string: str):
44+
if not isinstance(string, str):
45+
raise TypeError(
46+
f"'construct_from_string' expects a string, got {type(string)}"
47+
)
48+
49+
if string == cls.__name__:
50+
return cls()
51+
else:
52+
raise TypeError(f"Cannot construct a '{cls.__name__}' from '{string}'")
2753

2854
def test_infer_dtype_from_int_scalar(any_int_numpy_dtype):
2955
# Test that infer_dtype_from_scalar is
@@ -157,6 +183,7 @@ def test_infer_dtype_from_scalar_errors():
157183
(np.datetime64("2016-01-01"), np.dtype("M8[s]")),
158184
(Timestamp("20160101"), np.dtype("M8[s]")),
159185
(Timestamp("20160101", tz="UTC"), "datetime64[s, UTC]"),
186+
(MockScalar(), MockDtype())
160187
],
161188
)
162189
def test_infer_dtype_from_scalar(value, expected, using_infer_string):
@@ -189,6 +216,7 @@ def test_infer_dtype_from_scalar(value, expected, using_infer_string):
189216
Series(date_range("20160101", periods=3, tz="US/Eastern")),
190217
"datetime64[ns, US/Eastern]",
191218
),
219+
([MockScalar()], MockDtype())
192220
],
193221
)
194222
def test_infer_dtype_from_array(arr, expected, using_infer_string):

pandas/tests/dtypes/test_inference.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@
7575
FloatingArray,
7676
IntegerArray,
7777
)
78-
from pandas.core.dtypes.dtypes import ExtensionDtype
78+
from pandas.core.dtypes.dtypes import ExtensionDtype, register_extension_dtype
7979

80-
from pandas.core
8180

8281
@pytest.fixture(params=[True, False], ids=str)
8382
def coerce(request):
@@ -2032,6 +2031,7 @@ def test_infer_dtype_extensiondtype():
20322031
class MockScalar:
20332032
pass
20342033

2034+
@register_extension_dtype
20352035
class MockDtype(ExtensionDtype):
20362036
@property
20372037
def name(self):

pandas/tests/series/test_constructors.py

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,62 @@
4444
from pandas.core.arrays import (
4545
IntegerArray,
4646
IntervalArray,
47-
period_array,
47+
period_array,ExtensionArray
4848
)
4949
from pandas.core.internals.blocks import NumpyBlock
5050

51+
from pandas.core.dtypes.dtypes import ExtensionDtype, register_extension_dtype
52+
53+
class MockScalar:
54+
pass
55+
56+
@register_extension_dtype
57+
class MockDtype(ExtensionDtype):
58+
type = MockScalar
59+
@property
60+
def name(self):
61+
return "MockDtype"
62+
def is_unambiguous_scalar(scalar):
63+
if isinstance(scalar, MockScalar):
64+
return True
65+
return False
66+
67+
@classmethod
68+
def construct_from_string(cls, string: str):
69+
if not isinstance(string, str):
70+
raise TypeError(
71+
f"'construct_from_string' expects a string, got {type(string)}"
72+
)
73+
74+
if string == cls.__name__:
75+
return cls()
76+
else:
77+
raise TypeError(f"Cannot construct a '{cls.__name__}' from '{string}'")
78+
79+
@classmethod
80+
def construct_array_type(cls):
81+
"""
82+
Return the array type associated with this dtype.
83+
84+
Returns
85+
-------
86+
type
87+
"""
88+
return MockArray
89+
90+
@property
91+
def is_external_dtype(self):
92+
return True
93+
94+
95+
from pandas.core.arrays._mixins import NDArrayBackedExtensionArray
96+
class MockArray(NDArrayBackedExtensionArray):
97+
dtype = MockDtype()
98+
@classmethod
99+
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
100+
scalars = np.ndarray([0 for i in scalars])
101+
return cls(scalars, "O")
102+
51103

52104
class TestSeriesConstructors:
53105
def test_from_ints_with_non_nano_dt64_dtype(self, index_or_series):
@@ -152,6 +204,27 @@ def test_scalar_extension_dtype(self, ea_scalar_and_dtype):
152204
assert ser.dtype == ea_dtype
153205
tm.assert_series_equal(ser, expected)
154206

207+
208+
def test_scalar_extension_dtype2(self):
209+
# GH 28401
210+
from pandas.core.dtypes.cast import (
211+
LossySetitemError,
212+
construct_1d_arraylike_from_scalar,
213+
find_common_type,
214+
infer_dtype_from,
215+
maybe_box_native,
216+
maybe_cast_pointwise_result,
217+
)
218+
ea_scalar, ea_dtype = MockScalar(), MockDtype()
219+
220+
# import pdb; pdb.set_trace()
221+
infer_dtype_from(MockScalar())
222+
ser = Series(ea_scalar, index=range(3))
223+
expected = Series([ea_scalar] * 3, dtype=ea_dtype)
224+
225+
assert ser.dtype == ea_dtype
226+
# tm.assert_series_equal(ser, expected)
227+
155228
def test_constructor(self, datetime_series, using_infer_string):
156229
empty_series = Series()
157230
assert datetime_series.index._is_all_dates

0 commit comments

Comments
 (0)