Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEPR: Deprecate pandas.np module #30386

Merged
merged 14 commits into from
Dec 27, 2019
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ Other enhancements
(:meth:`~DataFrame.to_parquet` / :func:`read_parquet`) using the `'pyarrow'` engine
now preserve those data types with pyarrow >= 1.0.0 (:issue:`20612`).
- The ``partition_cols`` argument in :meth:`DataFrame.to_parquet` now accepts a string (:issue:`27117`)
- The pandas.np submodule is now deprecated. Import numpy directly instead(:issue:`30296`)
lithomas1 marked this conversation as resolved.
Show resolved Hide resolved
- :func:`to_parquet` now appropriately handles the ``schema`` argument for user defined schemas in the pyarrow engine. (:issue: `30270`)
- DataFrame constructor preserve `ExtensionArray` dtype with `ExtensionArray` (:issue:`11363`)

Expand Down
33 changes: 31 additions & 2 deletions pandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
to_datetime,
to_timedelta,
# misc
np,
Grouper,
factorize,
unique,
Expand Down Expand Up @@ -189,7 +188,6 @@
__git_version__ = v.get("full-revisionid")
del get_versions, v


# GH 27101
# TODO: remove Panel compat in 1.0
if pandas.compat.PY37:
Expand All @@ -211,6 +209,19 @@ class Panel:
pass

return Panel

elif name == "np":
lithomas1 marked this conversation as resolved.
Show resolved Hide resolved

warnings.warn(
"The pandas.np module is deprecated and will be removed from pandas in a future version. "
"Import numpy directly instead",
FutureWarning,
stacklevel=2,
)
import numpy as np

return np

elif name in {"SparseSeries", "SparseDataFrame"}:
warnings.warn(
"The {} class is removed from pandas. Accessing it from "
Expand All @@ -236,6 +247,24 @@ class SparseDataFrame:
class SparseSeries:
pass

class numpy:
def __init__(self):
import numpy as np
import warnings

self.np = np
self.warnings = warnings

def __getattr__(self, item):
self.warnings.warn(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im getting a mypy complaint about this. why use self.warnings rather than just warnings?

"The pandas.np module is deprecated and will be removed from pandas in a future version. "
"Import numpy directly instead",
FutureWarning,
stacklevel=2,
)
return getattr(self.np, item)

np = numpy()

# module level doc-string
__doc__ = """
Expand Down
2 changes: 0 additions & 2 deletions pandas/core/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# flake8: noqa

import numpy as np

from pandas._libs import NaT, Period, Timedelta, Timestamp
from pandas._libs.missing import NA

Expand Down
15 changes: 14 additions & 1 deletion pandas/tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class TestPDApi(Base):
]
if not compat.PY37:
classes.extend(["Panel", "SparseSeries", "SparseDataFrame"])
deprecated_modules.extend("np")

# these are already deprecated; awaiting removal
deprecated_classes: List[str] = []
Expand All @@ -101,7 +102,7 @@ class TestPDApi(Base):
deprecated_classes_in_future: List[str] = []

# external modules exposed in pandas namespace
modules = ["np", "datetime"]
modules = ["datetime"]

# top-level functions
funcs = [
Expand Down Expand Up @@ -220,6 +221,18 @@ def test_api(self):
self.ignored,
)

def test_depr(self):
deprecated = (
self.deprecated_modules
+ self.deprecated_classes
+ self.deprecated_classes_in_future
+ self.deprecated_funcs
+ self.deprecated_funcs_in_future
)
for depr in deprecated:
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
lithomas1 marked this conversation as resolved.
Show resolved Hide resolved
getattr(pd, depr)


class TestApi(Base):

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/period/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ def test_indexing(self):
def test_period_index_indexer(self):
# GH4125
idx = pd.period_range("2002-01", "2003-12", freq="M")
df = pd.DataFrame(pd.np.random.randn(24, 10), index=idx)
df = pd.DataFrame(np.random.randn(24, 10), index=idx)
tm.assert_frame_equal(df, df.loc[idx])
tm.assert_frame_equal(df, df.loc[list(idx)])
tm.assert_frame_equal(df, df.loc[list(idx)])
Expand Down