From 075f9d2802b0324157f26ac07ba829963fbde3c4 Mon Sep 17 00:00:00 2001 From: leeyspaul Date: Sun, 7 Jul 2019 16:10:25 -0700 Subject: [PATCH 1/7] suppress warnings related to msgpack and frame_methods --- pandas/core/generic.py | 15 +++++++++------ pandas/core/indexing.py | 5 ++++- pandas/io/packers.py | 36 ++++++++++++++++++++---------------- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 4e9f74162ae78..ae8672343bc6c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5596,13 +5596,16 @@ def get_dtype_counts(self): object 1 dtype: int64 """ - warnings.warn( - "`get_dtype_counts` has been deprecated and will be " - "removed in a future version. For DataFrames use " - "`.dtypes.value_counts()", - FutureWarning, - stacklevel=2, + with warnings.catch_warnings(): + warnings.simplefilter("ignore", FutureWarning) + warnings.warn( + "`get_dtype_counts` has been deprecated and will be " + "removed in a future version. For DataFrames use " + "`.dtypes.value_counts()", + FutureWarning, + stacklevel=2, ) + from pandas import Series return Series(self._data.get_dtype_counts()) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index ccc3a027af70d..846c2d25dc304 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1375,7 +1375,10 @@ class _IXIndexer(_NDFrameIndexer): ) # noqa def __init__(self, name, obj): - warnings.warn(self._ix_deprecation_warning, FutureWarning, stacklevel=2) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", FutureWarning) + warnings.warn(self._ix_deprecation_warning, FutureWarning, stacklevel=2) + super().__init__(name, obj) @Appender(_NDFrameIndexer._validate_key.__doc__) diff --git a/pandas/io/packers.py b/pandas/io/packers.py index b0ce7a4ccb12a..8675cdacd3a7c 100644 --- a/pandas/io/packers.py +++ b/pandas/io/packers.py @@ -116,14 +116,16 @@ def to_msgpack(path_or_buf, *args, **kwargs): compress : type of compressor (zlib or blosc), default to None (no compression) """ - warnings.warn( - "to_msgpack is deprecated and will be removed in a " - "future version.\n" - "It is recommended to use pyarrow for on-the-wire " - "transmission of pandas objects.", - FutureWarning, - stacklevel=3, - ) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", FutureWarning) + warnings.warn( + "to_msgpack is deprecated and will be removed in a " + "future version.\n" + "It is recommended to use pyarrow for on-the-wire " + "transmission of pandas objects.", + FutureWarning, + stacklevel=3, + ) global compressor compressor = kwargs.pop("compress", None) @@ -176,14 +178,16 @@ def read_msgpack(path_or_buf, encoding="utf-8", iterator=False, **kwargs): read_msgpack is only guaranteed to be backwards compatible to pandas 0.20.3. """ - warnings.warn( - "The read_msgpack is deprecated and will be removed in a " - "future version.\n" - "It is recommended to use pyarrow for on-the-wire " - "transmission of pandas objects.", - FutureWarning, - stacklevel=3, - ) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", FutureWarning) + warnings.warn( + "The read_msgpack is deprecated and will be removed in a " + "future version.\n" + "It is recommended to use pyarrow for on-the-wire " + "transmission of pandas objects.", + FutureWarning, + stacklevel=3, + ) path_or_buf, _, _, should_close = get_filepath_or_buffer(path_or_buf) if iterator: From b29c06a3e7e5599f4f605917a77b05b589a5b622 Mon Sep 17 00:00:00 2001 From: leeyspaul Date: Mon, 8 Jul 2019 20:34:37 -0700 Subject: [PATCH 2/7] black reformatting --- pandas/core/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index ae8672343bc6c..64937291b04c7 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5604,7 +5604,7 @@ def get_dtype_counts(self): "`.dtypes.value_counts()", FutureWarning, stacklevel=2, - ) + ) from pandas import Series From c89db5e8386d78a9b1578afc39d60e29c47bd07f Mon Sep 17 00:00:00 2001 From: leeyspaul Date: Mon, 8 Jul 2019 22:06:16 -0700 Subject: [PATCH 3/7] change tests to expect suppression --- pandas/tests/generic/test_generic.py | 3 ++- pandas/tests/indexing/test_ix.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py index aef6c3fe8070c..c1bd68c02c725 100644 --- a/pandas/tests/generic/test_generic.py +++ b/pandas/tests/generic/test_generic.py @@ -1,4 +1,5 @@ from copy import copy, deepcopy +from warnings import catch_warnings import numpy as np import pytest @@ -944,5 +945,5 @@ def test_deprecated_to_dense(self): def test_deprecated_get_dtype_counts(self): # GH 18262 df = DataFrame([1]) - with tm.assert_produces_warning(FutureWarning): + with catch_warnings(record=True): df.get_dtype_counts() diff --git a/pandas/tests/indexing/test_ix.py b/pandas/tests/indexing/test_ix.py index ee62c91ad9698..bed30044f7729 100644 --- a/pandas/tests/indexing/test_ix.py +++ b/pandas/tests/indexing/test_ix.py @@ -16,7 +16,7 @@ def test_ix_deprecation(): # GH 15114 df = DataFrame({"A": [1, 2, 3]}) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=True): + with catch_warnings(record=True): df.ix[1, "A"] From 79c6ea02022f6261e75448b7637ec9b60df8b23d Mon Sep 17 00:00:00 2001 From: leeyspaul Date: Tue, 9 Jul 2019 07:33:45 -0700 Subject: [PATCH 4/7] remove suppressed warnings in wrong locations --- pandas/core/generic.py | 16 +++++++--------- pandas/core/indexing.py | 5 +---- pandas/io/packers.py | 36 ++++++++++++++++-------------------- 3 files changed, 24 insertions(+), 33 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 64937291b04c7..37104f1cb8f67 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5596,15 +5596,13 @@ def get_dtype_counts(self): object 1 dtype: int64 """ - with warnings.catch_warnings(): - warnings.simplefilter("ignore", FutureWarning) - warnings.warn( - "`get_dtype_counts` has been deprecated and will be " - "removed in a future version. For DataFrames use " - "`.dtypes.value_counts()", - FutureWarning, - stacklevel=2, - ) + warnings.warn( + "`get_dtype_counts` has been deprecated and will be " + "removed in a future version. For DataFrames use " + "`.dtypes.value_counts()", + FutureWarning, + stacklevel=2, + ) from pandas import Series diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 846c2d25dc304..ccc3a027af70d 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1375,10 +1375,7 @@ class _IXIndexer(_NDFrameIndexer): ) # noqa def __init__(self, name, obj): - with warnings.catch_warnings(): - warnings.simplefilter("ignore", FutureWarning) - warnings.warn(self._ix_deprecation_warning, FutureWarning, stacklevel=2) - + warnings.warn(self._ix_deprecation_warning, FutureWarning, stacklevel=2) super().__init__(name, obj) @Appender(_NDFrameIndexer._validate_key.__doc__) diff --git a/pandas/io/packers.py b/pandas/io/packers.py index 8675cdacd3a7c..b0ce7a4ccb12a 100644 --- a/pandas/io/packers.py +++ b/pandas/io/packers.py @@ -116,16 +116,14 @@ def to_msgpack(path_or_buf, *args, **kwargs): compress : type of compressor (zlib or blosc), default to None (no compression) """ - with warnings.catch_warnings(): - warnings.simplefilter("ignore", FutureWarning) - warnings.warn( - "to_msgpack is deprecated and will be removed in a " - "future version.\n" - "It is recommended to use pyarrow for on-the-wire " - "transmission of pandas objects.", - FutureWarning, - stacklevel=3, - ) + warnings.warn( + "to_msgpack is deprecated and will be removed in a " + "future version.\n" + "It is recommended to use pyarrow for on-the-wire " + "transmission of pandas objects.", + FutureWarning, + stacklevel=3, + ) global compressor compressor = kwargs.pop("compress", None) @@ -178,16 +176,14 @@ def read_msgpack(path_or_buf, encoding="utf-8", iterator=False, **kwargs): read_msgpack is only guaranteed to be backwards compatible to pandas 0.20.3. """ - with warnings.catch_warnings(): - warnings.simplefilter("ignore", FutureWarning) - warnings.warn( - "The read_msgpack is deprecated and will be removed in a " - "future version.\n" - "It is recommended to use pyarrow for on-the-wire " - "transmission of pandas objects.", - FutureWarning, - stacklevel=3, - ) + warnings.warn( + "The read_msgpack is deprecated and will be removed in a " + "future version.\n" + "It is recommended to use pyarrow for on-the-wire " + "transmission of pandas objects.", + FutureWarning, + stacklevel=3, + ) path_or_buf, _, _, should_close = get_filepath_or_buffer(path_or_buf) if iterator: From 7c2f8d685308cfeac962a53e39e9741856ca434f Mon Sep 17 00:00:00 2001 From: leeyspaul Date: Tue, 9 Jul 2019 07:37:43 -0700 Subject: [PATCH 5/7] restore tests to original to expect warnings --- pandas/tests/generic/test_generic.py | 3 +-- pandas/tests/indexing/test_ix.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py index c1bd68c02c725..aef6c3fe8070c 100644 --- a/pandas/tests/generic/test_generic.py +++ b/pandas/tests/generic/test_generic.py @@ -1,5 +1,4 @@ from copy import copy, deepcopy -from warnings import catch_warnings import numpy as np import pytest @@ -945,5 +944,5 @@ def test_deprecated_to_dense(self): def test_deprecated_get_dtype_counts(self): # GH 18262 df = DataFrame([1]) - with catch_warnings(record=True): + with tm.assert_produces_warning(FutureWarning): df.get_dtype_counts() diff --git a/pandas/tests/indexing/test_ix.py b/pandas/tests/indexing/test_ix.py index bed30044f7729..ee62c91ad9698 100644 --- a/pandas/tests/indexing/test_ix.py +++ b/pandas/tests/indexing/test_ix.py @@ -16,7 +16,7 @@ def test_ix_deprecation(): # GH 15114 df = DataFrame({"A": [1, 2, 3]}) - with catch_warnings(record=True): + with tm.assert_produces_warning(FutureWarning, check_stacklevel=True): df.ix[1, "A"] From c58e8d6d6cef0ffac977af05f746c4f9419b3861 Mon Sep 17 00:00:00 2001 From: leeyspaul Date: Tue, 9 Jul 2019 07:55:28 -0700 Subject: [PATCH 6/7] add suppressed warnings to benchmarks --- asv_bench/benchmarks/frame_methods.py | 18 ++++++++------ asv_bench/benchmarks/indexing.py | 36 ++++++++++++++++----------- asv_bench/benchmarks/io/msgpack.py | 4 ++- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/asv_bench/benchmarks/frame_methods.py b/asv_bench/benchmarks/frame_methods.py index af4741f94d294..5008b77d9fb28 100644 --- a/asv_bench/benchmarks/frame_methods.py +++ b/asv_bench/benchmarks/frame_methods.py @@ -1,3 +1,4 @@ +import warnings import string import numpy as np @@ -320,9 +321,10 @@ class Dropna: def setup(self, how, axis): self.df = DataFrame(np.random.randn(10000, 1000)) - self.df.ix[50:1000, 20:50] = np.nan - self.df.ix[2000:3000] = np.nan - self.df.ix[:, 60:70] = np.nan + with warnings.catch_warnings(record=True): + self.df.ix[50:1000, 20:50] = np.nan + self.df.ix[2000:3000] = np.nan + self.df.ix[:, 60:70] = np.nan self.df_mixed = self.df.copy() self.df_mixed["foo"] = "bar" @@ -340,9 +342,10 @@ class Count: def setup(self, axis): self.df = DataFrame(np.random.randn(10000, 1000)) - self.df.ix[50:1000, 20:50] = np.nan - self.df.ix[2000:3000] = np.nan - self.df.ix[:, 60:70] = np.nan + with warnings.catch_warnings(record=True): + self.df.ix[50:1000, 20:50] = np.nan + self.df.ix[2000:3000] = np.nan + self.df.ix[:, 60:70] = np.nan self.df_mixed = self.df.copy() self.df_mixed["foo"] = "bar" @@ -561,7 +564,8 @@ def setup(self): self.df = DataFrame(np.random.randn(10, 10000)) def time_frame_get_dtype_counts(self): - self.df.get_dtype_counts() + with warnings.catch_warnings(record=True): + self.df.get_dtype_counts() def time_info(self): self.df.info() diff --git a/asv_bench/benchmarks/indexing.py b/asv_bench/benchmarks/indexing.py index 489e5c4cd63ea..d5248b276c8ea 100644 --- a/asv_bench/benchmarks/indexing.py +++ b/asv_bench/benchmarks/indexing.py @@ -131,7 +131,8 @@ class DataFrameStringIndexing: def setup(self): index = tm.makeStringIndex(1000) columns = tm.makeStringIndex(30) - self.df = DataFrame(np.random.randn(1000, 30), index=index, columns=columns) + with warnings.catch_warnings(record=True): + self.df = DataFrame(np.random.randn(1000, 30), index=index, columns=columns) self.idx_scalar = index[100] self.col_scalar = columns[10] self.bool_indexer = self.df[self.col_scalar] > 0 @@ -142,7 +143,8 @@ def time_get_value(self): self.df.get_value(self.idx_scalar, self.col_scalar) def time_ix(self): - self.df.ix[self.idx_scalar, self.col_scalar] + with warnings.catch_warnings(record=True): + self.df.ix[self.idx_scalar, self.col_scalar] def time_loc(self): self.df.loc[self.idx_scalar, self.col_scalar] @@ -206,24 +208,27 @@ def setup(self): self.df = DataFrame(self.s) n = 100000 - self.mdt = DataFrame( - { - "A": np.random.choice(range(10000, 45000, 1000), n), - "B": np.random.choice(range(10, 400), n), - "C": np.random.choice(range(1, 150), n), - "D": np.random.choice(range(10000, 45000), n), - "x": np.random.choice(range(400), n), - "y": np.random.choice(range(25), n), - } - ) + with warnings.catch_warnings(record=True): + self.mdt = DataFrame( + { + "A": np.random.choice(range(10000, 45000, 1000), n), + "B": np.random.choice(range(10, 400), n), + "C": np.random.choice(range(1, 150), n), + "D": np.random.choice(range(10000, 45000), n), + "x": np.random.choice(range(400), n), + "y": np.random.choice(range(25), n), + } + ) self.idx = IndexSlice[20000:30000, 20:30, 35:45, 30000:40000] self.mdt = self.mdt.set_index(["A", "B", "C", "D"]).sort_index() def time_series_ix(self): - self.s.ix[999] + with warnings.catch_warnings(record=True): + self.s.ix[999] def time_frame_ix(self): - self.df.ix[999] + with warnings.catch_warnings(record=True): + self.df.ix[999] def time_index_slice(self): self.mdt.loc[self.idx, :] @@ -300,7 +305,8 @@ def time_lookup_iloc(self, s): s.iloc def time_lookup_ix(self, s): - s.ix + with warnings.catch_warnings(record=True): + s.ix def time_lookup_loc(self, s): s.loc diff --git a/asv_bench/benchmarks/io/msgpack.py b/asv_bench/benchmarks/io/msgpack.py index c43df7c2e91ed..d97b4ae13f0bd 100644 --- a/asv_bench/benchmarks/io/msgpack.py +++ b/asv_bench/benchmarks/io/msgpack.py @@ -1,3 +1,4 @@ +import warnings import numpy as np from pandas import DataFrame, date_range, read_msgpack import pandas.util.testing as tm @@ -16,7 +17,8 @@ def setup(self): index=date_range("20000101", periods=N, freq="H"), ) self.df["object"] = tm.makeStringIndex(N) - self.df.to_msgpack(self.fname) + with warnings.catch_warnings(record=True): + self.df.to_msgpack(self.fname) def time_read_msgpack(self): read_msgpack(self.fname) From 39948ce26d24b1d83fefbbb26bdc8a4967bfbc73 Mon Sep 17 00:00:00 2001 From: leeyspaul Date: Tue, 9 Jul 2019 08:00:10 -0700 Subject: [PATCH 7/7] suppress more indexing warnings related to .ix deprecation warning --- asv_bench/benchmarks/indexing.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/asv_bench/benchmarks/indexing.py b/asv_bench/benchmarks/indexing.py index d5248b276c8ea..e8368f269d08a 100644 --- a/asv_bench/benchmarks/indexing.py +++ b/asv_bench/benchmarks/indexing.py @@ -66,16 +66,20 @@ def time_iloc_slice(self, index, index_structure): self.data.iloc[:800000] def time_ix_array(self, index, index_structure): - self.data.ix[self.array] + with warnings.catch_warnings(record=True): + self.data.ix[self.array] def time_ix_list_like(self, index, index_structure): - self.data.ix[[800000]] + with warnings.catch_warnings(record=True): + self.data.ix[[800000]] def time_ix_scalar(self, index, index_structure): - self.data.ix[800000] + with warnings.catch_warnings(record=True): + self.data.ix[800000] def time_ix_slice(self, index, index_structure): - self.data.ix[:800000] + with warnings.catch_warnings(record=True): + self.data.ix[:800000] def time_loc_array(self, index, index_structure): self.data.loc[self.array]