REF: avoid _with_infer constructor (#50001) · pandas-dev/pandas@a85a386 (original) (raw)

`@@ -2678,6 +2678,7 @@ def fillna(self, value=None, downcast=None):

`

2678

2678

`if downcast is None:

`

2679

2679

`# no need to care metadata other than name

`

2680

2680

`# because it can't have freq if it has NaTs

`

``

2681

`+

_with_infer needed for test_fillna_categorical

`

2681

2682

`return Index._with_infer(result, name=self.name)

`

2682

2683

`raise NotImplementedError(

`

2683

2684

`f"{type(self).name}.fillna does not support 'downcast' "

`

`@@ -4230,10 +4231,10 @@ def _reindex_non_unique(

`

4230

4231

`new_indexer = np.arange(len(self.take(indexer)), dtype=np.intp)

`

4231

4232

`new_indexer[~check] = -1

`

4232

4233

``

4233

``

`-

if isinstance(self, ABCMultiIndex):

`

4234

``

`-

new_index = type(self).from_tuples(new_labels, names=self.names)

`

``

4234

`+

if not isinstance(self, ABCMultiIndex):

`

``

4235

`+

new_index = Index(new_labels, name=self.name)

`

4235

4236

`else:

`

4236

``

`-

new_index = Index._with_infer(new_labels, name=self.name)

`

``

4237

`+

new_index = type(self).from_tuples(new_labels, names=self.names)

`

4237

4238

`return new_index, indexer, new_indexer

`

4238

4239

``

4239

4240

`# --------------------------------------------------------------------

`

`@@ -6477,7 +6478,7 @@ def insert(self, loc: int, item) -> Index:

`

6477

6478

`if self._typ == "numericindex":

`

6478

6479

`# Use self._constructor instead of Index to retain NumericIndex GH#43921

`

6479

6480

`# TODO(2.0) can use Index instead of self._constructor

`

6480

``

`-

return self._constructor._with_infer(new_values, name=self.name)

`

``

6481

`+

return self._constructor(new_values, name=self.name)

`

6481

6482

`else:

`

6482

6483

`return Index._with_infer(new_values, name=self.name)

`

6483

6484

``

`@@ -6850,7 +6851,7 @@ def ensure_index_from_sequences(sequences, names=None) -> Index:

`

6850

6851

`if len(sequences) == 1:

`

6851

6852

`if names is not None:

`

6852

6853

`names = names[0]

`

6853

``

`-

return Index._with_infer(sequences[0], name=names)

`

``

6854

`+

return Index(sequences[0], name=names)

`

6854

6855

`else:

`

6855

6856

`return MultiIndex.from_arrays(sequences, names=names)

`

6856

6857

``

`@@ -6893,7 +6894,7 @@ def ensure_index(index_like: Axes, copy: bool = False) -> Index:

`

6893

6894

``

6894

6895

`if isinstance(index_like, ABCSeries):

`

6895

6896

`name = index_like.name

`

6896

``

`-

return Index._with_infer(index_like, name=name, copy=copy)

`

``

6897

`+

return Index(index_like, name=name, copy=copy)

`

6897

6898

``

6898

6899

`if is_iterator(index_like):

`

6899

6900

`index_like = list(index_like)

`

`@@ -6909,9 +6910,9 @@ def ensure_index(index_like: Axes, copy: bool = False) -> Index:

`

6909

6910

``

6910

6911

`return MultiIndex.from_arrays(index_like)

`

6911

6912

`else:

`

6912

``

`-

return Index._with_infer(index_like, copy=copy, tupleize_cols=False)

`

``

6913

`+

return Index(index_like, copy=copy, tupleize_cols=False)

`

6913

6914

`else:

`

6914

``

`-

return Index._with_infer(index_like, copy=copy)

`

``

6915

`+

return Index(index_like, copy=copy)

`

6915

6916

``

6916

6917

``

6917

6918

`def ensure_has_len(seq):

`