Added Pandas logical operations nodes.
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import pandas as pd
|
||||
|
||||
PROJECT_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
|
||||
sys.path.append(MODULE_ROOT)
|
||||
from pandas_wrapper.pandas_create_from_dict_index_list import PandasCreateFromDictIndexList
|
||||
|
||||
class TestPandasCreateFromDictIndexList(unittest.TestCase):
|
||||
def setUp(self):
|
||||
"""Set up test data."""
|
||||
self.node = PandasCreateFromDictIndexList()
|
||||
self.test_cases = [
|
||||
# Test with a standard dictionary and provided index
|
||||
(
|
||||
{"a": [1, 2, 3], "b": [4, 5, 6]},
|
||||
["row1", "row2", "row3"],
|
||||
pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}, index=["row1", "row2", "row3"])
|
||||
),
|
||||
# Test with an empty dictionary (should return an empty DataFrame)
|
||||
(
|
||||
{},
|
||||
["row1", "row2"],
|
||||
pd.DataFrame()
|
||||
),
|
||||
# Test with missing index (should use default range index)
|
||||
(
|
||||
{"a": [7, 8, 9]},
|
||||
None,
|
||||
pd.DataFrame({"a": [7, 8, 9]}, index=[0, 1, 2])
|
||||
),
|
||||
# Test with integer index values
|
||||
(
|
||||
{"x": [10, 20], "y": [30, 40]},
|
||||
[100, 101],
|
||||
pd.DataFrame({"x": [10, 20], "y": [30, 40]}, index=[100, 101])
|
||||
),
|
||||
# Test with a single column and mismatched index length (should raise an error)
|
||||
(
|
||||
{"c": [1, 2]},
|
||||
["only_one_row"],
|
||||
ValueError # Expecting an error because index length doesn't match data length
|
||||
),
|
||||
]
|
||||
|
||||
def test_create_dataframe(self):
|
||||
"""Test creating a pandas DataFrame from a dictionary with an index."""
|
||||
for data, index, expected in self.test_cases:
|
||||
with self.subTest(data=data, index=index):
|
||||
if expected is ValueError:
|
||||
with self.assertRaises(ValueError):
|
||||
self.node.f(data, index)
|
||||
else:
|
||||
retval = self.node.f(data, index)
|
||||
self.assertIsInstance(retval, tuple, "Return value should be a tuple.")
|
||||
self.assertEqual(len(retval), 1, "Return tuple should have a single DataFrame.")
|
||||
pd.testing.assert_frame_equal(retval[0], expected)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,58 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import pandas as pd
|
||||
|
||||
PROJECT_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
|
||||
sys.path.append(MODULE_ROOT)
|
||||
from pandas_wrapper.pandas_eq import PandasEq
|
||||
|
||||
class TestPandasEq(unittest.TestCase):
|
||||
def setUp(self):
|
||||
"""Set up test data."""
|
||||
self.node = PandasEq()
|
||||
self.test_cases = [
|
||||
# Test where all values are True (a == b)
|
||||
(
|
||||
pd.DataFrame({"a": [1.5, 2.5]}, index=["row1", "row2"]),
|
||||
pd.DataFrame({"a": [1.5, 2.5]}, index=["row1", "row2"]),
|
||||
pd.DataFrame({"a": [True, True]}, index=["row1", "row2"])
|
||||
),
|
||||
# Test with mixed float and integer (True for all)
|
||||
(
|
||||
pd.DataFrame({"a": [3.0, 4]}, index=[0, 1]), # Second value is an integer
|
||||
pd.DataFrame({"a": [3.0, 4]}, index=[0, 1]), # Second value is an integer
|
||||
pd.DataFrame({"a": [True, True]}, index=[0, 1])
|
||||
),
|
||||
# Test with integer column labels (True for all)
|
||||
(
|
||||
pd.DataFrame({1: [10.0, 20]}, index=[0, 1]), # Second value is an integer
|
||||
pd.DataFrame({1: [10.0, 20]}, index=[0, 1]), # Second value is an integer
|
||||
pd.DataFrame({1: [True, True]}, index=[0, 1])
|
||||
),
|
||||
# Test with a False case (a != b)
|
||||
(
|
||||
pd.DataFrame({"a": [7.0]}, index=["row1"]),
|
||||
pd.DataFrame({"a": [7.5]}, index=["row1"]),
|
||||
pd.DataFrame({"a": [False]}, index=["row1"])
|
||||
),
|
||||
# Another False case with an integer column label
|
||||
(
|
||||
pd.DataFrame({2: [9]}, index=["row1"]), # Integer value
|
||||
pd.DataFrame({2: [10]}, index=["row1"]), # Different integer value
|
||||
pd.DataFrame({2: [False]}, index=["row1"])
|
||||
),
|
||||
]
|
||||
|
||||
def test_eq_operation(self):
|
||||
"""Test equality operation between two pandas DataFrames."""
|
||||
for df_a, df_b, expected in self.test_cases:
|
||||
with self.subTest(df_a=df_a, df_b=df_b):
|
||||
retval = self.node.f(df_a, df_b)
|
||||
self.assertIsInstance(retval, tuple, "Return value should be a tuple.")
|
||||
self.assertEqual(len(retval), 1, "Return tuple should have a single DataFrame.")
|
||||
pd.testing.assert_frame_equal(retval[0], expected)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,58 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import pandas as pd
|
||||
|
||||
PROJECT_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
|
||||
sys.path.append(MODULE_ROOT)
|
||||
from pandas_wrapper.pandas_ge import PandasGe
|
||||
|
||||
class TestPandasGe(unittest.TestCase):
|
||||
def setUp(self):
|
||||
"""Set up test data."""
|
||||
self.node = PandasGe()
|
||||
self.test_cases = [
|
||||
# Test where all values are True (a >= b)
|
||||
(
|
||||
pd.DataFrame({"a": [2.0, 3.0]}, index=["row1", "row2"]),
|
||||
pd.DataFrame({"a": [1.5, 2.5]}, index=["row1", "row2"]),
|
||||
pd.DataFrame({"a": [True, True]}, index=["row1", "row2"])
|
||||
),
|
||||
# Test with equality and lt
|
||||
(
|
||||
pd.DataFrame({"a": [2.5, 4.5]}, index=[0, 1]),
|
||||
pd.DataFrame({"a": [3.0, 4.5]}, index=[0, 1]),
|
||||
pd.DataFrame({"a": [False, True]}, index=[0, 1])
|
||||
),
|
||||
# Test with integer column labels (True)
|
||||
(
|
||||
pd.DataFrame({1: [15.0, 25.0]}, index=[0, 1]),
|
||||
pd.DataFrame({1: [10.0, 20.0]}, index=[0, 1]),
|
||||
pd.DataFrame({1: [True, True]}, index=[0, 1])
|
||||
),
|
||||
# Test with a False case (a < b)
|
||||
(
|
||||
pd.DataFrame({"a": [7.0]}, index=["row1"]),
|
||||
pd.DataFrame({"a": [7.5]}, index=["row1"]),
|
||||
pd.DataFrame({"a": [False]}, index=["row1"])
|
||||
),
|
||||
# True case with an integer column label
|
||||
(
|
||||
pd.DataFrame({2: [9.5]}, index=["row1"]),
|
||||
pd.DataFrame({2: [9.5]}, index=["row1"]),
|
||||
pd.DataFrame({2: [True]}, index=["row1"])
|
||||
),
|
||||
]
|
||||
|
||||
def test_ge_operation(self):
|
||||
"""Test greater-than-or-equal operation between two pandas DataFrames."""
|
||||
for df_a, df_b, expected in self.test_cases:
|
||||
with self.subTest(df_a=df_a, df_b=df_b):
|
||||
retval = self.node.f(df_a, df_b)
|
||||
self.assertIsInstance(retval, tuple, "Return value should be a tuple.")
|
||||
self.assertEqual(len(retval), 1, "Return tuple should have a single DataFrame.")
|
||||
pd.testing.assert_frame_equal(retval[0], expected)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,58 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import pandas as pd
|
||||
|
||||
PROJECT_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
|
||||
sys.path.append(MODULE_ROOT)
|
||||
from pandas_wrapper.pandas_gt import PandasGt
|
||||
|
||||
class TestPandasGt(unittest.TestCase):
|
||||
def setUp(self):
|
||||
"""Set up test data."""
|
||||
self.node = PandasGt()
|
||||
self.test_cases = [
|
||||
# Test where all values are True
|
||||
(
|
||||
pd.DataFrame({"a": [2.5, 3.5]}, index=["row1", "row2"]),
|
||||
pd.DataFrame({"a": [2.0, 3.0]}, index=["row1", "row2"]),
|
||||
pd.DataFrame({"a": [True, True]}, index=["row1", "row2"])
|
||||
),
|
||||
# Test with mixed result
|
||||
(
|
||||
pd.DataFrame({"a": [3.6, 4.9]}, index=[0, 1]),
|
||||
pd.DataFrame({"a": [3.5, 5.0]}, index=[0, 1]),
|
||||
pd.DataFrame({"a": [True, False]}, index=[0, 1])
|
||||
),
|
||||
# Test with integer column labels
|
||||
(
|
||||
pd.DataFrame({1: [14.0, 26.0]}, index=[0, 1]),
|
||||
pd.DataFrame({1: [15.0, 25.0]}, index=[0, 1]),
|
||||
pd.DataFrame({1: [False, True]}, index=[0, 1])
|
||||
),
|
||||
# Test with a False case (a < b)
|
||||
(
|
||||
pd.DataFrame({"a": [6.0]}, index=["row1"]),
|
||||
pd.DataFrame({"a": [7.0]}, index=["row1"]),
|
||||
pd.DataFrame({"a": [False]}, index=["row1"])
|
||||
),
|
||||
# Another False case with an integer column label
|
||||
(
|
||||
pd.DataFrame({2: [7.5]}, index=["row1"]),
|
||||
pd.DataFrame({2: [8.0]}, index=["row1"]),
|
||||
pd.DataFrame({2: [False]}, index=["row1"])
|
||||
),
|
||||
]
|
||||
|
||||
def test_gt_operation(self):
|
||||
"""Test greater-than operation between two pandas DataFrames."""
|
||||
for df_a, df_b, expected in self.test_cases:
|
||||
with self.subTest(df_a=df_a, df_b=df_b):
|
||||
retval = self.node.f(df_a, df_b)
|
||||
self.assertIsInstance(retval, tuple, "Return value should be a tuple.")
|
||||
self.assertEqual(len(retval), 1, "Return tuple should have a single DataFrame.")
|
||||
pd.testing.assert_frame_equal(retval[0], expected)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,58 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import pandas as pd
|
||||
|
||||
PROJECT_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
|
||||
sys.path.append(MODULE_ROOT)
|
||||
from pandas_wrapper.pandas_le import PandasLe
|
||||
|
||||
class TestPandasLe(unittest.TestCase):
|
||||
def setUp(self):
|
||||
"""Set up test data."""
|
||||
self.node = PandasLe()
|
||||
self.test_cases = [
|
||||
# Test where all values are True
|
||||
(
|
||||
pd.DataFrame({"a": [1.5, 2.5]}, index=["row1", "row2"]),
|
||||
pd.DataFrame({"a": [2.0, 2.5]}, index=["row1", "row2"]),
|
||||
pd.DataFrame({"a": [True, True]}, index=["row1", "row2"])
|
||||
),
|
||||
# Test with integer (still True)
|
||||
(
|
||||
pd.DataFrame({"a": [3, 4]}, index=[0, 1]),
|
||||
pd.DataFrame({"a": [3, 4]}, index=[0, 1]),
|
||||
pd.DataFrame({"a": [True, True]}, index=[0, 1])
|
||||
),
|
||||
# Test with integer column labels (True)
|
||||
(
|
||||
pd.DataFrame({1: [10.0, 20.0]}, index=[0, 1]),
|
||||
pd.DataFrame({1: [15.0, 20.0]}, index=[0, 1]),
|
||||
pd.DataFrame({1: [True, True]}, index=[0, 1])
|
||||
),
|
||||
# Test with a False case (a > b)
|
||||
(
|
||||
pd.DataFrame({"a": [7.0]}, index=["row1"]),
|
||||
pd.DataFrame({"a": [6.5]}, index=["row1"]),
|
||||
pd.DataFrame({"a": [False]}, index=["row1"])
|
||||
),
|
||||
# Another False case with an integer column label
|
||||
(
|
||||
pd.DataFrame({2: [9.5]}, index=["row1"]),
|
||||
pd.DataFrame({2: [8.0]}, index=["row1"]),
|
||||
pd.DataFrame({2: [False]}, index=["row1"])
|
||||
),
|
||||
]
|
||||
|
||||
def test_le_operation(self):
|
||||
"""Test less-than-or-equal operation between two pandas DataFrames."""
|
||||
for df_a, df_b, expected in self.test_cases:
|
||||
with self.subTest(df_a=df_a, df_b=df_b):
|
||||
retval = self.node.f(df_a, df_b)
|
||||
self.assertIsInstance(retval, tuple, "Return value should be a tuple.")
|
||||
self.assertEqual(len(retval), 1, "Return tuple should have a single DataFrame.")
|
||||
pd.testing.assert_frame_equal(retval[0], expected)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,58 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import pandas as pd
|
||||
|
||||
PROJECT_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
|
||||
sys.path.append(MODULE_ROOT)
|
||||
from pandas_wrapper.pandas_lt import PandasLt
|
||||
|
||||
class TestPandasLt(unittest.TestCase):
|
||||
def setUp(self):
|
||||
"""Set up test data."""
|
||||
self.node = PandasLt()
|
||||
self.test_cases = [
|
||||
# Test where all values are True
|
||||
(
|
||||
pd.DataFrame({"a": [1.5, 2.5]}, index=["row1", "row2"]),
|
||||
pd.DataFrame({"a": [2.0, 3.0]}, index=["row1", "row2"]),
|
||||
pd.DataFrame({"a": [True, True]}, index=["row1", "row2"])
|
||||
),
|
||||
# Test with integer (still True)
|
||||
(
|
||||
pd.DataFrame({"a": [3, 40]}, index=[0, 1]),
|
||||
pd.DataFrame({"a": [4, 50]}, index=[0, 1]),
|
||||
pd.DataFrame({"a": [True, True]}, index=[0, 1])
|
||||
),
|
||||
# Test with integer column labels
|
||||
(
|
||||
pd.DataFrame({1: [10.0, 20.0]}, index=[0, 1]),
|
||||
pd.DataFrame({1: [15.0, 20.0]}, index=[0, 1]),
|
||||
pd.DataFrame({1: [True, False]}, index=[0, 1])
|
||||
),
|
||||
# Test with a False case (a >= b)
|
||||
(
|
||||
pd.DataFrame({"a": [7.0]}, index=["row1"]),
|
||||
pd.DataFrame({"a": [7.0]}, index=["row1"]),
|
||||
pd.DataFrame({"a": [False]}, index=["row1"])
|
||||
),
|
||||
# Another False case with an integer column label
|
||||
(
|
||||
pd.DataFrame({2: [9.5]}, index=["row1"]),
|
||||
pd.DataFrame({2: [8.0]}, index=["row1"]),
|
||||
pd.DataFrame({2: [False]}, index=["row1"])
|
||||
),
|
||||
]
|
||||
|
||||
def test_lt_operation(self):
|
||||
"""Test less-than operation between two pandas DataFrames."""
|
||||
for df_a, df_b, expected in self.test_cases:
|
||||
with self.subTest(df_a=df_a, df_b=df_b):
|
||||
retval = self.node.f(df_a, df_b)
|
||||
self.assertIsInstance(retval, tuple, "Return value should be a tuple.")
|
||||
self.assertEqual(len(retval), 1, "Return tuple should have a single DataFrame.")
|
||||
pd.testing.assert_frame_equal(retval[0], expected)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,58 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import pandas as pd
|
||||
|
||||
PROJECT_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
MODULE_ROOT = os.path.join(PROJECT_ROOT, "modules")
|
||||
sys.path.append(MODULE_ROOT)
|
||||
from pandas_wrapper.pandas_ne import PandasNe
|
||||
|
||||
class TestPandasNe(unittest.TestCase):
|
||||
def setUp(self):
|
||||
"""Set up test data."""
|
||||
self.node = PandasNe()
|
||||
self.test_cases = [
|
||||
# Test where all values are True (a != b)
|
||||
(
|
||||
pd.DataFrame({"a": [1.5, 2.5]}, index=["row1", "row2"]),
|
||||
pd.DataFrame({"a": [2.0, 3.0]}, index=["row1", "row2"]),
|
||||
pd.DataFrame({"a": [True, True]}, index=["row1", "row2"])
|
||||
),
|
||||
# Test with mixed float and integer (True for all)
|
||||
(
|
||||
pd.DataFrame({"a": [3.0, 4]}, index=[0, 1]), # Second value is an integer
|
||||
pd.DataFrame({"a": [3.5, 5]}, index=[0, 1]), # Second value is an integer
|
||||
pd.DataFrame({"a": [True, True]}, index=[0, 1])
|
||||
),
|
||||
# Test with integer column labels (True for all)
|
||||
(
|
||||
pd.DataFrame({1: [10.0, 20]}, index=[0, 1]), # Second value is an integer
|
||||
pd.DataFrame({1: [15.0, 25]}, index=[0, 1]), # Second value is an integer
|
||||
pd.DataFrame({1: [True, True]}, index=[0, 1])
|
||||
),
|
||||
# Test with a False case (a == b)
|
||||
(
|
||||
pd.DataFrame({"a": [7.0]}, index=["row1"]),
|
||||
pd.DataFrame({"a": [7.0]}, index=["row1"]),
|
||||
pd.DataFrame({"a": [False]}, index=["row1"])
|
||||
),
|
||||
# Another False case with an integer column label
|
||||
(
|
||||
pd.DataFrame({2: [9]}, index=["row1"]), # Integer value
|
||||
pd.DataFrame({2: [9]}, index=["row1"]), # Integer value
|
||||
pd.DataFrame({2: [False]}, index=["row1"])
|
||||
),
|
||||
]
|
||||
|
||||
def test_ne_operation(self):
|
||||
"""Test not-equal operation between two pandas DataFrames."""
|
||||
for df_a, df_b, expected in self.test_cases:
|
||||
with self.subTest(df_a=df_a, df_b=df_b):
|
||||
retval = self.node.f(df_a, df_b)
|
||||
self.assertIsInstance(retval, tuple, "Return value should be a tuple.")
|
||||
self.assertEqual(len(retval), 1, "Return tuple should have a single DataFrame.")
|
||||
pd.testing.assert_frame_equal(retval[0], expected)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user