Skip to content

Commit c7b502b

Browse files
authored
ENH: Index.infer_objects (#50034)
* ENH: Index.infer_objects * GH ref * mypy fixup * raise NotImplementedError for MultiIndex
1 parent 362f348 commit c7b502b

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Other enhancements
8383
- :func:`timedelta_range` now supports a ``unit`` keyword ("s", "ms", "us", or "ns") to specify the desired resolution of the output index (:issue:`49824`)
8484
- :meth:`DataFrame.to_json` now supports a ``mode`` keyword with supported inputs 'w' and 'a'. Defaulting to 'w', 'a' can be used when lines=True and orient='records' to append record oriented json lines to an existing json file. (:issue:`35849`)
8585
- Added ``name`` parameter to :meth:`IntervalIndex.from_breaks`, :meth:`IntervalIndex.from_arrays` and :meth:`IntervalIndex.from_tuples` (:issue:`48911`)
86+
- Added :meth:`Index.infer_objects` analogous to :meth:`Series.infer_objects` (:issue:`50034`)
8687
- :meth:`DataFrame.plot.hist` now recognizes ``xlabel`` and ``ylabel`` arguments (:issue:`49793`)
8788
-
8889

pandas/core/indexes/base.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6521,6 +6521,36 @@ def drop(
65216521
indexer = indexer[~mask]
65226522
return self.delete(indexer)
65236523

6524+
def infer_objects(self, copy: bool = True) -> Index:
6525+
"""
6526+
If we have an object dtype, try to infer a non-object dtype.
6527+
6528+
Parameters
6529+
----------
6530+
copy : bool, default True
6531+
Whether to make a copy in cases where no inference occurs.
6532+
"""
6533+
if self._is_multi:
6534+
raise NotImplementedError(
6535+
"infer_objects is not implemented for MultiIndex. "
6536+
"Use index.to_frame().infer_objects() instead."
6537+
)
6538+
if self.dtype != object:
6539+
return self.copy() if copy else self
6540+
6541+
values = self._values
6542+
values = cast("npt.NDArray[np.object_]", values)
6543+
res_values = lib.maybe_convert_objects(
6544+
values,
6545+
convert_datetime=True,
6546+
convert_timedelta=True,
6547+
convert_period=True,
6548+
convert_interval=True,
6549+
)
6550+
if copy and res_values is values:
6551+
return self.copy()
6552+
return Index(res_values, name=self.name)
6553+
65246554
# --------------------------------------------------------------------
65256555
# Generated Arithmetic, Comparison, and Unary Methods
65266556

pandas/tests/indexes/multi/test_analytics.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
from pandas.core.api import UInt64Index
1313

1414

15+
def test_infer_objects(idx):
16+
with pytest.raises(NotImplementedError, match="to_frame"):
17+
idx.infer_objects()
18+
19+
1520
def test_shift(idx):
1621

1722
# GH8083 test the base class for shift
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import numpy as np
22

3-
from pandas import Series
43
import pandas._testing as tm
54

65

76
class TestInferObjects:
8-
def test_infer_objects_series(self):
7+
def test_infer_objects_series(self, index_or_series):
98
# GH#11221
10-
actual = Series(np.array([1, 2, 3], dtype="O")).infer_objects()
11-
expected = Series([1, 2, 3])
12-
tm.assert_series_equal(actual, expected)
9+
actual = index_or_series(np.array([1, 2, 3], dtype="O")).infer_objects()
10+
expected = index_or_series([1, 2, 3])
11+
tm.assert_equal(actual, expected)
1312

14-
actual = Series(np.array([1, 2, 3, None], dtype="O")).infer_objects()
15-
expected = Series([1.0, 2.0, 3.0, np.nan])
16-
tm.assert_series_equal(actual, expected)
13+
actual = index_or_series(np.array([1, 2, 3, None], dtype="O")).infer_objects()
14+
expected = index_or_series([1.0, 2.0, 3.0, np.nan])
15+
tm.assert_equal(actual, expected)
1716

1817
# only soft conversions, unconvertable pass thru unchanged
19-
actual = Series(np.array([1, 2, 3, None, "a"], dtype="O")).infer_objects()
20-
expected = Series([1, 2, 3, None, "a"])
18+
19+
obj = index_or_series(np.array([1, 2, 3, None, "a"], dtype="O"))
20+
actual = obj.infer_objects()
21+
expected = index_or_series([1, 2, 3, None, "a"], dtype=object)
2122

2223
assert actual.dtype == "object"
23-
tm.assert_series_equal(actual, expected)
24+
tm.assert_equal(actual, expected)

0 commit comments

Comments
 (0)