Repository: biolab/orange3

Issue:
Pivot Table: Group by should output actual date for datetime variables
When grouping by variable in Pivot Table, it would be nice if Group By would output an actual date for datetime variables.

E.g.:
- A mean of [2020-01-01, 2020-01-02, 2020-01-03] would output 2020-01-02.
- A median of [2020-01-01, 2020-01-02, 2020-01-03, 2020-01-03, 2020-01-04] would output 2020-01-03.
- A sum ... Don't know. Probably output a float?
- Min, max ... This one is obvious.
- Variance ... I guess also a float.


Discussion:

Resolving patch:
diff --git a/Orange/widgets/data/owpivot.py b/Orange/widgets/data/owpivot.py
--- a/Orange/widgets/data/owpivot.py
+++ b/Orange/widgets/data/owpivot.py
@@ -14,7 +14,7 @@
                              QGridLayout)
 
 from Orange.data import (Table, DiscreteVariable, Variable, Domain,
-                         ContinuousVariable)
+                         ContinuousVariable, TimeVariable)
 from Orange.data.domain import filter_visible
 from Orange.data.util import get_unique_names_duplicates, get_unique_names
 from Orange.data.filter import FilterContinuous, FilterDiscrete, Values
@@ -67,6 +67,7 @@ class Pivot:
     AnyVarFunctions = (Count_defined,)
     ContVarFunctions = (Sum, Mean, Min, Max, Mode, Median, Var)
     DiscVarFunctions = (Majority,)
+    TimeVarFunctions = (Mean, Min, Max, Mode, Median)
 
     class Tables:
         table = None  # type: Table
@@ -195,7 +196,12 @@ def _create_group_tables(self, var_indep_funs, var_dep_funs):
             if fun in self.DiscVarFunctions:
                 attrs.append(DiscreteVariable(name, var.values))
             else:
-                attrs.append(ContinuousVariable(name))
+                if isinstance(var, TimeVariable) and \
+                        fun in self.TimeVarFunctions:
+                    attrs.append(TimeVariable(name, have_date=var.have_date,
+                                              have_time=var.have_time))
+                else:
+                    attrs.append(ContinuousVariable(name))
         args = (var_indep_funs, var_dep_funs, attrs)
         for t, var in (("table", None), ("total_h", self._col_var),
                        ("total_v", self._row_var), ("total", self._total_var)):


Test patch:
diff --git a/Orange/widgets/data/tests/test_owpivot.py b/Orange/widgets/data/tests/test_owpivot.py
--- a/Orange/widgets/data/tests/test_owpivot.py
+++ b/Orange/widgets/data/tests/test_owpivot.py
@@ -10,7 +10,8 @@
 from AnyQt.QtTest import QTest
 
 from Orange.data import (Table, Domain, ContinuousVariable as Cv,
-                         StringVariable as sv, DiscreteVariable as Dv)
+                         StringVariable as sv, DiscreteVariable as Dv,
+                         TimeVariable as Tv)
 from Orange.widgets.data.owpivot import (OWPivot, Pivot,
                                          AggregationFunctionsEnum)
 from Orange.widgets.tests.base import WidgetTest
@@ -68,6 +69,16 @@ def test_output_grouped_data(self):
         self.send_signal(self.widget.Inputs.data, None)
         self.assertIsNone(self.get_output(self.widget.Outputs.grouped_data))
 
+    def test_output_grouped_data_time_var(self):
+        domain = Domain([Dv("d1", ("a", "b")), Tv("t1", have_date=1)])
+        X = np.array([[0, 1e9], [0, 1e8], [1, 2e8], [1, np.nan]])
+        data = Table(domain, X)
+        self.send_signal(self.widget.Inputs.data, data)
+        self.agg_checkboxes[Pivot.Functions.Mean.value].click()
+        grouped = self.get_output(self.widget.Outputs.grouped_data)
+        str_grouped = "[[a, 2, 1987-06-06],\n [b, 2, 1976-05-03]]"
+        self.assertEqual(str(grouped), str_grouped)
+
     def test_output_filtered_data(self):
         self.agg_checkboxes[Pivot.Functions.Sum.value].click()
         self.send_signal(self.widget.Inputs.data, self.iris)
@@ -333,6 +344,18 @@ def test_group_table(self):
              [1, 2, 1, 1, 1, 1, 2, 1, 7, 7, 7, 7, 7, 7, 0]])
         self.assert_table_equal(group_tab, Table(Domain(domain[:2] + atts), X))
 
+    def test_group_table_time_var(self):
+        domain = Domain([Dv("d1", ("a", "b")), Tv("t1", have_date=1)])
+        X = np.array([[0, 1e9], [0, 1e8], [1, 2e8], [1, np.nan]])
+        table = Table(domain, X)
+        pivot = Pivot(table, Pivot.Functions, domain[0], val_var=domain[1])
+        str_grouped = \
+            "[[a, 2, 2, a, 2, 1.1e+09, 1987-06-06, 1973-03-03, " \
+            "2001-09-09, 1973-03-03, 1987-06-06, 2.025e+17],\n " \
+            "[b, 2, 2, b, 1, 2e+08, 1976-05-03, 1976-05-03, " \
+            "1976-05-03, 1976-05-03, 1976-05-03, 0]]"
+        self.assertEqual(str(pivot.group_table), str_grouped)
+
     def test_group_table_metas(self):
         domain = Domain([Dv("d1", ("a", "b")), Cv("c1"),
                          Dv("d2", ("a", "b")), Cv("c2")])