69
69
NDFrameT ,
70
70
RandomState ,
71
71
Renamer ,
72
+ Scalar ,
72
73
SortKind ,
73
74
StorageOptions ,
74
75
Suffixes ,
@@ -5104,11 +5105,21 @@ def sort_index(
5104
5105
5105
5106
@doc (
5106
5107
klass = _shared_doc_kwargs ["klass" ],
5107
- axes = _shared_doc_kwargs ["axes" ],
5108
- optional_labels = "" ,
5109
- optional_axis = "" ,
5108
+ optional_reindex = "" ,
5110
5109
)
5111
- def reindex (self : NDFrameT , * args , ** kwargs ) -> NDFrameT :
5110
+ def reindex (
5111
+ self : NDFrameT ,
5112
+ labels = None ,
5113
+ index = None ,
5114
+ columns = None ,
5115
+ axis : Axis | None = None ,
5116
+ method : str | None = None ,
5117
+ copy : bool_t | None = None ,
5118
+ level : Level | None = None ,
5119
+ fill_value : Scalar | None = np .nan ,
5120
+ limit : int | None = None ,
5121
+ tolerance = None ,
5122
+ ) -> NDFrameT :
5112
5123
"""
5113
5124
Conform {klass} to new index with optional filling logic.
5114
5125
@@ -5118,11 +5129,7 @@ def reindex(self: NDFrameT, *args, **kwargs) -> NDFrameT:
5118
5129
5119
5130
Parameters
5120
5131
----------
5121
- {optional_labels}
5122
- {axes} : array-like, optional
5123
- New labels / index to conform to, should be specified using
5124
- keywords. Preferably an Index object to avoid duplicating data.
5125
- {optional_axis}
5132
+ {optional_reindex}
5126
5133
method : {{None, 'backfill'/'bfill', 'pad'/'ffill', 'nearest'}}
5127
5134
Method to use for filling holes in reindexed DataFrame.
5128
5135
Please note: this is only applicable to DataFrames/Series with a
@@ -5311,31 +5318,34 @@ def reindex(self: NDFrameT, *args, **kwargs) -> NDFrameT:
5311
5318
# TODO: Decide if we care about having different examples for different
5312
5319
# kinds
5313
5320
5314
- # construct the args
5315
- axes , kwargs = self ._construct_axes_from_arguments (args , kwargs )
5316
- method = clean_reindex_fill_method (kwargs .pop ("method" , None ))
5317
- level = kwargs .pop ("level" , None )
5318
- copy = kwargs .pop ("copy" , None )
5319
- limit = kwargs .pop ("limit" , None )
5320
- tolerance = kwargs .pop ("tolerance" , None )
5321
- fill_value = kwargs .pop ("fill_value" , None )
5322
-
5323
- # Series.reindex doesn't use / need the axis kwarg
5324
- # We pop and ignore it here, to make writing Series/Frame generic code
5325
- # easier
5326
- kwargs .pop ("axis" , None )
5327
-
5328
- if kwargs :
5329
- raise TypeError (
5330
- "reindex() got an unexpected keyword "
5331
- f'argument "{ list (kwargs .keys ())[0 ]} "'
5332
- )
5321
+ if index is not None and columns is not None and labels is not None :
5322
+ raise TypeError ("Cannot specify all of 'labels', 'index', 'columns'." )
5323
+ elif index is not None or columns is not None :
5324
+ if axis is not None :
5325
+ raise TypeError (
5326
+ "Cannot specify both 'axis' and any of 'index' or 'columns'"
5327
+ )
5328
+ if labels is not None :
5329
+ if index is not None :
5330
+ columns = labels
5331
+ else :
5332
+ index = labels
5333
+ else :
5334
+ if axis and self ._get_axis_number (axis ) == 1 :
5335
+ columns = labels
5336
+ else :
5337
+ index = labels
5338
+ axes : dict [Literal ["index" , "columns" ], Any ] = {
5339
+ "index" : index ,
5340
+ "columns" : columns ,
5341
+ }
5342
+ method = clean_reindex_fill_method (method )
5333
5343
5334
5344
# if all axes that are requested to reindex are equal, then only copy
5335
5345
# if indicated must have index names equal here as well as values
5336
5346
if all (
5337
- self ._get_axis (axis ).identical (ax )
5338
- for axis , ax in axes .items ()
5347
+ self ._get_axis (axis_name ).identical (ax )
5348
+ for axis_name , ax in axes .items ()
5339
5349
if ax is not None
5340
5350
):
5341
5351
return self .copy (deep = copy )
@@ -5519,7 +5529,7 @@ def filter(
5519
5529
name = self ._get_axis_name (axis )
5520
5530
# error: Keywords must be strings
5521
5531
return self .reindex ( # type: ignore[misc]
5522
- ** {name : [r for r in items if r in labels ]}
5532
+ ** {name : [r for r in items if r in labels ]} # type: ignore[arg-type]
5523
5533
)
5524
5534
elif like :
5525
5535
0 commit comments