@@ -356,6 +356,36 @@ def _from_sequence_of_strings(
356356 """
357357 raise AbstractMethodError (cls )
358358
359+ @classmethod
360+ def _from_factorized (cls , values , original ):
361+ """
362+ Reconstruct an ExtensionArray after factorization.
363+
364+ Parameters
365+ ----------
366+ values : ndarray
367+ An integer ndarray with the factorized values.
368+ original : ExtensionArray
369+ The original ExtensionArray that factorize was called on.
370+
371+ See Also
372+ --------
373+ factorize : Top-level factorize method that dispatches here.
374+ ExtensionArray.factorize : Encode the extension array as an enumerated type.
375+
376+ Examples
377+ --------
378+ >>> interv_arr = pd.arrays.IntervalArray(
379+ ... [pd.Interval(0, 1), pd.Interval(1, 5), pd.Interval(1, 5)]
380+ ... )
381+ >>> codes, uniques = pd.factorize(interv_arr)
382+ >>> pd.arrays.IntervalArray._from_factorized(uniques, interv_arr)
383+ <IntervalArray>
384+ [(0, 1], (1, 5]]
385+ Length: 2, dtype: interval[int64, right]
386+ """
387+ raise AbstractMethodError (cls )
388+
359389 @classmethod
360390 def _from_scalars (cls , scalars , * , dtype : DtypeObj ) -> Self :
361391 """
@@ -374,7 +404,7 @@ def _from_scalars(cls, scalars, *, dtype: DtypeObj) -> Self:
374404 Notes
375405 -----
376406 This is called in a try/except block when casting the result of a
377- pointwise operation.
407+ pointwise operation in ExtensionArray._cast_pointwise_result .
378408 """
379409 try :
380410 return cls ._from_sequence (scalars , dtype = dtype , copy = False )
@@ -388,37 +418,7 @@ def _from_scalars(cls, scalars, *, dtype: DtypeObj) -> Self:
388418 )
389419 raise
390420
391- @classmethod
392- def _from_factorized (cls , values , original ):
393- """
394- Reconstruct an ExtensionArray after factorization.
395-
396- Parameters
397- ----------
398- values : ndarray
399- An integer ndarray with the factorized values.
400- original : ExtensionArray
401- The original ExtensionArray that factorize was called on.
402-
403- See Also
404- --------
405- factorize : Top-level factorize method that dispatches here.
406- ExtensionArray.factorize : Encode the extension array as an enumerated type.
407-
408- Examples
409- --------
410- >>> interv_arr = pd.arrays.IntervalArray(
411- ... [pd.Interval(0, 1), pd.Interval(1, 5), pd.Interval(1, 5)]
412- ... )
413- >>> codes, uniques = pd.factorize(interv_arr)
414- >>> pd.arrays.IntervalArray._from_factorized(uniques, interv_arr)
415- <IntervalArray>
416- [(0, 1], (1, 5]]
417- Length: 2, dtype: interval[int64, right]
418- """
419- raise AbstractMethodError (cls )
420-
421- def _cast_pointwise_result (self , values : ArrayLike ) -> ArrayLike :
421+ def _cast_pointwise_result (self , values ) -> ArrayLike :
422422 """
423423 Construct an ExtensionArray after a pointwise operation.
424424
@@ -437,7 +437,8 @@ def _cast_pointwise_result(self, values: ArrayLike) -> ArrayLike:
437437 try :
438438 return type (self )._from_scalars (values , dtype = self .dtype )
439439 except (ValueError , TypeError ):
440- return values
440+ values = np .asarray (values , dtype = object )
441+ return lib .maybe_convert_objects (values , convert_non_numeric = True )
441442
442443 # ------------------------------------------------------------------------
443444 # Must be a Sequence
0 commit comments