mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-26 15:28:28 +01:00
TabularData: Support protected`columns.
TabularData: Add subset arg to dropna and drop_duplicates. Closes #7083
This commit is contained in:
@@ -144,12 +144,28 @@ def test_fill_value():
|
||||
|
||||
|
||||
def test_drop():
|
||||
td = TabularData(["col1", "col2", "col3"])
|
||||
td.append(["foo", "bar", "baz"])
|
||||
assert list(td) == [["foo", "bar", "baz"]]
|
||||
td = TabularData(["col1", "col2", "col3", "other"])
|
||||
td.append(["foo", "bar", "baz", "other_val"])
|
||||
assert list(td) == [["foo", "bar", "baz", "other_val"]]
|
||||
td.drop("col2")
|
||||
assert td.keys() == ["col1", "col3"]
|
||||
assert list(td) == [["foo", "baz"]]
|
||||
assert td.keys() == ["col1", "col3", "other"]
|
||||
assert list(td) == [["foo", "baz", "other_val"]]
|
||||
|
||||
|
||||
def test_protected():
|
||||
td = TabularData(["col1", "col2", "col3", "other"])
|
||||
td.append(["foo", "bar", "baz", "other_val"])
|
||||
td.protect("col1", "col2")
|
||||
|
||||
td.drop("col1", "col2", "col3", "other")
|
||||
assert td.keys() == ["col1", "col2"]
|
||||
assert list(td) == [["foo", "bar"]]
|
||||
|
||||
td.unprotect("col2")
|
||||
|
||||
td.drop("col1", "col2")
|
||||
assert td.keys() == ["col1"]
|
||||
assert list(td) == [["foo"]]
|
||||
|
||||
|
||||
def test_row_from_dict():
|
||||
@@ -220,6 +236,20 @@ def test_dropna(axis, how, data, expected):
|
||||
assert list(td) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"axis,expected",
|
||||
[
|
||||
("cols", [["foo", ""], ["foo", ""], ["foo", "foobar"]]),
|
||||
("rows", [["foo", "bar", ""], ["foo", "bar", "foobar"]]),
|
||||
],
|
||||
)
|
||||
def test_dropna_subset(axis, expected):
|
||||
td = TabularData(["col-1", "col-2", "col-3"])
|
||||
td.extend([["foo"], ["foo", "bar"], ["foo", "bar", "foobar"]])
|
||||
td.dropna(axis, subset=["col-1", "col-2"])
|
||||
assert list(td) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"axis,expected,ignore_empty",
|
||||
[
|
||||
@@ -263,29 +293,6 @@ def test_drop_duplicates(axis, expected, ignore_empty):
|
||||
assert list(td) == expected
|
||||
|
||||
|
||||
def test_drop_duplicates_ignore_empty():
|
||||
td = TabularData(["col-1", "col-2", "col-3"], fill_value="-")
|
||||
td.extend(
|
||||
[["foo"], ["foo", "foo"], ["foo", "foo"], ["foo", "bar", "foobar"]]
|
||||
)
|
||||
|
||||
assert list(td) == [
|
||||
["foo", "-", "-"],
|
||||
["foo", "foo", "-"],
|
||||
["foo", "foo", "-"],
|
||||
["foo", "bar", "foobar"],
|
||||
]
|
||||
|
||||
td.drop_duplicates("cols", ignore_empty=False)
|
||||
|
||||
assert list(td) == [
|
||||
["-", "-"],
|
||||
["foo", "-"],
|
||||
["foo", "-"],
|
||||
["bar", "foobar"],
|
||||
]
|
||||
|
||||
|
||||
def test_drop_duplicates_rich_text():
|
||||
from dvc.ui import ui
|
||||
|
||||
@@ -312,6 +319,51 @@ def test_drop_duplicates_rich_text():
|
||||
assert list(td) == [["-"], ["foo"], ["foo"], ["bar"]]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"axis,subset,expected",
|
||||
[
|
||||
(
|
||||
"rows",
|
||||
["col-1"],
|
||||
[["foo", "foo", "foo", "bar"]],
|
||||
),
|
||||
(
|
||||
"rows",
|
||||
["col-1", "col-3"],
|
||||
[
|
||||
["foo", "foo", "foo", "bar"],
|
||||
["foo", "bar", "foobar", "bar"],
|
||||
],
|
||||
),
|
||||
(
|
||||
"cols",
|
||||
["col-1", "col-3"],
|
||||
[
|
||||
["foo", "foo", "bar"],
|
||||
["bar", "foo", "bar"],
|
||||
["bar", "foobar", "bar"],
|
||||
],
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_drop_duplicates_subset(axis, subset, expected):
|
||||
td = TabularData(["col-1", "col-2", "col-3", "col-4"])
|
||||
td.extend(
|
||||
[
|
||||
["foo", "foo", "foo", "bar"],
|
||||
["foo", "bar", "foo", "bar"],
|
||||
["foo", "bar", "foobar", "bar"],
|
||||
]
|
||||
)
|
||||
assert list(td) == [
|
||||
["foo", "foo", "foo", "bar"],
|
||||
["foo", "bar", "foo", "bar"],
|
||||
["foo", "bar", "foobar", "bar"],
|
||||
]
|
||||
td.drop_duplicates(axis, subset=subset)
|
||||
assert list(td) == expected
|
||||
|
||||
|
||||
def test_dropna_invalid_axis():
|
||||
td = TabularData(["col-1", "col-2", "col-3"])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user