Skip to content

Commit 775f716

Browse files
DEPR: fix stacklevel for DataFrame(mgr) deprecation (pandas-dev#55591)
1 parent a167f13 commit 775f716

22 files changed

+134
-215
lines changed

doc/source/user_guide/10min.rst

-2
Original file line numberDiff line numberDiff line change
@@ -763,14 +763,12 @@ Parquet
763763
Writing to a Parquet file:
764764

765765
.. ipython:: python
766-
:okwarning:
767766
768767
df.to_parquet("foo.parquet")
769768
770769
Reading from a Parquet file Store using :func:`read_parquet`:
771770

772771
.. ipython:: python
773-
:okwarning:
774772
775773
pd.read_parquet("foo.parquet")
776774

doc/source/user_guide/io.rst

-11
Original file line numberDiff line numberDiff line change
@@ -2247,7 +2247,6 @@ 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:
22512250
22522251
from io import BytesIO
22532252
df = pd.read_json(BytesIO(jsonl.encode()), lines=True, engine="pyarrow")
@@ -5372,15 +5371,13 @@ See the documentation for `pyarrow <https://arrow.apache.org/docs/python/>`__ an
53725371
Write to a parquet file.
53735372

53745373
.. ipython:: python
5375-
:okwarning:
53765374
53775375
df.to_parquet("example_pa.parquet", engine="pyarrow")
53785376
df.to_parquet("example_fp.parquet", engine="fastparquet")
53795377
53805378
Read from a parquet file.
53815379

53825380
.. ipython:: python
5383-
:okwarning:
53845381
53855382
result = pd.read_parquet("example_fp.parquet", engine="fastparquet")
53865383
result = pd.read_parquet("example_pa.parquet", engine="pyarrow")
@@ -5390,7 +5387,6 @@ Read from a parquet file.
53905387
By setting the ``dtype_backend`` argument you can control the default dtypes used for the resulting DataFrame.
53915388

53925389
.. ipython:: python
5393-
:okwarning:
53945390
53955391
result = pd.read_parquet("example_pa.parquet", engine="pyarrow", dtype_backend="pyarrow")
53965392
@@ -5404,7 +5400,6 @@ By setting the ``dtype_backend`` argument you can control the default dtypes use
54045400
Read only certain columns of a parquet file.
54055401

54065402
.. ipython:: python
5407-
:okwarning:
54085403
54095404
result = pd.read_parquet(
54105405
"example_fp.parquet",
@@ -5433,7 +5428,6 @@ Serializing a ``DataFrame`` to parquet may include the implicit index as one or
54335428
more columns in the output file. Thus, this code:
54345429

54355430
.. ipython:: python
5436-
:okwarning:
54375431
54385432
df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
54395433
df.to_parquet("test.parquet", engine="pyarrow")
@@ -5450,7 +5444,6 @@ If you want to omit a dataframe's indexes when writing, pass ``index=False`` to
54505444
:func:`~pandas.DataFrame.to_parquet`:
54515445

54525446
.. ipython:: python
5453-
:okwarning:
54545447
54555448
df.to_parquet("test.parquet", index=False)
54565449
@@ -5473,7 +5466,6 @@ Partitioning Parquet files
54735466
Parquet supports partitioning of data based on the values of one or more columns.
54745467

54755468
.. ipython:: python
5476-
:okwarning:
54775469
54785470
df = pd.DataFrame({"a": [0, 0, 1, 1], "b": [0, 1, 0, 1]})
54795471
df.to_parquet(path="test", engine="pyarrow", partition_cols=["a"], compression=None)
@@ -5539,14 +5531,12 @@ ORC format, :func:`~pandas.read_orc` and :func:`~pandas.DataFrame.to_orc`. This
55395531
Write to an orc file.
55405532

55415533
.. ipython:: python
5542-
:okwarning:
55435534
55445535
df.to_orc("example_pa.orc", engine="pyarrow")
55455536
55465537
Read from an orc file.
55475538

55485539
.. ipython:: python
5549-
:okwarning:
55505540
55515541
result = pd.read_orc("example_pa.orc")
55525542
@@ -5555,7 +5545,6 @@ Read from an orc file.
55555545
Read only certain columns of an orc file.
55565546

55575547
.. ipython:: python
5558-
:okwarning:
55595548
55605549
result = pd.read_orc(
55615550
"example_pa.orc",

doc/source/user_guide/pyarrow.rst

-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ 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:
108107
109108
table = pa.table([pa.array([1, 2, 3], type=pa.int64())], names=["a"])
110109
@@ -165,7 +164,6 @@ functions provide an ``engine`` keyword that can dispatch to PyArrow to accelera
165164
* :func:`read_feather`
166165

167166
.. ipython:: python
168-
:okwarning:
169167
170168
import io
171169
data = io.StringIO("""a,b,c
@@ -180,7 +178,6 @@ PyArrow-backed data by specifying the parameter ``dtype_backend="pyarrow"``. A r
180178
``engine="pyarrow"`` to necessarily return PyArrow-backed data.
181179

182180
.. ipython:: python
183-
:okwarning:
184181
185182
import io
186183
data = io.StringIO("""a,b,c,d,e,f,g,h,i

doc/source/user_guide/scale.rst

-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ 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:
5554
5655
columns = ["id_0", "name_0", "x_0", "y_0"]
5756
@@ -60,7 +59,6 @@ Option 1 loads in all the data and then filters to what we need.
6059
Option 2 only loads the columns we request.
6160

6261
.. ipython:: python
63-
:okwarning:
6462
6563
pd.read_parquet("timeseries_wide.parquet", columns=columns)
6664
@@ -202,7 +200,6 @@ counts up to this point. As long as each individual file fits in memory, this wi
202200
work for arbitrary-sized datasets.
203201

204202
.. ipython:: python
205-
:okwarning:
206203
207204
%%time
208205
files = pathlib.Path("data/timeseries/").glob("ts*.parquet")

doc/source/whatsnew/v2.0.0.rst

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

154154
.. ipython:: python
155-
:okwarning:
156155
157156
import io
158157
data = io.StringIO("""a,b,c,d,e,f,g,h,i

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ def __init__(
697697
"is deprecated and will raise in a future version. "
698698
"Use public APIs instead.",
699699
DeprecationWarning,
700-
stacklevel=find_stack_level(),
700+
stacklevel=1, # bump to 2 once pyarrow 15.0 is released with fix
701701
)
702702

703703
if using_copy_on_write():

pandas/core/series.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def __init__(
407407
"is deprecated and will raise in a future version. "
408408
"Use public APIs instead.",
409409
DeprecationWarning,
410-
stacklevel=find_stack_level(),
410+
stacklevel=2,
411411
)
412412
if using_copy_on_write():
413413
data = data.copy(deep=False)
@@ -446,7 +446,7 @@ def __init__(
446446
"is deprecated and will raise in a future version. "
447447
"Use public APIs instead.",
448448
DeprecationWarning,
449-
stacklevel=find_stack_level(),
449+
stacklevel=2,
450450
)
451451

452452
if copy:
@@ -465,7 +465,7 @@ def __init__(
465465
"is deprecated and will raise in a future version. "
466466
"Use public APIs instead.",
467467
DeprecationWarning,
468-
stacklevel=find_stack_level(),
468+
stacklevel=2,
469469
)
470470

471471
name = ibase.maybe_extract_name(name, data, type(self))
@@ -539,7 +539,7 @@ def __init__(
539539
"is deprecated and will raise in a future version. "
540540
"Use public APIs instead.",
541541
DeprecationWarning,
542-
stacklevel=find_stack_level(),
542+
stacklevel=2,
543543
)
544544
allow_mgr = True
545545

pandas/tests/arrays/interval/test_interval.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ def test_arrow_array_missing():
309309
assert result.storage.equals(expected)
310310

311311

312+
@pytest.mark.filterwarnings(
313+
"ignore:Passing a BlockManager to DataFrame:DeprecationWarning"
314+
)
312315
@pytest.mark.parametrize(
313316
"breaks",
314317
[[0.0, 1.0, 2.0, 3.0], date_range("2017", periods=4, freq="D")],
@@ -325,29 +328,26 @@ def test_arrow_table_roundtrip(breaks):
325328

326329
table = pa.table(df)
327330
assert isinstance(table.field("a").type, ArrowIntervalType)
328-
msg = "Passing a BlockManager to DataFrame is deprecated"
329-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
330-
result = table.to_pandas()
331+
result = table.to_pandas()
331332
assert isinstance(result["a"].dtype, pd.IntervalDtype)
332333
tm.assert_frame_equal(result, df)
333334

334335
table2 = pa.concat_tables([table, table])
335-
msg = "Passing a BlockManager to DataFrame is deprecated"
336-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
337-
result = table2.to_pandas()
336+
result = table2.to_pandas()
338337
expected = pd.concat([df, df], ignore_index=True)
339338
tm.assert_frame_equal(result, expected)
340339

341340
# GH-41040
342341
table = pa.table(
343342
[pa.chunked_array([], type=table.column(0).type)], schema=table.schema
344343
)
345-
msg = "Passing a BlockManager to DataFrame is deprecated"
346-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
347-
result = table.to_pandas()
344+
result = table.to_pandas()
348345
tm.assert_frame_equal(result, expected[0:0])
349346

350347

348+
@pytest.mark.filterwarnings(
349+
"ignore:Passing a BlockManager to DataFrame:DeprecationWarning"
350+
)
351351
@pytest.mark.parametrize(
352352
"breaks",
353353
[[0.0, 1.0, 2.0, 3.0], date_range("2017", periods=4, freq="D")],
@@ -365,9 +365,7 @@ def test_arrow_table_roundtrip_without_metadata(breaks):
365365
table = table.replace_schema_metadata()
366366
assert table.schema.metadata is None
367367

368-
msg = "Passing a BlockManager to DataFrame is deprecated"
369-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
370-
result = table.to_pandas()
368+
result = table.to_pandas()
371369
assert isinstance(result["a"].dtype, pd.IntervalDtype)
372370
tm.assert_frame_equal(result, df)
373371

pandas/tests/arrays/masked/test_arrow_compat.py

+9-15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import pandas as pd
55
import pandas._testing as tm
66

7+
pytestmark = pytest.mark.filterwarnings(
8+
"ignore:Passing a BlockManager to DataFrame:DeprecationWarning"
9+
)
10+
711
pa = pytest.importorskip("pyarrow")
812

913
from pandas.core.arrays.arrow._arrow_utils import pyarrow_array_to_numpy_and_mask
@@ -36,9 +40,7 @@ def test_arrow_roundtrip(data):
3640
table = pa.table(df)
3741
assert table.field("a").type == str(data.dtype.numpy_dtype)
3842

39-
msg = "Passing a BlockManager to DataFrame is deprecated"
40-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
41-
result = table.to_pandas()
43+
result = table.to_pandas()
4244
assert result["a"].dtype == data.dtype
4345
tm.assert_frame_equal(result, df)
4446

@@ -56,9 +58,7 @@ def types_mapper(arrow_type):
5658
record_batch = pa.RecordBatch.from_arrays(
5759
[bools_array, ints_array, small_ints_array], ["bools", "ints", "small_ints"]
5860
)
59-
msg = "Passing a BlockManager to DataFrame is deprecated"
60-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
61-
result = record_batch.to_pandas(types_mapper=types_mapper)
61+
result = record_batch.to_pandas(types_mapper=types_mapper)
6262
bools = pd.Series([True, None, False], dtype="boolean")
6363
ints = pd.Series([1, None, 2], dtype="Int64")
6464
small_ints = pd.Series([-1, 0, 7], dtype="Int64")
@@ -75,9 +75,7 @@ def test_arrow_load_from_zero_chunks(data):
7575
table = pa.table(
7676
[pa.chunked_array([], type=table.field("a").type)], schema=table.schema
7777
)
78-
msg = "Passing a BlockManager to DataFrame is deprecated"
79-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
80-
result = table.to_pandas()
78+
result = table.to_pandas()
8179
assert result["a"].dtype == data.dtype
8280
tm.assert_frame_equal(result, df)
8381

@@ -98,18 +96,14 @@ def test_arrow_sliced(data):
9896

9997
df = pd.DataFrame({"a": data})
10098
table = pa.table(df)
101-
msg = "Passing a BlockManager to DataFrame is deprecated"
102-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
103-
result = table.slice(2, None).to_pandas()
99+
result = table.slice(2, None).to_pandas()
104100
expected = df.iloc[2:].reset_index(drop=True)
105101
tm.assert_frame_equal(result, expected)
106102

107103
# no missing values
108104
df2 = df.fillna(data[0])
109105
table = pa.table(df2)
110-
msg = "Passing a BlockManager to DataFrame is deprecated"
111-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
112-
result = table.slice(2, None).to_pandas()
106+
result = table.slice(2, None).to_pandas()
113107
expected = df2.iloc[2:].reset_index(drop=True)
114108
tm.assert_frame_equal(result, expected)
115109

pandas/tests/arrays/period/test_arrow_compat.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
period_array,
1212
)
1313

14+
pytestmark = pytest.mark.filterwarnings(
15+
"ignore:Passing a BlockManager to DataFrame:DeprecationWarning"
16+
)
17+
18+
1419
pa = pytest.importorskip("pyarrow")
1520

1621

@@ -81,16 +86,12 @@ def test_arrow_table_roundtrip():
8186

8287
table = pa.table(df)
8388
assert isinstance(table.field("a").type, ArrowPeriodType)
84-
msg = "Passing a BlockManager to DataFrame is deprecated"
85-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
86-
result = table.to_pandas()
89+
result = table.to_pandas()
8790
assert isinstance(result["a"].dtype, PeriodDtype)
8891
tm.assert_frame_equal(result, df)
8992

9093
table2 = pa.concat_tables([table, table])
91-
msg = "Passing a BlockManager to DataFrame is deprecated"
92-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
93-
result = table2.to_pandas()
94+
result = table2.to_pandas()
9495
expected = pd.concat([df, df], ignore_index=True)
9596
tm.assert_frame_equal(result, expected)
9697

@@ -109,9 +110,7 @@ def test_arrow_load_from_zero_chunks():
109110
[pa.chunked_array([], type=table.column(0).type)], schema=table.schema
110111
)
111112

112-
msg = "Passing a BlockManager to DataFrame is deprecated"
113-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
114-
result = table.to_pandas()
113+
result = table.to_pandas()
115114
assert isinstance(result["a"].dtype, PeriodDtype)
116115
tm.assert_frame_equal(result, df)
117116

@@ -126,8 +125,6 @@ def test_arrow_table_roundtrip_without_metadata():
126125
table = table.replace_schema_metadata()
127126
assert table.schema.metadata is None
128127

129-
msg = "Passing a BlockManager to DataFrame is deprecated"
130-
with tm.assert_produces_warning(DeprecationWarning, match=msg):
131-
result = table.to_pandas()
128+
result = table.to_pandas()
132129
assert isinstance(result["a"].dtype, PeriodDtype)
133130
tm.assert_frame_equal(result, df)

0 commit comments

Comments
 (0)