From 0a6ccf8afdd8cc975214c467b03817100b396c7d Mon Sep 17 00:00:00 2001 From: Kenta Murata Date: Fri, 17 May 2024 12:41:28 +0900 Subject: [PATCH 1/4] Make FutureWarning error --- test/helper.rb | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/test/helper.rb b/test/helper.rb index c20b58f8..4069f6de 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -40,7 +40,71 @@ end module Charty + module PythonTestHelpers + def define_python_warning_check + PyCall.exec(< None: + super().__init__(io.BytesIO(), encoding="UTF-8", newline="", write_through=True) + + def getvalue(self) -> str: + assert isinstance(self.buffer, io.BytesIO) + return self.buffer.getvalue().decode("UTF-8") + +class WarningCheckIO(CaptureIO): + def __init__(self, pattern: str) -> None: + super().__init__() + self.pattern = pattern + + def write(self, s: str) -> int: + if re.search(self.pattern, s) is not None: + raise WarningCheckError(s) + return super().write(s) + +@contextlib.contextmanager +def warning_check(pattern: str): + old = sys.stderr + setattr(sys, "stderr", WarningCheckIO(pattern)) + try: + yield + finally: + setattr(sys, "stderr", old) +PYTHON + end + + def python_warning_check(pattern) + f = PyCall.eval("warning_check") + f.(pattern) + rescue PyCall::PyError + define_python_warning_check + python_warning_check(pattern) + end + + def check_python_warning(pattern, &block) + PyCall.with(python_warning_check(pattern), &block) + end + end + module TestHelpers + include PythonTestHelpers + + def setup + super + if pandas_available? + check_python_warning("FutureWarning") do + yield + end + end + end + module_function def arrow_available? defined?(::Arrow::Table) and Arrow::Version::MAJOR >= 6 end From 3a632ba786638cd9b032f93c6fe32598247e4be6 Mon Sep 17 00:00:00 2001 From: Kenta Murata Date: Fri, 17 May 2024 15:02:24 +0900 Subject: [PATCH 2/4] Fix ChainedAssignmentError warnings in test code --- test/plot_methods/hist_plot_test.rb | 18 +++++++++++------- test/table/table_group_by_test.rb | 4 ++-- test/table/table_reset_index_test.rb | 9 ++++++--- test/vector/pandas_test.rb | 4 ++-- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/test/plot_methods/hist_plot_test.rb b/test/plot_methods/hist_plot_test.rb index a05653c2..4ae577c3 100644 --- a/test/plot_methods/hist_plot_test.rb +++ b/test/plot_methods/hist_plot_test.rb @@ -44,10 +44,12 @@ def setup_arrow_data def setup_pandas_data pandas_required - @data = Pandas::DataFrame.new(data: @array_data) - @data[:red] = @data[:red].astype("float64") - @data[:blue] = @data[:blue].astype("float64") - @data[:green] = @data[:green].astype("float64") + data = Pandas::DataFrame.new(data: @array_data) + @data = Pandas::DataFrame.new(data: { + red: data[:red].astype("float64"), + blue: data[:blue].astype("float64"), + green: data[:green].astype("float64") + }) end end @@ -86,9 +88,11 @@ def setup_arrow_data def setup_pandas_data pandas_required - @data = Pandas::DataFrame.new(data: @array_data) - @data[:a] = @data[:a].astype("float64") - @data[:c] = @data[:c].astype("category") + data = Pandas::DataFrame.new(data: @array_data) + @data = Pandas::DataFrame.new(data: { + a: data[:a].astype("float64"), + c: data[:c].astype("category") + }) end end end diff --git a/test/table/table_group_by_test.rb b/test/table/table_group_by_test.rb index dd426373..97ac97d8 100644 --- a/test/table/table_group_by_test.rb +++ b/test/table/table_group_by_test.rb @@ -237,9 +237,9 @@ def setup_table_by_pandas @expected_applied_table.adapter.data.each do |key, value| data[key] = value.to_a end + data[:a_min] = data[:a_min].map(&:to_f) + data[:a_max] = data[:a_max].map(&:to_f) df = Pandas::DataFrame.new(data: data) - df[:a_min] = df[:a_min].astype(:float64) - df[:a_max] = df[:a_max].astype(:float64) @expected_applied_table = Charty::Table.new(df, index: @expected_applied_table.index) end diff --git a/test/table/table_reset_index_test.rb b/test/table/table_reset_index_test.rb index 8f36620f..3da11211 100644 --- a/test/table/table_reset_index_test.rb +++ b/test/table/table_reset_index_test.rb @@ -105,9 +105,12 @@ def setup_table_by_pandas @table = Charty::Table.new(Pandas::DataFrame.new(data: @data)) - df = Pandas::DataFrame.new(data: {b: @expected_indices.keys}.merge(@expected_applied_data)) - df[:a_min] = df[:a_min].astype(:float64) - df[:a_max] = df[:a_max].astype(:float64) + df = Pandas::DataFrame.new(data: { + b: @expected_indices.keys, + a: @expected_applied_data[:a], + a_min: @expected_applied_data[:a_min].map(&:to_f), + a_max: @expected_applied_data[:a_max].map(&:to_f) + }) @expected_result = Charty::Table.new(df) end end diff --git a/test/vector/pandas_test.rb b/test/vector/pandas_test.rb index 93b0bba4..5e1ede30 100644 --- a/test/vector/pandas_test.rb +++ b/test/vector/pandas_test.rb @@ -71,10 +71,10 @@ def test_aref 5 ], [ - @vector[1], + @vector.iloc(1), @vector["c"], @vector["d"], - @vector[4] + @vector.iloc(4) ]) end end From 8b6c466f205ee2f430ae7c26831571cfd0fc77c2 Mon Sep 17 00:00:00 2001 From: Kenta Murata Date: Fri, 17 May 2024 16:01:49 +0900 Subject: [PATCH 3/4] Use loc to suppress ChainAssignmentError --- lib/charty/table_adapters/pandas_adapter.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/charty/table_adapters/pandas_adapter.rb b/lib/charty/table_adapters/pandas_adapter.rb index 161fc909..238bcbb4 100644 --- a/lib/charty/table_adapters/pandas_adapter.rb +++ b/lib/charty/table_adapters/pandas_adapter.rb @@ -90,13 +90,14 @@ def [](row, column) end def []=(key, values) + all_slice = PyCall::Slice.new(nil) case values when Charty::Vector case values.adapter when Charty::VectorAdapters::PandasSeriesAdapter - @data[key] = values.adapter.data + @data.loc[all_slice, key] = values.adapter.data else - @data[key] = values.to_a + @data.loc[all_slice, key] = values.to_a end else orig_values = values @@ -104,7 +105,7 @@ def []=(key, values) if values.nil? raise ArgumentError, "`values` must be convertible to Array" end - @data[key] = values + @data.loc[all_slice, key] = values end return values end From f6009ce8ddbfb20a03219f433590f6214c4ba478 Mon Sep 17 00:00:00 2001 From: Kenta Murata Date: Fri, 17 May 2024 16:10:43 +0900 Subject: [PATCH 4/4] Fix for FutrueWarning of get_group --- lib/charty/table_adapters/pandas_adapter.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/charty/table_adapters/pandas_adapter.rb b/lib/charty/table_adapters/pandas_adapter.rb index 238bcbb4..9aea54ee 100644 --- a/lib/charty/table_adapters/pandas_adapter.rb +++ b/lib/charty/table_adapters/pandas_adapter.rb @@ -204,6 +204,12 @@ def apply(*args, &block) end def [](key) + key = case key + when PyCall::Tuple + key + else + PyCall::Tuple.new(*key.to_a) + end Charty::Table.new(@groupby.get_group(key)) end end