3
3
"""
4
4
5
5
from collections import abc
6
- from typing import Iterable , List , Mapping , Union , overload
6
+ from typing import TYPE_CHECKING , Iterable , List , Mapping , Union , overload
7
7
8
8
import numpy as np
9
9
12
12
from pandas .core .dtypes .concat import concat_compat
13
13
from pandas .core .dtypes .generic import ABCDataFrame , ABCSeries
14
14
15
- from pandas import DataFrame , Index , MultiIndex , Series
16
15
from pandas .core .arrays .categorical import (
17
16
factorize_from_iterable ,
18
17
factorize_from_iterables ,
19
18
)
20
19
import pandas .core .common as com
21
- from pandas .core .generic import NDFrame
22
20
from pandas .core .indexes .api import (
21
+ Index ,
22
+ MultiIndex ,
23
23
all_indexes_same ,
24
24
ensure_index ,
25
25
get_consensus_names ,
28
28
import pandas .core .indexes .base as ibase
29
29
from pandas .core .internals import concatenate_block_managers
30
30
31
+ if TYPE_CHECKING :
32
+ from pandas import DataFrame
33
+
31
34
# ---------------------------------------------------------------------
32
35
# Concatenate DataFrame objects
33
36
@@ -291,7 +294,7 @@ class _Concatenator:
291
294
292
295
def __init__ (
293
296
self ,
294
- objs ,
297
+ objs : Union [ Iterable [ FrameOrSeries ], Mapping [ Label , FrameOrSeries ]] ,
295
298
axis = 0 ,
296
299
join : str = "outer" ,
297
300
keys = None ,
@@ -302,7 +305,7 @@ def __init__(
302
305
copy : bool = True ,
303
306
sort = False ,
304
307
):
305
- if isinstance (objs , (NDFrame , str )):
308
+ if isinstance (objs , (ABCSeries , ABCDataFrame , str )):
306
309
raise TypeError (
307
310
"first argument must be an iterable of pandas "
308
311
f'objects, you passed an object of type "{ type (objs ).__name__ } "'
@@ -348,7 +351,7 @@ def __init__(
348
351
# consolidate data & figure out what our result ndim is going to be
349
352
ndims = set ()
350
353
for obj in objs :
351
- if not isinstance (obj , (Series , DataFrame )):
354
+ if not isinstance (obj , (ABCSeries , ABCDataFrame )):
352
355
msg = (
353
356
f"cannot concatenate object of type '{ type (obj )} '; "
354
357
"only Series and DataFrame objs are valid"
@@ -374,7 +377,7 @@ def __init__(
374
377
# filter out the empties if we have not multi-index possibilities
375
378
# note to keep empty Series as it affect to result columns / name
376
379
non_empties = [
377
- obj for obj in objs if sum (obj .shape ) > 0 or isinstance (obj , Series )
380
+ obj for obj in objs if sum (obj .shape ) > 0 or isinstance (obj , ABCSeries )
378
381
]
379
382
380
383
if len (non_empties ) and (
@@ -388,15 +391,15 @@ def __init__(
388
391
self .objs = objs
389
392
390
393
# Standardize axis parameter to int
391
- if isinstance (sample , Series ):
392
- axis = DataFrame ._get_axis_number (axis )
394
+ if isinstance (sample , ABCSeries ):
395
+ axis = sample . _constructor_expanddim ._get_axis_number (axis )
393
396
else :
394
397
axis = sample ._get_axis_number (axis )
395
398
396
399
# Need to flip BlockManager axis in the DataFrame special case
397
400
self ._is_frame = isinstance (sample , ABCDataFrame )
398
401
if self ._is_frame :
399
- axis = DataFrame ._get_block_manager_axis (axis )
402
+ axis = sample ._get_block_manager_axis (axis )
400
403
401
404
self ._is_series = isinstance (sample , ABCSeries )
402
405
if not 0 <= axis <= sample .ndim :
@@ -543,7 +546,7 @@ def _get_concat_axis(self) -> Index:
543
546
num = 0
544
547
has_names = False
545
548
for i , x in enumerate (self .objs ):
546
- if not isinstance (x , Series ):
549
+ if not isinstance (x , ABCSeries ):
547
550
raise TypeError (
548
551
f"Cannot concatenate type 'Series' with "
549
552
f"object of type '{ type (x ).__name__ } '"
0 commit comments