Skip to content

Commit d5b2094

Browse files
authored
DEPR: Index.asi8 (#37877)
1 parent 3d259e6 commit d5b2094

File tree

6 files changed

+54
-11
lines changed

6 files changed

+54
-11
lines changed

doc/source/whatsnew/v1.2.0.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,8 @@ Deprecations
474474
- :class:`Index` methods ``&``, ``|``, and ``^`` behaving as the set operations :meth:`Index.intersection`, :meth:`Index.union`, and :meth:`Index.symmetric_difference`, respectively, are deprecated and in the future will behave as pointwise boolean operations matching :class:`Series` behavior. Use the named set methods instead (:issue:`36758`)
475475
- :meth:`Categorical.is_dtype_equal` and :meth:`CategoricalIndex.is_dtype_equal` are deprecated, will be removed in a future version (:issue:`37545`)
476476
- :meth:`Series.slice_shift` and :meth:`DataFrame.slice_shift` are deprecated, use :meth:`Series.shift` or :meth:`DataFrame.shift` instead (:issue:`37601`)
477-
- Partial slicing on unordered :class:`DatetimeIndexes` with keys, which are not in Index is deprecated and will be removed in a future version (:issue:`18531`)
477+
- Partial slicing on unordered :class:`DatetimeIndex` with keys, which are not in Index is deprecated and will be removed in a future version (:issue:`18531`)
478+
- Deprecated :meth:`Index.asi8` for :class:`Index` subclasses other than :class:`DatetimeIndex`, :class:`TimedeltaIndex`, and :class:`PeriodIndex` (:issue:`37877`)
478479
- The ``inplace`` parameter of :meth:`Categorical.remove_unused_categories` is deprecated and will be removed in a future version (:issue:`37643`)
479480

480481
.. ---------------------------------------------------------------------------

pandas/core/indexes/base.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,11 @@ def asi8(self):
415415
ndarray
416416
An ndarray with int64 dtype.
417417
"""
418+
warnings.warn(
419+
"Index.asi8 is deprecated and will be removed in a future version",
420+
FutureWarning,
421+
stacklevel=2,
422+
)
418423
return None
419424

420425
@classmethod
@@ -4738,12 +4743,13 @@ def argsort(self, *args, **kwargs) -> np.ndarray:
47384743
>>> idx[order]
47394744
Index(['a', 'b', 'c', 'd'], dtype='object')
47404745
"""
4741-
result = self.asi8
4742-
4743-
if result is None:
4744-
result = np.array(self)
4746+
if needs_i8_conversion(self.dtype):
4747+
# TODO: these do not match the underlying EA argsort methods GH#37863
4748+
return self.asi8.argsort(*args, **kwargs)
47454749

4746-
return result.argsort(*args, **kwargs)
4750+
# This works for either ndarray or EA, is overriden
4751+
# by RangeIndex, MultIIndex
4752+
return self._data.argsort(*args, **kwargs)
47474753

47484754
@final
47494755
def get_value(self, series: "Series", key):

pandas/core/indexes/numeric.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import operator
22
from typing import Any
3+
import warnings
34

45
import numpy as np
56

@@ -266,6 +267,11 @@ def inferred_type(self) -> str:
266267
@property
267268
def asi8(self) -> np.ndarray:
268269
# do not cache or you'll create a memory leak
270+
warnings.warn(
271+
"Index.asi8 is deprecated and will be removed in a future version",
272+
FutureWarning,
273+
stacklevel=2,
274+
)
269275
return self._values.view(self._default_dtype)
270276

271277

pandas/core/tools/numeric.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
is_number,
1111
is_numeric_dtype,
1212
is_scalar,
13+
needs_i8_conversion,
1314
)
1415
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
1516

@@ -123,8 +124,9 @@ def to_numeric(arg, errors="raise", downcast=None):
123124
values = arg.values
124125
elif isinstance(arg, ABCIndexClass):
125126
is_index = True
126-
values = arg.asi8
127-
if values is None:
127+
if needs_i8_conversion(arg.dtype):
128+
values = arg.asi8
129+
else:
128130
values = arg.values
129131
elif isinstance(arg, (list, tuple)):
130132
values = np.array(arg, dtype="O")

pandas/core/window/rolling.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,13 @@ def _get_roll_func(self, func_name: str) -> Callable[..., Any]:
337337
)
338338
return window_func
339339

340+
@property
341+
def _index_array(self):
342+
# TODO: why do we get here with e.g. MultiIndex?
343+
if needs_i8_conversion(self._on.dtype):
344+
return self._on.asi8
345+
return None
346+
340347
def _get_window_indexer(self) -> BaseIndexer:
341348
"""
342349
Return an indexer class that will compute the window start and end bounds
@@ -345,7 +352,7 @@ def _get_window_indexer(self) -> BaseIndexer:
345352
return self.window
346353
if self.is_freq_type:
347354
return VariableWindowIndexer(
348-
index_array=self._on.asi8, window_size=self.window
355+
index_array=self._index_array, window_size=self.window
349356
)
350357
return FixedWindowIndexer(window_size=self.window)
351358

@@ -2140,7 +2147,7 @@ def _get_window_indexer(self) -> GroupbyIndexer:
21402147
"""
21412148
rolling_indexer: Type[BaseIndexer]
21422149
indexer_kwargs: Optional[Dict[str, Any]] = None
2143-
index_array = self._on.asi8
2150+
index_array = self._index_array
21442151
window = self.window
21452152
if isinstance(self.window, BaseIndexer):
21462153
rolling_indexer = type(self.window)

pandas/tests/indexes/test_common.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@
1313
from pandas.core.dtypes.common import is_period_dtype, needs_i8_conversion
1414

1515
import pandas as pd
16-
from pandas import CategoricalIndex, MultiIndex, RangeIndex
16+
from pandas import (
17+
CategoricalIndex,
18+
DatetimeIndex,
19+
Int64Index,
20+
MultiIndex,
21+
PeriodIndex,
22+
RangeIndex,
23+
TimedeltaIndex,
24+
UInt64Index,
25+
)
1726
import pandas._testing as tm
1827

1928

@@ -348,6 +357,18 @@ def test_ravel_deprecation(self, index):
348357
with tm.assert_produces_warning(FutureWarning):
349358
index.ravel()
350359

360+
def test_asi8_deprecation(self, index):
361+
# GH#37877
362+
if isinstance(
363+
index, (Int64Index, UInt64Index, DatetimeIndex, TimedeltaIndex, PeriodIndex)
364+
):
365+
warn = None
366+
else:
367+
warn = FutureWarning
368+
369+
with tm.assert_produces_warning(warn):
370+
index.asi8
371+
351372

352373
@pytest.mark.parametrize("na_position", [None, "middle"])
353374
def test_sort_values_invalid_na_position(index_with_missing, na_position):

0 commit comments

Comments
 (0)