Skip to content

Commit e54dcdb

Browse files
committed
Code simplification and new comments about scalar type interpretation
1 parent f1fb54e commit e54dcdb

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

pandas/core/arrays/base.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@
4646
is_scalar,
4747
pandas_dtype,
4848
)
49-
from pandas.core.dtypes.dtypes import (
50-
ExtensionDtype,
51-
NumpyEADtype,
52-
)
49+
from pandas.core.dtypes.dtypes import ExtensionDtype
5350
from pandas.core.dtypes.generic import (
5451
ABCDataFrame,
5552
ABCIndex,
@@ -2490,16 +2487,15 @@ def map(self, mapper, na_action: Literal["ignore"] | None = None):
24902487
a MultiIndex will be returned.
24912488
"""
24922489
result = map_array(self, mapper, na_action=na_action)
2493-
if isinstance(result, ExtensionArray):
2494-
if isinstance(self.dtype, NumpyEADtype):
2495-
return pd_array(result, dtype=NumpyEADtype(result.dtype))
2496-
else:
2497-
return result
2498-
elif isinstance(result, np.ndarray):
2499-
result_types = set(np.array([type(x) for x in result]))
2500-
2501-
# if internal values types are compatible with self dtype
2502-
if all(issubclass(t, self.dtype.type) for t in result_types):
2490+
if isinstance(result, np.ndarray):
2491+
# Get the scalar types
2492+
scalar_types = set(np.array([type(x) for x in result]))
2493+
2494+
# if scalar values types are compatible with self dtype
2495+
# we use the self dtype
2496+
# For example if scalar types are dict and UserDict and self is a JSONArray,
2497+
# we use self.dtype
2498+
if all(issubclass(t, self.dtype.type) for t in scalar_types):
25032499
return pd_array(result, self.dtype)
25042500
else:
25052501
return pd_array(result, result.dtype)

0 commit comments

Comments
 (0)