TabularData: Fix drop_duplicates for rich_text NaN.

When filling missing values with `ui.rich_text` (i.e. in experiments show CMD), those values were not being correctly matched against `self._fill_value`)
This commit is contained in:
David de la Iglesia Castro
2021-12-29 21:33:36 +01:00
parent 51a6116b92
commit 12a737d905
2 changed files with 36 additions and 8 deletions
+3 -1
View File
@@ -260,7 +260,9 @@ class TabularData(MutableSequence[Sequence["CellT"]]):
cols_to_drop: List[str] = []
for n_col, col in enumerate(self.columns):
# Cast to str because Text is not hashable error
unique_vals = {str(x) for x in col if x != self._fill_value}
unique_vals = {
str(x) for x in col if str(x) != self._fill_value
}
if len(unique_vals) == 1:
cols_to_drop.append(self.keys()[n_col])
self.drop(*cols_to_drop)
+33 -7
View File
@@ -226,24 +226,24 @@ def test_dropna(axis, how, data, expected):
(
"rows",
[
["foo", "", ""],
["foo", "foo", ""],
["foo", "-", "-"],
["foo", "foo", "-"],
["foo", "bar", "foobar"],
],
),
("cols", [[""], ["foo"], ["foo"], ["bar"]]),
("cols", [["-"], ["foo"], ["foo"], ["bar"]]),
],
)
def test_drop_duplicates(axis, expected):
td = TabularData(["col-1", "col-2", "col-3"])
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", "-", "-"],
["foo", "foo", "-"],
["foo", "foo", "-"],
["foo", "bar", "foobar"],
]
@@ -252,6 +252,32 @@ def test_drop_duplicates(axis, expected):
assert list(td) == expected
def test_drop_duplicates_rich_text():
from dvc.ui import ui
td = TabularData(["col-1", "col-2", "col-3"], fill_value="-")
td.extend(
[
["foo", None, ui.rich_text("-")],
["foo", "foo"],
["foo", "foo"],
["foo", "bar", "foobar"],
]
)
assert list(td) == [
["foo", "-", ui.rich_text("-")],
["foo", "foo", "-"],
["foo", "foo", "-"],
["foo", "bar", "foobar"],
]
td.drop_duplicates("cols")
assert list(td) == [["-"], ["foo"], ["foo"], ["bar"]]
def test_dropna_invalid_axis():
td = TabularData(["col-1", "col-2", "col-3"])