Skip to content

Commit 5cc3861

Browse files
committed
add deprecation trigger in pd.__init__.py
1 parent 1faf9b9 commit 5cc3861

31 files changed

+120
-62
lines changed

pandas/__init__.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
# indexes
7272
Index,
7373
CategoricalIndex,
74-
Int64Index,
7574
UInt64Index,
7675
RangeIndex,
7776
Float64Index,
@@ -187,10 +186,41 @@
187186

188187

189188
# GH 27101
189+
deprecated_num_index_names = ["Float64Index", "Int64Index", "UInt64Index"]
190+
191+
192+
def __dir__():
193+
return list(globals().keys()) + deprecated_num_index_names
194+
195+
190196
def __getattr__(name):
191197
import warnings
192198

193-
if name == "datetime":
199+
if name in deprecated_num_index_names:
200+
num_msg = (
201+
f"The pandas.{name} class is deprecated "
202+
"and will be removed from pandas in a future version. "
203+
"Use pandas.NumericIndex with the appropriate dtype instead."
204+
)
205+
if name == "Int64Index":
206+
from pandas.core.api import Int64Index
207+
208+
warnings.warn(num_msg, FutureWarning, stacklevel=2)
209+
return Int64Index
210+
elif name == "Unt64Index":
211+
from pandas.core.api import UInt64Index
212+
213+
warnings.warn(num_msg, FutureWarning, stacklevel=2)
214+
return UInt64Index
215+
elif name == "Float64Index":
216+
from pandas.core.api import Float64Index
217+
218+
warnings.warn(num_msg, FutureWarning, stacklevel=2)
219+
return Float64Index
220+
else:
221+
raise NameError(name)
222+
223+
elif name == "datetime":
194224
warnings.warn(
195225
"The pandas.datetime class is deprecated "
196226
"and will be removed from pandas in a future version. "

pandas/_testing/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,12 @@
4545
CategoricalIndex,
4646
DataFrame,
4747
DatetimeIndex,
48-
Float64Index,
4948
Index,
50-
Int64Index,
5149
IntervalIndex,
5250
MultiIndex,
5351
NumericIndex,
5452
RangeIndex,
5553
Series,
56-
UInt64Index,
5754
bdate_range,
5855
)
5956
from pandas._testing._io import ( # noqa:F401
@@ -106,6 +103,11 @@
106103
use_numexpr,
107104
with_csv_dialect,
108105
)
106+
from pandas.core.api import (
107+
Float64Index,
108+
Int64Index,
109+
UInt64Index,
110+
)
109111
from pandas.core.arrays import (
110112
DatetimeArray,
111113
PandasArray,

pandas/core/algorithms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ def __init__(self, obj, n: int, keep: str, columns):
13341334

13351335
def compute(self, method: str) -> DataFrame:
13361336

1337-
from pandas import Int64Index
1337+
from pandas.core.api import Int64Index
13381338

13391339
n = self.n
13401340
frame = self.obj

pandas/core/indexes/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,15 +572,15 @@ def _dtype_to_subclass(cls, dtype: DtypeObj):
572572
return TimedeltaIndex
573573

574574
elif is_float_dtype(dtype):
575-
from pandas import Float64Index
575+
from pandas.core.api import Float64Index
576576

577577
return Float64Index
578578
elif is_unsigned_integer_dtype(dtype):
579-
from pandas import UInt64Index
579+
from pandas.core.api import UInt64Index
580580

581581
return UInt64Index
582582
elif is_signed_integer_dtype(dtype):
583-
from pandas import Int64Index
583+
from pandas.core.api import Int64Index
584584

585585
return Int64Index
586586

pandas/io/feather_format.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
from pandas.compat._optional import import_optional_dependency
1010
from pandas.util._decorators import doc
1111

12-
from pandas import (
12+
from pandas.core import generic
13+
from pandas.core.api import (
1314
DataFrame,
1415
Int64Index,
1516
RangeIndex,
1617
)
17-
from pandas.core import generic
1818

1919
from pandas.io.common import get_handle
2020

pandas/io/pytables.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@
6565
DataFrame,
6666
DatetimeIndex,
6767
Index,
68-
Int64Index,
6968
MultiIndex,
7069
PeriodIndex,
7170
Series,
7271
TimedeltaIndex,
7372
concat,
7473
isna,
7574
)
75+
from pandas.core.api import Int64Index
7676
from pandas.core.arrays import (
7777
Categorical,
7878
DatetimeArray,

pandas/tests/frame/constructors/test_from_records.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
CategoricalIndex,
1212
DataFrame,
1313
Index,
14-
Int64Index,
1514
Interval,
1615
RangeIndex,
1716
Series,
@@ -450,7 +449,7 @@ def test_from_records_empty_with_nonempty_fields_gh3682(self):
450449
a = np.array([(1, 2)], dtype=[("id", np.int64), ("value", np.int64)])
451450
df = DataFrame.from_records(a, index="id")
452451

453-
ex_index = Int64Index([1], name="id")
452+
ex_index = Index([1], name="id")
454453
expected = DataFrame({"value": [2]}, index=ex_index, columns=["value"])
455454
tm.assert_frame_equal(df, expected)
456455

pandas/tests/frame/methods/test_truncate.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import pandas as pd
55
from pandas import (
66
DataFrame,
7+
DatetimeIndex,
78
Series,
89
date_range,
910
)
1011
import pandas._testing as tm
12+
from pandas.core.api import Int64Index
1113

1214

1315
class TestDataFrameTruncate:
@@ -116,13 +118,13 @@ def test_truncate_nonsortedindex_axis1(self):
116118
"before, after, indices",
117119
[(1, 2, [2, 1]), (None, 2, [2, 1, 0]), (1, None, [3, 2, 1])],
118120
)
119-
@pytest.mark.parametrize("klass", [pd.Int64Index, pd.DatetimeIndex])
121+
@pytest.mark.parametrize("klass", [Int64Index, DatetimeIndex])
120122
def test_truncate_decreasing_index(
121123
self, before, after, indices, klass, frame_or_series
122124
):
123125
# https://github.com/pandas-dev/pandas/issues/33756
124126
idx = klass([3, 2, 1, 0])
125-
if klass is pd.DatetimeIndex:
127+
if klass is DatetimeIndex:
126128
before = pd.Timestamp(before) if before is not None else None
127129
after = pd.Timestamp(after) if after is not None else None
128130
indices = [pd.Timestamp(i) for i in indices]

pandas/tests/frame/test_constructors.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
PeriodArray,
5454
SparseArray,
5555
)
56+
from pandas.core.api import Int64Index
5657

5758
MIXED_FLOAT_DTYPES = ["float16", "float32", "float64"]
5859
MIXED_INT_DTYPES = [
@@ -580,7 +581,7 @@ def test_constructor_2d_index(self):
580581
df = DataFrame([[1]], columns=[[1]], index=[1, 2])
581582
expected = DataFrame(
582583
[1, 1],
583-
index=pd.Int64Index([1, 2], dtype="int64"),
584+
index=Int64Index([1, 2], dtype="int64"),
584585
columns=MultiIndex(levels=[[1]], codes=[[0]]),
585586
)
586587
tm.assert_frame_equal(df, expected)

pandas/tests/indexes/common.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
from pandas import (
2222
CategoricalIndex,
2323
DatetimeIndex,
24-
Float64Index,
2524
Index,
26-
Int64Index,
2725
IntervalIndex,
2826
MultiIndex,
2927
NumericIndex,
@@ -33,8 +31,12 @@
3331
TimedeltaIndex,
3432
isna,
3533
)
36-
from pandas import UInt64Index # noqa:F401
3734
import pandas._testing as tm
35+
from pandas.core.api import ( # noqa:F401
36+
Float64Index,
37+
Int64Index,
38+
UInt64Index,
39+
)
3840
from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin
3941

4042

pandas/tests/indexes/datetimes/methods/test_astype.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
from pandas import (
1010
DatetimeIndex,
1111
Index,
12-
Int64Index,
1312
NaT,
1413
PeriodIndex,
1514
Timestamp,
1615
date_range,
1716
)
1817
import pandas._testing as tm
18+
from pandas.core.api import Int64Index
1919

2020

2121
class TestDatetimeIndex:

pandas/tests/indexes/datetimes/test_setops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
DataFrame,
1111
DatetimeIndex,
1212
Index,
13-
Int64Index,
1413
Series,
1514
bdate_range,
1615
date_range,
1716
)
1817
import pandas._testing as tm
18+
from pandas.core.api import Int64Index
1919

2020
from pandas.tseries.offsets import (
2121
BMonthEnd,

pandas/tests/indexes/interval/test_constructors.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
from pandas import (
1010
Categorical,
1111
CategoricalIndex,
12-
Float64Index,
1312
Index,
14-
Int64Index,
1513
Interval,
1614
IntervalIndex,
1715
date_range,
@@ -20,6 +18,10 @@
2018
timedelta_range,
2119
)
2220
import pandas._testing as tm
21+
from pandas.core.api import (
22+
Float64Index,
23+
Int64Index,
24+
)
2325
from pandas.core.arrays import IntervalArray
2426
import pandas.core.common as com
2527

pandas/tests/indexes/multi/test_drop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,5 @@ def test_droplevel_multiindex_one_level():
189189
# GH#37208
190190
index = MultiIndex.from_tuples([(2,)], names=("b",))
191191
result = index.droplevel([])
192-
expected = pd.Int64Index([2], name="b")
192+
expected = Index([2], name="b")
193193
tm.assert_index_equal(result, expected)

pandas/tests/indexes/multi/test_integrity.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
RangeIndex,
1313
)
1414
import pandas._testing as tm
15+
from pandas.core.api import Int64Index
1516

1617

1718
def test_labels_dtypes():
@@ -83,17 +84,17 @@ def test_values_multiindex_periodindex():
8384
idx = MultiIndex.from_arrays([ints, pidx])
8485
result = idx.values
8586

86-
outer = pd.Int64Index([x[0] for x in result])
87-
tm.assert_index_equal(outer, pd.Int64Index(ints))
87+
outer = Int64Index([x[0] for x in result])
88+
tm.assert_index_equal(outer, Int64Index(ints))
8889

8990
inner = pd.PeriodIndex([x[1] for x in result])
9091
tm.assert_index_equal(inner, pidx)
9192

9293
# n_lev > n_lab
9394
result = idx[:2].values
9495

95-
outer = pd.Int64Index([x[0] for x in result])
96-
tm.assert_index_equal(outer, pd.Int64Index(ints[:2]))
96+
outer = Int64Index([x[0] for x in result])
97+
tm.assert_index_equal(outer, Int64Index(ints[:2]))
9798

9899
inner = pd.PeriodIndex([x[1] for x in result])
99100
tm.assert_index_equal(inner, pidx[:2])
@@ -246,11 +247,11 @@ def test_rangeindex_fallback_coercion_bug():
246247
tm.assert_frame_equal(df, expected, check_like=True)
247248

248249
result = df.index.get_level_values("fizz")
249-
expected = pd.Int64Index(np.arange(10), name="fizz").repeat(10)
250+
expected = Int64Index(np.arange(10), name="fizz").repeat(10)
250251
tm.assert_index_equal(result, expected)
251252

252253
result = df.index.get_level_values("buzz")
253-
expected = pd.Int64Index(np.tile(np.arange(10), 10), name="buzz")
254+
expected = Int64Index(np.tile(np.arange(10), 10), name="buzz")
254255
tm.assert_index_equal(result, expected)
255256

256257

pandas/tests/indexes/multi/test_names.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def test_setting_names_from_levels_raises():
148148
new.index.name = "bar"
149149

150150
assert pd.Index._no_setting_name is False
151-
assert pd.Int64Index._no_setting_name is False
151+
assert pd.NumericIndex._no_setting_name is False
152152
assert pd.RangeIndex._no_setting_name is False
153153

154154

pandas/tests/indexes/period/test_indexing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
period_range,
2424
)
2525
import pandas._testing as tm
26+
from pandas.core.api import Int64Index
2627

2728
dti4 = date_range("2016-01-01", periods=4)
2829
dti = dti4[:-1]
@@ -929,7 +930,7 @@ def test_asof_locs_mismatched_type(self):
929930

930931
msg = "must be DatetimeIndex or PeriodIndex"
931932
with pytest.raises(TypeError, match=msg):
932-
pi.asof_locs(pd.Int64Index(pi.asi8), mask)
933+
pi.asof_locs(Int64Index(pi.asi8), mask)
933934

934935
with pytest.raises(TypeError, match=msg):
935936
pi.asof_locs(pd.Float64Index(pi.asi8), mask)

pandas/tests/indexes/ranges/test_indexing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import numpy as np
22
import pytest
33

4-
import pandas as pd
54
from pandas import RangeIndex
65
import pandas._testing as tm
6+
from pandas.core.api import Int64Index
77

88

99
class TestGetIndexer:
@@ -55,7 +55,7 @@ def test_take_fill_value(self):
5555
# GH#12631
5656
idx = RangeIndex(1, 4, name="xxx")
5757
result = idx.take(np.array([1, 0, -1]))
58-
expected = pd.Int64Index([2, 1, 3], name="xxx")
58+
expected = Int64Index([2, 1, 3], name="xxx")
5959
tm.assert_index_equal(result, expected)
6060

6161
# fill_value
@@ -65,7 +65,7 @@ def test_take_fill_value(self):
6565

6666
# allow_fill=False
6767
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
68-
expected = pd.Int64Index([2, 1, 3], name="xxx")
68+
expected = Int64Index([2, 1, 3], name="xxx")
6969
tm.assert_index_equal(result, expected)
7070

7171
msg = "Unable to fill values because RangeIndex cannot contain NA"

0 commit comments

Comments
 (0)