Skip to content

Commit a85a386

Browse files
authored
REF: avoid _with_infer constructor (#50001)
1 parent 13b6b59 commit a85a386

File tree

11 files changed

+25
-21
lines changed

11 files changed

+25
-21
lines changed

Diff for: pandas/_testing/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def box_expected(expected, box_cls, transpose: bool = True):
274274
else:
275275
expected = pd.array(expected, copy=False)
276276
elif box_cls is Index:
277-
expected = Index._with_infer(expected)
277+
expected = Index(expected)
278278
elif box_cls is Series:
279279
expected = Series(expected)
280280
elif box_cls is DataFrame:

Diff for: pandas/core/algorithms.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ def value_counts(
894894

895895
# For backwards compatibility, we let Index do its normal type
896896
# inference, _except_ for if if infers from object to bool.
897-
idx = Index._with_infer(keys)
897+
idx = Index(keys)
898898
if idx.dtype == bool and keys.dtype == object:
899899
idx = idx.astype(object)
900900

Diff for: pandas/core/indexes/base.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -2678,6 +2678,7 @@ def fillna(self, value=None, downcast=None):
26782678
if downcast is None:
26792679
# no need to care metadata other than name
26802680
# because it can't have freq if it has NaTs
2681+
# _with_infer needed for test_fillna_categorical
26812682
return Index._with_infer(result, name=self.name)
26822683
raise NotImplementedError(
26832684
f"{type(self).__name__}.fillna does not support 'downcast' "
@@ -4230,10 +4231,10 @@ def _reindex_non_unique(
42304231
new_indexer = np.arange(len(self.take(indexer)), dtype=np.intp)
42314232
new_indexer[~check] = -1
42324233

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)
42354236
else:
4236-
new_index = Index._with_infer(new_labels, name=self.name)
4237+
new_index = type(self).from_tuples(new_labels, names=self.names)
42374238
return new_index, indexer, new_indexer
42384239

42394240
# --------------------------------------------------------------------
@@ -6477,7 +6478,7 @@ def insert(self, loc: int, item) -> Index:
64776478
if self._typ == "numericindex":
64786479
# Use self._constructor instead of Index to retain NumericIndex GH#43921
64796480
# 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)
64816482
else:
64826483
return Index._with_infer(new_values, name=self.name)
64836484

@@ -6850,7 +6851,7 @@ def ensure_index_from_sequences(sequences, names=None) -> Index:
68506851
if len(sequences) == 1:
68516852
if names is not None:
68526853
names = names[0]
6853-
return Index._with_infer(sequences[0], name=names)
6854+
return Index(sequences[0], name=names)
68546855
else:
68556856
return MultiIndex.from_arrays(sequences, names=names)
68566857

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

68946895
if isinstance(index_like, ABCSeries):
68956896
name = index_like.name
6896-
return Index._with_infer(index_like, name=name, copy=copy)
6897+
return Index(index_like, name=name, copy=copy)
68976898

68986899
if is_iterator(index_like):
68996900
index_like = list(index_like)
@@ -6909,9 +6910,9 @@ def ensure_index(index_like: Axes, copy: bool = False) -> Index:
69096910

69106911
return MultiIndex.from_arrays(index_like)
69116912
else:
6912-
return Index._with_infer(index_like, copy=copy, tupleize_cols=False)
6913+
return Index(index_like, copy=copy, tupleize_cols=False)
69136914
else:
6914-
return Index._with_infer(index_like, copy=copy)
6915+
return Index(index_like, copy=copy)
69156916

69166917

69176918
def ensure_has_len(seq):

Diff for: pandas/core/indexes/multi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2112,7 +2112,7 @@ def append(self, other):
21122112
# setting names to None automatically
21132113
return MultiIndex.from_tuples(new_tuples)
21142114
except (TypeError, IndexError):
2115-
return Index._with_infer(new_tuples)
2115+
return Index(new_tuples)
21162116

21172117
def argsort(self, *args, **kwargs) -> npt.NDArray[np.intp]:
21182118
if len(args) == 0 and len(kwargs) == 0:

Diff for: pandas/core/strings/accessor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def cons_row(x):
319319
out = out.get_level_values(0)
320320
return out
321321
else:
322-
return Index._with_infer(result, name=name)
322+
return Index(result, name=name)
323323
else:
324324
index = self._orig.index
325325
# This is a mess.

Diff for: pandas/core/util/hashing.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,7 @@ def _hash_ndarray(
344344
)
345345

346346
codes, categories = factorize(vals, sort=False)
347-
cat = Categorical(
348-
codes, Index._with_infer(categories), ordered=False, fastpath=True
349-
)
347+
cat = Categorical(codes, Index(categories), ordered=False, fastpath=True)
350348
return _hash_categorical(cat, encoding, hash_key)
351349

352350
try:

Diff for: pandas/tests/arithmetic/test_numeric.py

+6
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,9 @@ def test_numarr_with_dtype_add_nan(self, dtype, box_with_array):
11471147

11481148
ser = tm.box_expected(ser, box)
11491149
expected = tm.box_expected(expected, box)
1150+
if box is Index and dtype is object:
1151+
# TODO: avoid this; match behavior with Series
1152+
expected = expected.astype(np.float64)
11501153

11511154
result = np.nan + ser
11521155
tm.assert_equal(result, expected)
@@ -1162,6 +1165,9 @@ def test_numarr_with_dtype_add_int(self, dtype, box_with_array):
11621165

11631166
ser = tm.box_expected(ser, box)
11641167
expected = tm.box_expected(expected, box)
1168+
if box is Index and dtype is object:
1169+
# TODO: avoid this; match behavior with Series
1170+
expected = expected.astype(np.int64)
11651171

11661172
result = 1 + ser
11671173
tm.assert_equal(result, expected)

Diff for: pandas/tests/arrays/integer/test_dtypes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def test_astype_index(all_data, dropna):
8989
other = all_data
9090

9191
dtype = all_data.dtype
92-
idx = pd.Index._with_infer(np.array(other))
92+
idx = pd.Index(np.array(other))
9393
assert isinstance(idx, ABCIndex)
9494

9595
result = idx.astype(dtype)

Diff for: pandas/tests/extension/base/groupby.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_groupby_extension_agg(self, as_index, data_for_grouping):
3333
_, uniques = pd.factorize(data_for_grouping, sort=True)
3434

3535
if as_index:
36-
index = pd.Index._with_infer(uniques, name="B")
36+
index = pd.Index(uniques, name="B")
3737
expected = pd.Series([3.0, 1.0, 4.0], index=index, name="A")
3838
self.assert_series_equal(result, expected)
3939
else:
@@ -61,7 +61,7 @@ def test_groupby_extension_no_sort(self, data_for_grouping):
6161
result = df.groupby("B", sort=False).A.mean()
6262
_, index = pd.factorize(data_for_grouping, sort=False)
6363

64-
index = pd.Index._with_infer(index, name="B")
64+
index = pd.Index(index, name="B")
6565
expected = pd.Series([1.0, 3.0, 4.0], index=index, name="A")
6666
self.assert_series_equal(result, expected)
6767

Diff for: pandas/tests/extension/test_string.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ def test_groupby_extension_agg(self, as_index, data_for_grouping):
391391
_, uniques = pd.factorize(data_for_grouping, sort=True)
392392

393393
if as_index:
394-
index = pd.Index._with_infer(uniques, name="B")
394+
index = pd.Index(uniques, name="B")
395395
expected = pd.Series([3.0, 1.0, 4.0], index=index, name="A")
396396
self.assert_series_equal(result, expected)
397397
else:

Diff for: pandas/tests/io/test_stata.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
DataFrame,
2121
Series,
2222
)
23-
from pandas.core.indexes.api import ensure_index
2423
from pandas.tests.io.test_compression import _compression_to_extension
2524

2625
from pandas.io.parsers import read_csv
@@ -1144,7 +1143,7 @@ def _convert_categorical(from_frame: DataFrame) -> DataFrame:
11441143
if is_categorical_dtype(ser.dtype):
11451144
cat = ser._values.remove_unused_categories()
11461145
if cat.categories.dtype == object:
1147-
categories = ensure_index(cat.categories._values)
1146+
categories = pd.Index._with_infer(cat.categories._values)
11481147
cat = cat.set_categories(categories)
11491148
from_frame[col] = cat
11501149
return from_frame

0 commit comments

Comments
 (0)