Skip to content

Commit 358bf13

Browse files
jorisvandenbosschetm9k1
authored andcommitted
DEPR: deprecate fastpath keyword in Index constructors (pandas-dev#23110)
1 parent d5629b1 commit 358bf13

File tree

7 files changed

+76
-26
lines changed

7 files changed

+76
-26
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@ Deprecations
734734
many ``Series``, ``Index`` or 1-dimensional ``np.ndarray``, or alternatively, only scalar values. (:issue:`21950`)
735735
- :meth:`FrozenNDArray.searchsorted` has deprecated the ``v`` parameter in favor of ``value`` (:issue:`14645`)
736736
- :func:`DatetimeIndex.shift` and :func:`PeriodIndex.shift` now accept ``periods`` argument instead of ``n`` for consistency with :func:`Index.shift` and :func:`Series.shift`. Using ``n`` throws a deprecation warning (:issue:`22458`, :issue:`22912`)
737+
- The ``fastpath`` keyword of the different Index constructors is deprecated (:issue:`23110`).
737738

738739
.. _whatsnew_0240.prior_deprecations:
739740

pandas/core/indexes/base.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,17 @@ def _outer_indexer(self, left, right):
262262
str = CachedAccessor("str", StringMethods)
263263

264264
def __new__(cls, data=None, dtype=None, copy=False, name=None,
265-
fastpath=False, tupleize_cols=True, **kwargs):
265+
fastpath=None, tupleize_cols=True, **kwargs):
266266

267267
if name is None and hasattr(data, 'name'):
268268
name = data.name
269269

270-
if fastpath:
271-
return cls._simple_new(data, name)
270+
if fastpath is not None:
271+
warnings.warn("The 'fastpath' keyword is deprecated, and will be "
272+
"removed in a future version.",
273+
FutureWarning, stacklevel=2)
274+
if fastpath:
275+
return cls._simple_new(data, name)
272276

273277
from .range import RangeIndex
274278

pandas/core/indexes/category.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import operator
2+
import warnings
23

34
import numpy as np
45
from pandas._libs import index as libindex
@@ -87,10 +88,14 @@ class CategoricalIndex(Index, accessor.PandasDelegate):
8788
_attributes = ['name']
8889

8990
def __new__(cls, data=None, categories=None, ordered=None, dtype=None,
90-
copy=False, name=None, fastpath=False):
91-
92-
if fastpath:
93-
return cls._simple_new(data, name=name, dtype=dtype)
91+
copy=False, name=None, fastpath=None):
92+
93+
if fastpath is not None:
94+
warnings.warn("The 'fastpath' keyword is deprecated, and will be "
95+
"removed in a future version.",
96+
FutureWarning, stacklevel=2)
97+
if fastpath:
98+
return cls._simple_new(data, name=name, dtype=dtype)
9499

95100
if name is None and hasattr(data, 'name'):
96101
name = data.name

pandas/core/indexes/numeric.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
import numpy as np
24
from pandas._libs import index as libindex
35
from pandas.core.dtypes.common import (
@@ -34,10 +36,14 @@ class NumericIndex(Index):
3436
_is_numeric_dtype = True
3537

3638
def __new__(cls, data=None, dtype=None, copy=False, name=None,
37-
fastpath=False):
38-
39-
if fastpath:
40-
return cls._simple_new(data, name=name)
39+
fastpath=None):
40+
41+
if fastpath is not None:
42+
warnings.warn("The 'fastpath' keyword is deprecated, and will be "
43+
"removed in a future version.",
44+
FutureWarning, stacklevel=2)
45+
if fastpath:
46+
return cls._simple_new(data, name=name)
4147

4248
# is_scalar, generators handled in coerce_to_ndarray
4349
data = cls._coerce_to_ndarray(data)

pandas/core/indexes/period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def __contains__(self, key):
268268

269269
@cache_readonly
270270
def _int64index(self):
271-
return Int64Index(self.asi8, name=self.name, fastpath=True)
271+
return Int64Index._simple_new(self.asi8, name=self.name)
272272

273273
@property
274274
def values(self):

pandas/core/indexes/range.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import operator
22
from datetime import timedelta
33
from sys import getsizeof
4+
import warnings
45

56
import numpy as np
67

@@ -62,10 +63,14 @@ class RangeIndex(Int64Index):
6263
_engine_type = libindex.Int64Engine
6364

6465
def __new__(cls, start=None, stop=None, step=None,
65-
dtype=None, copy=False, name=None, fastpath=False):
66+
dtype=None, copy=False, name=None, fastpath=None):
6667

67-
if fastpath:
68-
return cls._simple_new(start, stop, step, name=name)
68+
if fastpath is not None:
69+
warnings.warn("The 'fastpath' keyword is deprecated, and will be "
70+
"removed in a future version.",
71+
FutureWarning, stacklevel=2)
72+
if fastpath:
73+
return cls._simple_new(start, stop, step, name=name)
6974

7075
cls._validate_dtype(dtype)
7176

@@ -168,7 +173,7 @@ def _data(self):
168173

169174
@cache_readonly
170175
def _int64index(self):
171-
return Int64Index(self._data, name=self.name, fastpath=True)
176+
return Int64Index._simple_new(self._data, name=self.name)
172177

173178
def _get_data_as_items(self):
174179
""" return a list of tuples of start, stop, step """
@@ -256,8 +261,8 @@ def tolist(self):
256261
@Appender(_index_shared_docs['_shallow_copy'])
257262
def _shallow_copy(self, values=None, **kwargs):
258263
if values is None:
259-
return RangeIndex(name=self.name, fastpath=True,
260-
**dict(self._get_data_as_items()))
264+
return RangeIndex._simple_new(
265+
name=self.name, **dict(self._get_data_as_items()))
261266
else:
262267
kwargs.setdefault('name', self.name)
263268
return self._int64index._shallow_copy(values, **kwargs)
@@ -267,8 +272,8 @@ def copy(self, name=None, deep=False, dtype=None, **kwargs):
267272
self._validate_dtype(dtype)
268273
if name is None:
269274
name = self.name
270-
return RangeIndex(name=name, fastpath=True,
271-
**dict(self._get_data_as_items()))
275+
return RangeIndex._simple_new(
276+
name=name, **dict(self._get_data_as_items()))
272277

273278
def _minmax(self, meth):
274279
no_steps = len(self) - 1
@@ -368,7 +373,7 @@ def intersection(self, other):
368373
tmp_start = first._start + (second._start - first._start) * \
369374
first._step // gcd * s
370375
new_step = first._step * second._step // gcd
371-
new_index = RangeIndex(tmp_start, int_high, new_step, fastpath=True)
376+
new_index = RangeIndex._simple_new(tmp_start, int_high, new_step)
372377

373378
# adjust index to limiting interval
374379
new_index._start = new_index._min_fitting_element(int_low)
@@ -546,7 +551,7 @@ def __getitem__(self, key):
546551
stop = self._start + self._step * stop
547552
step = self._step * step
548553

549-
return RangeIndex(start, stop, step, name=self.name, fastpath=True)
554+
return RangeIndex._simple_new(start, stop, step, name=self.name)
550555

551556
# fall back to Int64Index
552557
return super_getitem(key)
@@ -559,12 +564,12 @@ def __floordiv__(self, other):
559564
start = self._start // other
560565
step = self._step // other
561566
stop = start + len(self) * step
562-
return RangeIndex(start, stop, step, name=self.name,
563-
fastpath=True)
567+
return RangeIndex._simple_new(
568+
start, stop, step, name=self.name)
564569
if len(self) == 1:
565570
start = self._start // other
566-
return RangeIndex(start, start + 1, 1, name=self.name,
567-
fastpath=True)
571+
return RangeIndex._simple_new(
572+
start, start + 1, 1, name=self.name)
568573
return self._int64index // other
569574

570575
@classmethod

pandas/tests/indexes/test_base.py

+29
Original file line numberDiff line numberDiff line change
@@ -2530,3 +2530,32 @@ def test_index_subclass_constructor_wrong_kwargs(index_maker):
25302530
# GH #19348
25312531
with tm.assert_raises_regex(TypeError, 'unexpected keyword argument'):
25322532
index_maker(foo='bar')
2533+
2534+
2535+
def test_deprecated_fastpath():
2536+
2537+
with tm.assert_produces_warning(FutureWarning):
2538+
idx = pd.Index(
2539+
np.array(['a', 'b'], dtype=object), name='test', fastpath=True)
2540+
2541+
expected = pd.Index(['a', 'b'], name='test')
2542+
tm.assert_index_equal(idx, expected)
2543+
2544+
with tm.assert_produces_warning(FutureWarning):
2545+
idx = pd.Int64Index(
2546+
np.array([1, 2, 3], dtype='int64'), name='test', fastpath=True)
2547+
2548+
expected = pd.Index([1, 2, 3], name='test', dtype='int64')
2549+
tm.assert_index_equal(idx, expected)
2550+
2551+
with tm.assert_produces_warning(FutureWarning):
2552+
idx = pd.RangeIndex(0, 5, 2, name='test', fastpath=True)
2553+
2554+
expected = pd.RangeIndex(0, 5, 2, name='test')
2555+
tm.assert_index_equal(idx, expected)
2556+
2557+
with tm.assert_produces_warning(FutureWarning):
2558+
idx = pd.CategoricalIndex(['a', 'b', 'c'], name='test', fastpath=True)
2559+
2560+
expected = pd.CategoricalIndex(['a', 'b', 'c'], name='test')
2561+
tm.assert_index_equal(idx, expected)

0 commit comments

Comments
 (0)