10
10
11
11
import numpy as np
12
12
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
18
14
from pandas ._typing import (
19
15
ArrayLike ,
20
16
DtypeObj ,
32
28
is_1d_only_ea_obj ,
33
29
is_datetime64tz_dtype ,
34
30
is_dtype_equal ,
35
- needs_i8_conversion ,
36
31
)
37
32
from pandas .core .dtypes .concat import (
38
33
cast_to_common_type ,
39
34
concat_compat ,
40
35
)
41
36
from pandas .core .dtypes .dtypes import ExtensionDtype
42
- from pandas .core .dtypes .missing import is_valid_na_for_dtype
43
37
44
38
import pandas .core .algorithms as algos
45
39
from pandas .core .arrays import (
@@ -381,36 +375,6 @@ def dtype(self):
381
375
return blk .dtype
382
376
return ensure_dtype_can_hold_na (blk .dtype )
383
377
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
-
414
378
@cache_readonly
415
379
def is_na (self ) -> bool :
416
380
blk = self .block
@@ -421,24 +385,14 @@ def is_na(self) -> bool:
421
385
def get_reindexed_values (self , empty_dtype : DtypeObj , upcasted_na ) -> ArrayLike :
422
386
values : ArrayLike
423
387
424
- if upcasted_na is None and self .block . dtype . kind != "V" :
388
+ if upcasted_na is None and not self .is_na :
425
389
# No upcasting is necessary
426
390
fill_value = self .block .fill_value
427
391
values = self .block .get_values ()
428
392
else :
429
393
fill_value = upcasted_na
430
394
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 :
442
396
443
397
if is_datetime64tz_dtype (empty_dtype ):
444
398
i8values = np .full (self .shape , fill_value .value )
@@ -507,8 +461,7 @@ def _concatenate_join_units(
507
461
508
462
empty_dtype = _get_empty_dtype (join_units )
509
463
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 )
512
465
513
466
to_concat = [
514
467
ju .get_reindexed_values (empty_dtype = empty_dtype , upcasted_na = upcasted_na )
@@ -548,7 +501,7 @@ def _concatenate_join_units(
548
501
return concat_values
549
502
550
503
551
- def _dtype_to_na_value (dtype : DtypeObj , has_none_blocks : bool ):
504
+ def _dtype_to_na_value (dtype : DtypeObj ):
552
505
"""
553
506
Find the NA value to go with this dtype.
554
507
"""
@@ -587,11 +540,9 @@ def _get_empty_dtype(join_units: Sequence[JoinUnit]) -> DtypeObj:
587
540
empty_dtype = join_units [0 ].block .dtype
588
541
return empty_dtype
589
542
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 )
591
544
592
545
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" ]
595
546
596
547
dtype = find_common_type (dtypes )
597
548
if has_none_blocks :
0 commit comments