Skip to content

Commit

Permalink
REF: avoid _with_infer constructor (#50001)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Dec 3, 2022
1 parent 13b6b59 commit a85a386
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def box_expected(expected, box_cls, transpose: bool = True):
else:
expected = pd.array(expected, copy=False)
elif box_cls is Index:
expected = Index._with_infer(expected)
expected = Index(expected)
elif box_cls is Series:
expected = Series(expected)
elif box_cls is DataFrame:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ def value_counts(

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

Expand Down
17 changes: 9 additions & 8 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2678,6 +2678,7 @@ def fillna(self, value=None, downcast=None):
if downcast is None:
# no need to care metadata other than name
# because it can't have freq if it has NaTs
# _with_infer needed for test_fillna_categorical
return Index._with_infer(result, name=self.name)
raise NotImplementedError(
f"{type(self).__name__}.fillna does not support 'downcast' "
Expand Down Expand Up @@ -4230,10 +4231,10 @@ def _reindex_non_unique(
new_indexer = np.arange(len(self.take(indexer)), dtype=np.intp)
new_indexer[~check] = -1

if isinstance(self, ABCMultiIndex):
new_index = type(self).from_tuples(new_labels, names=self.names)
if not isinstance(self, ABCMultiIndex):
new_index = Index(new_labels, name=self.name)
else:
new_index = Index._with_infer(new_labels, name=self.name)
new_index = type(self).from_tuples(new_labels, names=self.names)
return new_index, indexer, new_indexer

# --------------------------------------------------------------------
Expand Down Expand Up @@ -6477,7 +6478,7 @@ def insert(self, loc: int, item) -> Index:
if self._typ == "numericindex":
# Use self._constructor instead of Index to retain NumericIndex GH#43921
# TODO(2.0) can use Index instead of self._constructor
return self._constructor._with_infer(new_values, name=self.name)
return self._constructor(new_values, name=self.name)
else:
return Index._with_infer(new_values, name=self.name)

Expand Down Expand Up @@ -6850,7 +6851,7 @@ def ensure_index_from_sequences(sequences, names=None) -> Index:
if len(sequences) == 1:
if names is not None:
names = names[0]
return Index._with_infer(sequences[0], name=names)
return Index(sequences[0], name=names)
else:
return MultiIndex.from_arrays(sequences, names=names)

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

if isinstance(index_like, ABCSeries):
name = index_like.name
return Index._with_infer(index_like, name=name, copy=copy)
return Index(index_like, name=name, copy=copy)

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

return MultiIndex.from_arrays(index_like)
else:
return Index._with_infer(index_like, copy=copy, tupleize_cols=False)
return Index(index_like, copy=copy, tupleize_cols=False)
else:
return Index._with_infer(index_like, copy=copy)
return Index(index_like, copy=copy)


def ensure_has_len(seq):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2112,7 +2112,7 @@ def append(self, other):
# setting names to None automatically
return MultiIndex.from_tuples(new_tuples)
except (TypeError, IndexError):
return Index._with_infer(new_tuples)
return Index(new_tuples)

def argsort(self, *args, **kwargs) -> npt.NDArray[np.intp]:
if len(args) == 0 and len(kwargs) == 0:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def cons_row(x):
out = out.get_level_values(0)
return out
else:
return Index._with_infer(result, name=name)
return Index(result, name=name)
else:
index = self._orig.index
# This is a mess.
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/util/hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,7 @@ def _hash_ndarray(
)

codes, categories = factorize(vals, sort=False)
cat = Categorical(
codes, Index._with_infer(categories), ordered=False, fastpath=True
)
cat = Categorical(codes, Index(categories), ordered=False, fastpath=True)
return _hash_categorical(cat, encoding, hash_key)

try:
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,9 @@ def test_numarr_with_dtype_add_nan(self, dtype, box_with_array):

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

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

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

result = 1 + ser
tm.assert_equal(result, expected)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/integer/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_astype_index(all_data, dropna):
other = all_data

dtype = all_data.dtype
idx = pd.Index._with_infer(np.array(other))
idx = pd.Index(np.array(other))
assert isinstance(idx, ABCIndex)

result = idx.astype(dtype)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/extension/base/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_groupby_extension_agg(self, as_index, data_for_grouping):
_, uniques = pd.factorize(data_for_grouping, sort=True)

if as_index:
index = pd.Index._with_infer(uniques, name="B")
index = pd.Index(uniques, name="B")
expected = pd.Series([3.0, 1.0, 4.0], index=index, name="A")
self.assert_series_equal(result, expected)
else:
Expand Down Expand Up @@ -61,7 +61,7 @@ def test_groupby_extension_no_sort(self, data_for_grouping):
result = df.groupby("B", sort=False).A.mean()
_, index = pd.factorize(data_for_grouping, sort=False)

index = pd.Index._with_infer(index, name="B")
index = pd.Index(index, name="B")
expected = pd.Series([1.0, 3.0, 4.0], index=index, name="A")
self.assert_series_equal(result, expected)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def test_groupby_extension_agg(self, as_index, data_for_grouping):
_, uniques = pd.factorize(data_for_grouping, sort=True)

if as_index:
index = pd.Index._with_infer(uniques, name="B")
index = pd.Index(uniques, name="B")
expected = pd.Series([3.0, 1.0, 4.0], index=index, name="A")
self.assert_series_equal(result, expected)
else:
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/io/test_stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
DataFrame,
Series,
)
from pandas.core.indexes.api import ensure_index
from pandas.tests.io.test_compression import _compression_to_extension

from pandas.io.parsers import read_csv
Expand Down Expand Up @@ -1144,7 +1143,7 @@ def _convert_categorical(from_frame: DataFrame) -> DataFrame:
if is_categorical_dtype(ser.dtype):
cat = ser._values.remove_unused_categories()
if cat.categories.dtype == object:
categories = ensure_index(cat.categories._values)
categories = pd.Index._with_infer(cat.categories._values)
cat = cat.set_categories(categories)
from_frame[col] = cat
return from_frame
Expand Down

0 comments on commit a85a386

Please sign in to comment.