Skip to content

Commit 53a885c

Browse files
committed
Code refacto and clean up
1 parent ebb8d26 commit 53a885c

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

pandas/_libs/lib.pyx

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2483,7 +2483,7 @@ def _convert_to_based_masked(
24832483

24842484
@cython.boundscheck(False)
24852485
@cython.wraparound(False)
2486-
def _seen_to_numpy_dtype(Seen seen, object scalar_type):
2486+
def _maybe_get_numpy_dtype(Seen seen, object scalar_type):
24872487
# Numpy scalar type
24882488
if issubclass(scalar_type, np.generic):
24892489
return np.dtype(scalar_type)
@@ -2500,6 +2500,25 @@ def _seen_to_numpy_dtype(Seen seen, object scalar_type):
25002500
return None
25012501

25022502

2503+
@cython.boundscheck(False)
2504+
@cython.wraparound(False)
2505+
def _maybe_get_based_masked_scalar_numpy_dtype(
2506+
val_types,
2507+
seen,
2508+
convert_to_nullable_dtype):
2509+
# If we have no type or more than one type we cannot build a based masked array
2510+
if not val_types or len(val_types) > 1:
2511+
return None
2512+
2513+
numpy_dtype = _maybe_get_numpy_dtype(seen, val_types.pop())
2514+
if (
2515+
numpy_dtype and numpy_dtype.kind in "biuf"
2516+
and convert_to_nullable_dtype):
2517+
return numpy_dtype
2518+
else:
2519+
return None
2520+
2521+
25032522
@cython.boundscheck(False)
25042523
@cython.wraparound(False)
25052524
def maybe_convert_objects(ndarray[object] objects,
@@ -2715,13 +2734,13 @@ def maybe_convert_objects(ndarray[object] objects,
27152734
if storage == "pyarrow":
27162735
return _convert_to_pyarrow(objects, mask, na_value)
27172736

2718-
numpy_dtype = None
2719-
if len(val_types) == 1:
2720-
numpy_dtype = _seen_to_numpy_dtype(seen, val_types.pop())
2721-
if (
2722-
numpy_dtype and numpy_dtype.kind in "biuf"
2723-
and convert_to_nullable_dtype):
2724-
return _convert_to_based_masked(objects, numpy_dtype)
2737+
based_masked_scalar_numpy_dtype = _maybe_get_based_masked_scalar_numpy_dtype(
2738+
val_types,
2739+
seen,
2740+
convert_to_nullable_dtype)
2741+
2742+
if based_masked_scalar_numpy_dtype:
2743+
return _convert_to_based_masked(objects, based_masked_scalar_numpy_dtype)
27252744

27262745
# we try to coerce datetime w/tz but must all have the same tz
27272746
if seen.datetimetz_:
@@ -2789,10 +2808,7 @@ def maybe_convert_objects(ndarray[object] objects,
27892808
elif storage == "python":
27902809
from pandas.core.arrays.string_ import StringDtype
27912810

2792-
if mask is not None and any(mask):
2793-
dtype = StringDtype(storage=storage, na_value=objects[mask][0])
2794-
else:
2795-
dtype = StringDtype(storage=storage)
2811+
dtype = StringDtype(storage=storage, na_value=na_value)
27962812
return dtype.construct_array_type()._from_sequence(objects, dtype=dtype)
27972813

27982814
seen.object_ = True

0 commit comments

Comments
 (0)