Skip to content

DEPR: Remove top level numpy/datetime/Sparse object imports #49218

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

Merged
merged 7 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions doc/source/whatsnew/v0.19.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1245,10 +1245,9 @@ Previously, sparse data were ``float64`` dtype by default, even if all inputs we
As of v0.19.0, sparse data keeps the input dtype, and uses more appropriate ``fill_value`` defaults (``0`` for ``int64`` dtype, ``False`` for ``bool`` dtype).

.. ipython:: python
:okwarning:

pd.SparseArray([1, 2, 0, 0], dtype=np.int64)
pd.SparseArray([True, False, False, False])
pd.arrays.SparseArray([1, 2, 0, 0], dtype=np.int64)
pd.arrays.SparseArray([True, False, False, False])

See the :ref:`docs <sparse.dtype>` for more details.

Expand Down
6 changes: 2 additions & 4 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,8 @@ When passed DataFrames whose values are sparse, :func:`concat` will now return a
:class:`Series` or :class:`DataFrame` with sparse values, rather than a :class:`SparseDataFrame` (:issue:`25702`).

.. ipython:: python
:okwarning:

df = pd.DataFrame({"A": pd.SparseArray([0, 1])})
df = pd.DataFrame({"A": pd.arrays.SparseArray([0, 1])})

*Previous behavior*:

Expand Down Expand Up @@ -918,9 +917,8 @@ by a ``Series`` or ``DataFrame`` with sparse values.
**New way**

.. ipython:: python
:okwarning:

df = pd.DataFrame({"A": pd.SparseArray([0, 0, 1, 2])})
df = pd.DataFrame({"A": pd.arrays.SparseArray([0, 0, 1, 2])})
df.dtypes

The memory usage of the two approaches is identical. See :ref:`sparse.migration` for more (:issue:`19239`).
Expand Down
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ Removal of prior version deprecations/changes
- Remove :meth:`DataFrameGroupBy.pad` and :meth:`DataFrameGroupBy.backfill` (:issue:`45076`)
- Remove ``numpy`` argument from :func:`read_json` (:issue:`30636`)
- Removed the ``center`` keyword in :meth:`DataFrame.expanding` (:issue:`20647`)
- Removed the ``pandas.datetime`` submodule (:issue:`30489`)
- Removed the ``pandas.np`` submodule (:issue:`30296`)
- Removed ``pandas.SparseArray`` in favor of :class:`arrays.SparseArray` (:issue:`30642`)
- Removed ``pandas.SparseSeries`` and ``pandas.SparseDataFrame`` (:issue:`30642`)
- Enforced disallowing a string column label into ``times`` in :meth:`DataFrame.ewm` (:issue:`43265`)
- Enforced :meth:`Rolling.count` with ``min_periods=None`` to default to the size of the window (:issue:`31302`)
-
Expand Down
48 changes: 0 additions & 48 deletions pandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,54 +212,6 @@ def __getattr__(name):
"Int64Index": Int64Index,
"UInt64Index": UInt64Index,
}[name]
elif name == "datetime":
warnings.warn(
"The pandas.datetime class is deprecated "
"and will be removed from pandas in a future version. "
"Import from datetime module instead.",
FutureWarning,
stacklevel=2,
)

from datetime import datetime as dt

return dt

elif name == "np":

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(
f"The {name} class is removed from pandas. Accessing it from "
"the top-level namespace will also be removed in the next version.",
FutureWarning,
stacklevel=2,
)

return type(name, (), {})

elif name == "SparseArray":

warnings.warn(
"The pandas.SparseArray class is deprecated "
"and will be removed from pandas in a future version. "
"Use pandas.arrays.SparseArray instead.",
FutureWarning,
stacklevel=2,
)
from pandas.core.arrays.sparse import SparseArray as _SparseArray

return _SparseArray

raise AttributeError(f"module 'pandas' has no attribute '{name}'")

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/sparse/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def from_coo(cls, A, dense_index: bool = False) -> Series:
----------
A : scipy.sparse.coo_matrix
dense_index : bool, default False
If False (default), the SparseSeries index consists of only the
If False (default), the index consists of only the
coords of the non-null entries of the original coo_matrix.
If True, the SparseSeries index consists of the full sorted
If True, the index consists of the full sorted
(row, col) coordinates of the coo_matrix.

Returns
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/sparse/scipy_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def coo_to_sparse_series(
A: scipy.sparse.coo_matrix, dense_index: bool = False
) -> Series:
"""
Convert a scipy.sparse.coo_matrix to a SparseSeries.
Convert a scipy.sparse.coo_matrix to a Series with type sparse.

Parameters
----------
Expand Down
39 changes: 1 addition & 38 deletions pandas/tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ class TestPDApi(Base):
]
private_lib = ["compat", "core", "pandas", "util"]

# these are already deprecated; awaiting removal
deprecated_modules: list[str] = ["np", "datetime"]

# misc
misc = ["IndexSlice", "NaT", "NA"]

Expand Down Expand Up @@ -101,9 +98,6 @@ class TestPDApi(Base):
# these are already deprecated; awaiting removal
deprecated_classes: list[str] = ["Float64Index", "Int64Index", "UInt64Index"]

# these should be deprecated in the future
deprecated_classes_in_future: list[str] = ["SparseArray"]

# external modules exposed in pandas namespace
modules: list[str] = []

Expand Down Expand Up @@ -237,9 +231,7 @@ def test_api_all(self):

def test_depr(self):
deprecated_list = (
self.deprecated_modules
+ self.deprecated_classes
+ self.deprecated_classes_in_future
self.deprecated_classes
+ self.deprecated_funcs
+ self.deprecated_funcs_in_future
)
Expand All @@ -248,35 +240,6 @@ def test_depr(self):
_ = getattr(pd, depr)


def test_datetime():
from datetime import datetime
import warnings

with warnings.catch_warnings():
warnings.simplefilter("ignore", FutureWarning)
assert datetime(2015, 1, 2, 0, 0) == datetime(2015, 1, 2, 0, 0)

assert isinstance(datetime(2015, 1, 2, 0, 0), datetime)


def test_sparsearray():
import warnings

with warnings.catch_warnings():
warnings.simplefilter("ignore", FutureWarning)
assert isinstance(pd.array([1, 2, 3], dtype="Sparse"), pd.SparseArray)


def test_np():
import warnings

import numpy as np

with warnings.catch_warnings():
warnings.simplefilter("ignore", FutureWarning)
assert (pd.np.arange(0, 10) == np.arange(0, 10)).all()


class TestApi(Base):
allowed = ["types", "extensions", "indexers", "interchange"]

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/sparse/test_arithmetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@pytest.fixture(params=["integer", "block"])
def kind(request):
"""kind kwarg to pass to SparseArray/SparseSeries"""
"""kind kwarg to pass to SparseArray"""
return request.param


Expand Down