Skip to content

Commit b2a622e

Browse files
authored
DEPR: accepting Manager objects in DataFrame/Series (#52419)
1 parent 3ccdc5b commit b2a622e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+437
-95
lines changed

doc/source/user_guide/10min.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,12 +763,14 @@ Parquet
763763
Writing to a Parquet file:
764764

765765
.. ipython:: python
766+
:okwarning:
766767
767768
df.to_parquet("foo.parquet")
768769
769770
Reading from a Parquet file Store using :func:`read_parquet`:
770771

771772
.. ipython:: python
773+
:okwarning:
772774
773775
pd.read_parquet("foo.parquet")
774776

doc/source/user_guide/io.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,6 +2247,7 @@ For line-delimited json files, pandas can also return an iterator which reads in
22472247
Line-limited json can also be read using the pyarrow reader by specifying ``engine="pyarrow"``.
22482248

22492249
.. ipython:: python
2250+
:okwarning:
22502251
22512252
from io import BytesIO
22522253
df = pd.read_json(BytesIO(jsonl.encode()), lines=True, engine="pyarrow")
@@ -5554,6 +5555,7 @@ Read from an orc file.
55545555
Read only certain columns of an orc file.
55555556

55565557
.. ipython:: python
5558+
:okwarning:
55575559
55585560
result = pd.read_orc(
55595561
"example_pa.orc",

doc/source/user_guide/pyarrow.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ To convert a :external+pyarrow:py:class:`pyarrow.Table` to a :class:`DataFrame`,
104104
:external+pyarrow:py:meth:`pyarrow.Table.to_pandas` method with ``types_mapper=pd.ArrowDtype``.
105105

106106
.. ipython:: python
107+
:okwarning:
107108
108109
table = pa.table([pa.array([1, 2, 3], type=pa.int64())], names=["a"])
109110
@@ -164,6 +165,7 @@ functions provide an ``engine`` keyword that can dispatch to PyArrow to accelera
164165
* :func:`read_feather`
165166

166167
.. ipython:: python
168+
:okwarning:
167169
168170
import io
169171
data = io.StringIO("""a,b,c
@@ -178,6 +180,7 @@ PyArrow-backed data by specifying the parameter ``dtype_backend="pyarrow"``. A r
178180
``engine="pyarrow"`` to necessarily return PyArrow-backed data.
179181

180182
.. ipython:: python
183+
:okwarning:
181184
182185
import io
183186
data = io.StringIO("""a,b,c,d,e,f,g,h,i

doc/source/user_guide/scale.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ To load the columns we want, we have two options.
5151
Option 1 loads in all the data and then filters to what we need.
5252

5353
.. ipython:: python
54+
:okwarning:
5455
5556
columns = ["id_0", "name_0", "x_0", "y_0"]
5657
@@ -59,6 +60,7 @@ Option 1 loads in all the data and then filters to what we need.
5960
Option 2 only loads the columns we request.
6061

6162
.. ipython:: python
63+
:okwarning:
6264
6365
pd.read_parquet("timeseries_wide.parquet", columns=columns)
6466
@@ -200,6 +202,7 @@ counts up to this point. As long as each individual file fits in memory, this wi
200202
work for arbitrary-sized datasets.
201203

202204
.. ipython:: python
205+
:okwarning:
203206
204207
%%time
205208
files = pathlib.Path("data/timeseries/").glob("ts*.parquet")

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ When this keyword is set to ``"pyarrow"``, then these functions will return pyar
152152
* :meth:`Series.convert_dtypes`
153153

154154
.. ipython:: python
155+
:okwarning:
155156
156157
import io
157158
data = io.StringIO("""a,b,c,d,e,f,g,h,i

doc/source/whatsnew/v2.2.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ Other Deprecations
249249
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_pickle` except ``path``. (:issue:`54229`)
250250
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_string` except ``buf``. (:issue:`54229`)
251251
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_xml` except ``path_or_buffer``. (:issue:`54229`)
252+
- Deprecated allowing passing :class:`BlockManager` objects to :class:`DataFrame` or :class:`SingleBlockManager` objects to :class:`Series` (:issue:`52419`)
252253
- Deprecated automatic downcasting of object-dtype results in :meth:`Series.replace` and :meth:`DataFrame.replace`, explicitly call ``result = result.infer_objects(copy=False)`` instead. To opt in to the future version, use ``pd.set_option("future.no_silent_downcasting", True)`` (:issue:`54710`)
253254
- Deprecated downcasting behavior in :meth:`Series.where`, :meth:`DataFrame.where`, :meth:`Series.mask`, :meth:`DataFrame.mask`, :meth:`Series.clip`, :meth:`DataFrame.clip`; in a future version these will not infer object-dtype columns to non-object dtype, or all-round floats to integer dtype. Call ``result.infer_objects(copy=False)`` on the result for object inference, or explicitly cast floats to ints. To opt in to the future version, use ``pd.set_option("future.no_silent_downcasting", True)`` (:issue:`53656`)
254255
- Deprecated including the groups in computations when using :meth:`DataFrameGroupBy.apply` and :meth:`DataFrameGroupBy.resample`; pass ``include_groups=False`` to exclude the groups (:issue:`7155`)

pandas/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ def pytest_collection_modifyitems(items, config) -> None:
178178
"DataFrameGroupBy.fillna",
179179
"DataFrame.fillna with 'method' is deprecated",
180180
),
181+
("read_parquet", "Passing a BlockManager to DataFrame is deprecated"),
181182
]
182183

183184
for item in items:

pandas/core/arraylike.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,10 @@ def array_ufunc(self, ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any)
263263
Series,
264264
)
265265
from pandas.core.generic import NDFrame
266-
from pandas.core.internals import BlockManager
266+
from pandas.core.internals import (
267+
ArrayManager,
268+
BlockManager,
269+
)
267270

268271
cls = type(self)
269272

@@ -347,7 +350,7 @@ def _reconstruct(result):
347350
if method == "outer":
348351
raise NotImplementedError
349352
return result
350-
if isinstance(result, BlockManager):
353+
if isinstance(result, (BlockManager, ArrayManager)):
351354
# we went through BlockManager.apply e.g. np.sqrt
352355
result = self._constructor_from_mgr(result, axes=result.axes)
353356
else:

pandas/core/frame.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,6 @@ def _constructor(self) -> Callable[..., DataFrame]:
644644

645645
def _constructor_from_mgr(self, mgr, axes):
646646
df = self._from_mgr(mgr, axes=axes)
647-
648647
if type(self) is DataFrame:
649648
# fastpath avoiding constructor call
650649
return df
@@ -677,17 +676,29 @@ def __init__(
677676
dtype: Dtype | None = None,
678677
copy: bool | None = None,
679678
) -> None:
679+
allow_mgr = False
680680
if dtype is not None:
681681
dtype = self._validate_dtype(dtype)
682682

683683
if isinstance(data, DataFrame):
684684
data = data._mgr
685+
allow_mgr = True
685686
if not copy:
686687
# if not copying data, ensure to still return a shallow copy
687688
# to avoid the result sharing the same Manager
688689
data = data.copy(deep=False)
689690

690691
if isinstance(data, (BlockManager, ArrayManager)):
692+
if not allow_mgr:
693+
# GH#52419
694+
warnings.warn(
695+
f"Passing a {type(data).__name__} to {type(self).__name__} "
696+
"is deprecated and will raise in a future version. "
697+
"Use public APIs instead.",
698+
DeprecationWarning,
699+
stacklevel=find_stack_level(),
700+
)
701+
691702
if using_copy_on_write():
692703
data = data.copy(deep=False)
693704
# first check if a Manager is passed without any other arguments
@@ -2462,7 +2473,7 @@ def maybe_reorder(
24622473
manager = _get_option("mode.data_manager", silent=True)
24632474
mgr = arrays_to_mgr(arrays, columns, result_index, typ=manager)
24642475

2465-
return cls(mgr)
2476+
return cls._from_mgr(mgr, axes=mgr.axes)
24662477

24672478
def to_records(
24682479
self, index: bool = True, column_dtypes=None, index_dtypes=None
@@ -2672,7 +2683,7 @@ def _from_arrays(
26722683
verify_integrity=verify_integrity,
26732684
typ=manager,
26742685
)
2675-
return cls(mgr)
2686+
return cls._from_mgr(mgr, axes=mgr.axes)
26762687

26772688
@doc(
26782689
storage_options=_shared_docs["storage_options"],

pandas/core/generic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,8 @@ def swapaxes(self, axis1: Axis, axis2: Axis, copy: bool_t | None = None) -> Self
829829
if not using_copy_on_write() and copy is not False:
830830
new_mgr = new_mgr.copy(deep=True)
831831

832-
return self._constructor(new_mgr).__finalize__(self, method="swapaxes")
832+
out = self._constructor_from_mgr(new_mgr, axes=new_mgr.axes)
833+
return out.__finalize__(self, method="swapaxes")
833834

834835
return self._constructor(
835836
new_values,

0 commit comments

Comments
 (0)