Skip to content
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

WIP: Fix for pandas FutureWarning #104

Merged
merged 4 commits into from
May 17, 2024
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
13 changes: 10 additions & 3 deletions lib/charty/table_adapters/pandas_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,22 @@ 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
values = Array.try_convert(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
Expand Down Expand Up @@ -203,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
Expand Down
64 changes: 64 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,71 @@
end

module Charty
module PythonTestHelpers
def define_python_warning_check
PyCall.exec(<<PYTHON)
import contextlib
import io
import re
import sys

class WarningCheckError(Exception):
pass

class CaptureIO(io.TextIOWrapper):
def __init__(self) -> 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
Expand Down
18 changes: 11 additions & 7 deletions test/plot_methods/hist_plot_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions test/table/table_group_by_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 6 additions & 3 deletions test/table/table_reset_index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/vector/pandas_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ def test_aref
5
],
[
@vector[1],
@vector.iloc(1),
@vector["c"],
@vector["d"],
@vector[4]
@vector.iloc(4)
])
end
end
Expand Down
Loading