Skip to content

Commit

Permalink
DEPR: Index.asi8 (#37877)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed Nov 19, 2020
1 parent 3d259e6 commit d5b2094
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 11 deletions.
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,8 @@ Deprecations
- :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`)
- :meth:`Categorical.is_dtype_equal` and :meth:`CategoricalIndex.is_dtype_equal` are deprecated, will be removed in a future version (:issue:`37545`)
- :meth:`Series.slice_shift` and :meth:`DataFrame.slice_shift` are deprecated, use :meth:`Series.shift` or :meth:`DataFrame.shift` instead (:issue:`37601`)
- 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`)
- 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`)
- Deprecated :meth:`Index.asi8` for :class:`Index` subclasses other than :class:`DatetimeIndex`, :class:`TimedeltaIndex`, and :class:`PeriodIndex` (:issue:`37877`)
- The ``inplace`` parameter of :meth:`Categorical.remove_unused_categories` is deprecated and will be removed in a future version (:issue:`37643`)

.. ---------------------------------------------------------------------------
Expand Down
16 changes: 11 additions & 5 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ def asi8(self):
ndarray
An ndarray with int64 dtype.
"""
warnings.warn(
"Index.asi8 is deprecated and will be removed in a future version",
FutureWarning,
stacklevel=2,
)
return None

@classmethod
Expand Down Expand Up @@ -4738,12 +4743,13 @@ def argsort(self, *args, **kwargs) -> np.ndarray:
>>> idx[order]
Index(['a', 'b', 'c', 'd'], dtype='object')
"""
result = self.asi8

if result is None:
result = np.array(self)
if needs_i8_conversion(self.dtype):
# TODO: these do not match the underlying EA argsort methods GH#37863
return self.asi8.argsort(*args, **kwargs)

return result.argsort(*args, **kwargs)
# This works for either ndarray or EA, is overriden
# by RangeIndex, MultIIndex
return self._data.argsort(*args, **kwargs)

@final
def get_value(self, series: "Series", key):
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import operator
from typing import Any
import warnings

import numpy as np

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


Expand Down
6 changes: 4 additions & 2 deletions pandas/core/tools/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
is_number,
is_numeric_dtype,
is_scalar,
needs_i8_conversion,
)
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries

Expand Down Expand Up @@ -123,8 +124,9 @@ def to_numeric(arg, errors="raise", downcast=None):
values = arg.values
elif isinstance(arg, ABCIndexClass):
is_index = True
values = arg.asi8
if values is None:
if needs_i8_conversion(arg.dtype):
values = arg.asi8
else:
values = arg.values
elif isinstance(arg, (list, tuple)):
values = np.array(arg, dtype="O")
Expand Down
11 changes: 9 additions & 2 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,13 @@ def _get_roll_func(self, func_name: str) -> Callable[..., Any]:
)
return window_func

@property
def _index_array(self):
# TODO: why do we get here with e.g. MultiIndex?
if needs_i8_conversion(self._on.dtype):
return self._on.asi8
return None

def _get_window_indexer(self) -> BaseIndexer:
"""
Return an indexer class that will compute the window start and end bounds
Expand All @@ -345,7 +352,7 @@ def _get_window_indexer(self) -> BaseIndexer:
return self.window
if self.is_freq_type:
return VariableWindowIndexer(
index_array=self._on.asi8, window_size=self.window
index_array=self._index_array, window_size=self.window
)
return FixedWindowIndexer(window_size=self.window)

Expand Down Expand Up @@ -2140,7 +2147,7 @@ def _get_window_indexer(self) -> GroupbyIndexer:
"""
rolling_indexer: Type[BaseIndexer]
indexer_kwargs: Optional[Dict[str, Any]] = None
index_array = self._on.asi8
index_array = self._index_array
window = self.window
if isinstance(self.window, BaseIndexer):
rolling_indexer = type(self.window)
Expand Down
23 changes: 22 additions & 1 deletion pandas/tests/indexes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@
from pandas.core.dtypes.common import is_period_dtype, needs_i8_conversion

import pandas as pd
from pandas import CategoricalIndex, MultiIndex, RangeIndex
from pandas import (
CategoricalIndex,
DatetimeIndex,
Int64Index,
MultiIndex,
PeriodIndex,
RangeIndex,
TimedeltaIndex,
UInt64Index,
)
import pandas._testing as tm


Expand Down Expand Up @@ -348,6 +357,18 @@ def test_ravel_deprecation(self, index):
with tm.assert_produces_warning(FutureWarning):
index.ravel()

def test_asi8_deprecation(self, index):
# GH#37877
if isinstance(
index, (Int64Index, UInt64Index, DatetimeIndex, TimedeltaIndex, PeriodIndex)
):
warn = None
else:
warn = FutureWarning

with tm.assert_produces_warning(warn):
index.asi8


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

0 comments on commit d5b2094

Please sign in to comment.