Skip to content

Commit 52d5520

Browse files
lithomas1AlexKirko
authored andcommitted
DEPR: Deprecate pandas.np module (pandas-dev#30386)
1 parent b35a5f4 commit 52d5520

File tree

5 files changed

+65
-11
lines changed

5 files changed

+65
-11
lines changed

doc/source/whatsnew/v1.0.0.rst

100644100755
+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ Other enhancements
205205
(:meth:`~DataFrame.to_parquet` / :func:`read_parquet`) using the `'pyarrow'` engine
206206
now preserve those data types with pyarrow >= 1.0.0 (:issue:`20612`).
207207
- The ``partition_cols`` argument in :meth:`DataFrame.to_parquet` now accepts a string (:issue:`27117`)
208+
- The ``pandas.np`` submodule is now deprecated. Import numpy directly instead (:issue:`30296`)
208209
- :func:`to_parquet` now appropriately handles the ``schema`` argument for user defined schemas in the pyarrow engine. (:issue: `30270`)
209210
- DataFrame constructor preserve `ExtensionArray` dtype with `ExtensionArray` (:issue:`11363`)
210211
- :meth:`DataFrame.sort_values` and :meth:`Series.sort_values` have gained ``ignore_index`` keyword to be able to reset index after sorting (:issue:`30114`)

pandas/__init__.py

+36-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@
105105
to_datetime,
106106
to_timedelta,
107107
# misc
108-
np,
109108
Grouper,
110109
factorize,
111110
unique,
@@ -189,7 +188,6 @@
189188
__git_version__ = v.get("full-revisionid")
190189
del get_versions, v
191190

192-
193191
# GH 27101
194192
# TODO: remove Panel compat in 1.0
195193
if pandas.compat.PY37:
@@ -211,6 +209,20 @@ class Panel:
211209
pass
212210

213211
return Panel
212+
213+
elif name == "np":
214+
215+
warnings.warn(
216+
"The pandas.np module is deprecated "
217+
"and will be removed from pandas in a future version. "
218+
"Import numpy directly instead",
219+
FutureWarning,
220+
stacklevel=2,
221+
)
222+
import numpy as np
223+
224+
return np
225+
214226
elif name in {"SparseSeries", "SparseDataFrame"}:
215227
warnings.warn(
216228
f"The {name} class is removed from pandas. Accessing it from "
@@ -236,6 +248,28 @@ class SparseDataFrame:
236248
class SparseSeries:
237249
pass
238250

251+
class __numpy:
252+
def __init__(self):
253+
import numpy as np
254+
import warnings
255+
256+
self.np = np
257+
self.warnings = warnings
258+
259+
def __getattr__(self, item):
260+
self.warnings.warn(
261+
"The pandas.np module is deprecated "
262+
"and will be removed from pandas in a future version. "
263+
"Import numpy directly instead",
264+
FutureWarning,
265+
stacklevel=2,
266+
)
267+
try:
268+
return getattr(self.np, item)
269+
except AttributeError:
270+
raise AttributeError(f"module numpy has no attribute {item}")
271+
272+
np = __numpy()
239273

240274
# module level doc-string
241275
__doc__ = """

pandas/core/api.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# flake8: noqa
22

3-
import numpy as np
4-
53
from pandas._libs import NaT, Period, Timedelta, Timestamp
64
from pandas._libs.missing import NA
75

pandas/tests/api/test_api.py

+27-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ def check(self, namespace, expected, ignored=None):
2020

2121

2222
class TestPDApi(Base):
23-
2423
# these are optionally imported based on testing
2524
# & need to be ignored
2625
ignored = ["tests", "locale", "conftest"]
@@ -93,6 +92,7 @@ class TestPDApi(Base):
9392
]
9493
if not compat.PY37:
9594
classes.extend(["Panel", "SparseSeries", "SparseDataFrame"])
95+
deprecated_modules.append("np")
9696

9797
# these are already deprecated; awaiting removal
9898
deprecated_classes: List[str] = []
@@ -101,7 +101,7 @@ class TestPDApi(Base):
101101
deprecated_classes_in_future: List[str] = []
102102

103103
# external modules exposed in pandas namespace
104-
modules = ["np", "datetime"]
104+
modules = ["datetime"]
105105

106106
# top-level functions
107107
funcs = [
@@ -220,22 +220,43 @@ def test_api(self):
220220
self.ignored,
221221
)
222222

223+
def test_depr(self):
224+
deprecated = (
225+
self.deprecated_modules
226+
+ self.deprecated_classes
227+
+ self.deprecated_classes_in_future
228+
+ self.deprecated_funcs
229+
+ self.deprecated_funcs_in_future
230+
)
231+
for depr in deprecated:
232+
with tm.assert_produces_warning(FutureWarning):
233+
if compat.PY37:
234+
getattr(pd, depr)
235+
else:
236+
deprecated = getattr(pd, depr)
237+
deprecated.__getattr__(dir(deprecated)[-1])
223238

224-
class TestApi(Base):
225239

240+
def test_np():
241+
import numpy as np
242+
import warnings
243+
244+
with warnings.catch_warnings():
245+
warnings.simplefilter("ignore", FutureWarning)
246+
assert (pd.np.arange(0, 10) == np.arange(0, 10)).all()
247+
248+
249+
class TestApi(Base):
226250
allowed = ["types", "extensions", "indexers"]
227251

228252
def test_api(self):
229-
230253
self.check(api, self.allowed)
231254

232255

233256
class TestTesting(Base):
234-
235257
funcs = ["assert_frame_equal", "assert_series_equal", "assert_index_equal"]
236258

237259
def test_testing(self):
238-
239260
from pandas import testing
240261

241262
self.check(testing, self.funcs)

pandas/tests/indexes/period/test_indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ def test_indexing(self):
679679
def test_period_index_indexer(self):
680680
# GH4125
681681
idx = pd.period_range("2002-01", "2003-12", freq="M")
682-
df = pd.DataFrame(pd.np.random.randn(24, 10), index=idx)
682+
df = pd.DataFrame(np.random.randn(24, 10), index=idx)
683683
tm.assert_frame_equal(df, df.loc[idx])
684684
tm.assert_frame_equal(df, df.loc[list(idx)])
685685
tm.assert_frame_equal(df, df.loc[list(idx)])

0 commit comments

Comments
 (0)