1010
1111import numpy as np
1212
13- from pandas ._libs import (
14- NaT ,
15- internals as libinternals ,
16- )
17- from pandas ._libs .missing import NA
13+ from pandas ._libs import internals as libinternals
1814from pandas ._typing import (
1915 ArrayLike ,
2016 DtypeObj ,
3228 is_1d_only_ea_obj ,
3329 is_datetime64tz_dtype ,
3430 is_dtype_equal ,
35- needs_i8_conversion ,
3631)
3732from pandas .core .dtypes .concat import (
3833 cast_to_common_type ,
3934 concat_compat ,
4035)
4136from pandas .core .dtypes .dtypes import ExtensionDtype
42- from pandas .core .dtypes .missing import is_valid_na_for_dtype
4337
4438import pandas .core .algorithms as algos
4539from pandas .core .arrays import (
@@ -381,36 +375,6 @@ def dtype(self):
381375 return blk .dtype
382376 return ensure_dtype_can_hold_na (blk .dtype )
383377
384- def _is_valid_na_for (self , dtype : DtypeObj ) -> bool :
385- """
386- Check that we are all-NA of a type/dtype that is compatible with this dtype.
387- Augments `self.is_na` with an additional check of the type of NA values.
388- """
389- if not self .is_na :
390- return False
391- if self .block .dtype .kind == "V" :
392- return True
393-
394- if self .dtype == object :
395- values = self .block .values
396- return all (is_valid_na_for_dtype (x , dtype ) for x in values .ravel (order = "K" ))
397-
398- na_value = self .block .fill_value
399- if na_value is NaT and not is_dtype_equal (self .dtype , dtype ):
400- # e.g. we are dt64 and other is td64
401- # fill_values match but we should not cast self.block.values to dtype
402- # TODO: this will need updating if we ever have non-nano dt64/td64
403- return False
404-
405- if na_value is NA and needs_i8_conversion (dtype ):
406- # FIXME: kludge; test_append_empty_frame_with_timedelta64ns_nat
407- # e.g. self.dtype == "Int64" and dtype is td64, we dont want
408- # to consider these as matching
409- return False
410-
411- # TODO: better to use can_hold_element?
412- return is_valid_na_for_dtype (na_value , dtype )
413-
414378 @cache_readonly
415379 def is_na (self ) -> bool :
416380 blk = self .block
@@ -421,24 +385,14 @@ def is_na(self) -> bool:
421385 def get_reindexed_values (self , empty_dtype : DtypeObj , upcasted_na ) -> ArrayLike :
422386 values : ArrayLike
423387
424- if upcasted_na is None and self .block . dtype . kind != "V" :
388+ if upcasted_na is None and not self .is_na :
425389 # No upcasting is necessary
426390 fill_value = self .block .fill_value
427391 values = self .block .get_values ()
428392 else :
429393 fill_value = upcasted_na
430394
431- if self ._is_valid_na_for (empty_dtype ):
432- # note: always holds when self.block.dtype.kind == "V"
433- blk_dtype = self .block .dtype
434-
435- if blk_dtype == np .dtype ("object" ):
436- # we want to avoid filling with np.nan if we are
437- # using None; we already know that we are all
438- # nulls
439- values = self .block .values .ravel (order = "K" )
440- if len (values ) and values [0 ] is None :
441- fill_value = None
395+ if self .is_na :
442396
443397 if is_datetime64tz_dtype (empty_dtype ):
444398 i8values = np .full (self .shape , fill_value .value )
@@ -507,8 +461,7 @@ def _concatenate_join_units(
507461
508462 empty_dtype = _get_empty_dtype (join_units )
509463
510- has_none_blocks = any (unit .block .dtype .kind == "V" for unit in join_units )
511- upcasted_na = _dtype_to_na_value (empty_dtype , has_none_blocks )
464+ upcasted_na = _dtype_to_na_value (empty_dtype )
512465
513466 to_concat = [
514467 ju .get_reindexed_values (empty_dtype = empty_dtype , upcasted_na = upcasted_na )
@@ -548,7 +501,7 @@ def _concatenate_join_units(
548501 return concat_values
549502
550503
551- def _dtype_to_na_value (dtype : DtypeObj , has_none_blocks : bool ):
504+ def _dtype_to_na_value (dtype : DtypeObj ):
552505 """
553506 Find the NA value to go with this dtype.
554507 """
@@ -587,11 +540,9 @@ def _get_empty_dtype(join_units: Sequence[JoinUnit]) -> DtypeObj:
587540 empty_dtype = join_units [0 ].block .dtype
588541 return empty_dtype
589542
590- has_none_blocks = any (unit .block . dtype . kind == "V" for unit in join_units )
543+ has_none_blocks = any (unit .is_na for unit in join_units )
591544
592545 dtypes = [unit .dtype for unit in join_units if not unit .is_na ]
593- if not len (dtypes ):
594- dtypes = [unit .dtype for unit in join_units if unit .block .dtype .kind != "V" ]
595546
596547 dtype = find_common_type (dtypes )
597548 if has_none_blocks :
0 commit comments