Repository: great-expectations/great_expectations

Issue:
Use cleaner solution for non-truncating division in python 2
Prefer `from __future__ import division` to `1.*x/y`


Discussion:
Done inline during improve_partitions push.

Resolving patch:
diff --git a/great_expectations/expectations/metrics/column_aggregate_metrics/column_histogram.py b/great_expectations/expectations/metrics/column_aggregate_metrics/column_histogram.py
--- a/great_expectations/expectations/metrics/column_aggregate_metrics/column_histogram.py
+++ b/great_expectations/expectations/metrics/column_aggregate_metrics/column_histogram.py
@@ -3,6 +3,7 @@
 from typing import Any, Dict
 
 import numpy as np
+import pandas as pd
 
 from great_expectations.core.util import (
     convert_to_json_serializable,
@@ -41,7 +42,10 @@ def _pandas(
         )
         column = accessor_domain_kwargs["column"]
         bins = metric_value_kwargs["bins"]
-        hist, bin_edges = np.histogram(df[column], bins, density=False)
+        column_series: pd.Series = df[column]
+        column_null_elements_cond: pd.Series = column_series.isnull()
+        column_nonnull_elements: pd.Series = column_series[~column_null_elements_cond]
+        hist, bin_edges = np.histogram(column_nonnull_elements, bins, density=False)
         return list(hist)
 
     @metric_value(engine=SqlAlchemyExecutionEngine)
diff --git a/great_expectations/expectations/metrics/column_aggregate_metrics/column_median.py b/great_expectations/expectations/metrics/column_aggregate_metrics/column_median.py
--- a/great_expectations/expectations/metrics/column_aggregate_metrics/column_median.py
+++ b/great_expectations/expectations/metrics/column_aggregate_metrics/column_median.py
@@ -27,7 +27,7 @@ class ColumnMedian(ColumnAggregateMetricProvider):
     @column_aggregate_value(engine=PandasExecutionEngine)
     def _pandas(cls, column, **kwargs):
         """Pandas Median Implementation"""
-        metrics: Dict[str, Any] = kwargs.get("metrics") or {}
+        metrics: Dict[str, Any] = kwargs.get("metrics", {})
         nonnull_count = metrics.get("column_values.nonnull.count")
         if not nonnull_count:
             return 0.0
diff --git a/great_expectations/expectations/metrics/map_metric_provider.py b/great_expectations/expectations/metrics/map_metric_provider.py
--- a/great_expectations/expectations/metrics/map_metric_provider.py
+++ b/great_expectations/expectations/metrics/map_metric_provider.py
@@ -87,10 +87,6 @@ def inner_func(
                 metrics: Dict[str, Any],
                 runtime_configuration: Dict,
             ):
-                filter_column_isnull = kwargs.get(
-                    "filter_column_isnull", getattr(cls, "filter_column_isnull", False)
-                )
-
                 (
                     df,
                     compute_domain_kwargs,
@@ -106,6 +102,9 @@ def inner_func(
                         message=f'Error: The column "{column_name}" in BatchData does not exist.'
                     )
 
+                filter_column_isnull = kwargs.get(
+                    "filter_column_isnull", getattr(cls, "filter_column_isnull", False)
+                )
                 if filter_column_isnull:
                     df = df[df[column_name].notnull()]
 
@@ -217,7 +216,6 @@ def inner_func(
                 filter_column_isnull = kwargs.get(
                     "filter_column_isnull", getattr(cls, "filter_column_isnull", False)
                 )
-
                 if filter_column_isnull:
                     compute_domain_kwargs = execution_engine.add_column_row_condition(
                         metric_domain_kwargs
@@ -309,10 +307,6 @@ def inner_func(
                 metrics: Dict[str, Any],
                 runtime_configuration: Dict,
             ):
-                filter_column_isnull = kwargs.get(
-                    "filter_column_isnull", getattr(cls, "filter_column_isnull", True)
-                )
-
                 (
                     df,
                     compute_domain_kwargs,
@@ -328,6 +322,9 @@ def inner_func(
                         message=f'Error: The column "{column_name}" in BatchData does not exist.'
                     )
 
+                filter_column_isnull = kwargs.get(
+                    "filter_column_isnull", getattr(cls, "filter_column_isnull", True)
+                )
                 if filter_column_isnull:
                     df = df[df[column_name].notnull()]
 
@@ -375,10 +372,6 @@ def inner_func(
                 metrics: Dict[str, Any],
                 runtime_configuration: Dict,
             ):
-                filter_column_isnull = kwargs.get(
-                    "filter_column_isnull", getattr(cls, "filter_column_isnull", True)
-                )
-
                 (
                     selectable,
                     compute_domain_kwargs,
@@ -410,6 +403,10 @@ def inner_func(
                     _sqlalchemy_engine=sqlalchemy_engine,
                     _metrics=metrics,
                 )
+
+                filter_column_isnull = kwargs.get(
+                    "filter_column_isnull", getattr(cls, "filter_column_isnull", True)
+                )
                 if filter_column_isnull:
                     # If we "filter" (ignore) nulls then we allow null as part of our new expected condition
                     unexpected_condition = sa.and_(
@@ -456,10 +453,6 @@ def inner_func(
                 metrics: Dict[str, Any],
                 runtime_configuration: Dict,
             ):
-                filter_column_isnull = kwargs.get(
-                    "filter_column_isnull", getattr(cls, "filter_column_isnull", True)
-                )
-
                 (
                     data,
                     compute_domain_kwargs,
@@ -485,6 +478,10 @@ def inner_func(
                     _compute_domain_kwargs=compute_domain_kwargs,
                     _accessor_domain_kwargs=accessor_domain_kwargs,
                 )
+
+                filter_column_isnull = kwargs.get(
+                    "filter_column_isnull", getattr(cls, "filter_column_isnull", True)
+                )
                 if partial_fn_type == MetricPartialFunctionTypes.WINDOW_CONDITION_FN:
                     if filter_column_isnull:
                         compute_domain_kwargs = (
@@ -1393,15 +1390,6 @@ def _pandas_column_map_condition_values(
         domain_kwargs=compute_domain_kwargs,
     )
 
-    ###
-    # NOTE: 20201111 - JPC - in the map_series / map_condition_series world (pandas), we
-    # currently handle filter_column_isnull differently than other map_fn / map_condition
-    # cases.
-    ###
-    filter_column_isnull = kwargs.get(
-        "filter_column_isnull", getattr(cls, "filter_column_isnull", False)
-    )
-
     if "column" not in accessor_domain_kwargs:
         raise ValueError(
             """No "column" found in provided metric_domain_kwargs, but it is required for a column map metric
@@ -1416,6 +1404,14 @@ def _pandas_column_map_condition_values(
             message=f'Error: The column "{column_name}" in BatchData does not exist.'
         )
 
+    ###
+    # NOTE: 20201111 - JPC - in the map_series / map_condition_series world (pandas), we
+    # currently handle filter_column_isnull differently than other map_fn / map_condition
+    # cases.
+    ###
+    filter_column_isnull = kwargs.get(
+        "filter_column_isnull", getattr(cls, "filter_column_isnull", False)
+    )
     if filter_column_isnull:
         df = df[df[column_name].notnull()]
 
@@ -1652,15 +1648,6 @@ def _pandas_column_map_series_and_domain_values(
         domain_kwargs=compute_domain_kwargs,
     )
 
-    ###
-    # NOTE: 20201111 - JPC - in the map_series / map_condition_series world (pandas), we
-    # currently handle filter_column_isnull differently than other map_fn / map_condition
-    # cases.
-    ###
-    filter_column_isnull = kwargs.get(
-        "filter_column_isnull", getattr(cls, "filter_column_isnull", False)
-    )
-
     if "column" not in accessor_domain_kwargs:
         raise ValueError(
             """No "column" found in provided metric_domain_kwargs, but it is required for a column map metric
@@ -1675,6 +1662,14 @@ def _pandas_column_map_series_and_domain_values(
             message=f'Error: The column "{column_name}" in BatchData does not exist.'
         )
 
+    ###
+    # NOTE: 20201111 - JPC - in the map_series / map_condition_series world (pandas), we
+    # currently handle filter_column_isnull differently than other map_fn / map_condition
+    # cases.
+    ###
+    filter_column_isnull = kwargs.get(
+        "filter_column_isnull", getattr(cls, "filter_column_isnull", False)
+    )
     if filter_column_isnull:
         df = df[df[column_name].notnull()]
 
@@ -1719,15 +1714,6 @@ def _pandas_map_condition_index(
         domain_kwargs=domain_kwargs,
     )
 
-    ###
-    # NOTE: 20201111 - JPC - in the map_series / map_condition_series world (pandas), we
-    # currently handle filter_column_isnull differently than other map_fn / map_condition
-    # cases.
-    ###
-    filter_column_isnull = kwargs.get(
-        "filter_column_isnull", getattr(cls, "filter_column_isnull", False)
-    )
-
     if "column" in accessor_domain_kwargs:
         column_name = accessor_domain_kwargs["column"]
 
@@ -1736,6 +1722,14 @@ def _pandas_map_condition_index(
                 message=f'Error: The column "{column_name}" in BatchData does not exist.'
             )
 
+        ###
+        # NOTE: 20201111 - JPC - in the map_series / map_condition_series world (pandas), we
+        # currently handle filter_column_isnull differently than other map_fn / map_condition
+        # cases.
+        ###
+        filter_column_isnull = kwargs.get(
+            "filter_column_isnull", getattr(cls, "filter_column_isnull", False)
+        )
         if filter_column_isnull:
             df = df[df[column_name].notnull()]
 
@@ -1776,15 +1770,6 @@ def _pandas_column_map_condition_value_counts(
         domain_kwargs=compute_domain_kwargs,
     )
 
-    ###
-    # NOTE: 20201111 - JPC - in the map_series / map_condition_series world (pandas), we
-    # currently handle filter_column_isnull differently than other map_fn / map_condition
-    # cases.
-    ###
-    filter_column_isnull = kwargs.get(
-        "filter_column_isnull", getattr(cls, "filter_column_isnull", False)
-    )
-
     column_name = accessor_domain_kwargs["column"]
 
     if "column" not in accessor_domain_kwargs:
@@ -1799,6 +1784,14 @@ def _pandas_column_map_condition_value_counts(
             message=f'Error: The column "{column_name}" in BatchData does not exist.'
         )
 
+    ###
+    # NOTE: 20201111 - JPC - in the map_series / map_condition_series world (pandas), we
+    # currently handle filter_column_isnull differently than other map_fn / map_condition
+    # cases.
+    ###
+    filter_column_isnull = kwargs.get(
+        "filter_column_isnull", getattr(cls, "filter_column_isnull", False)
+    )
     if filter_column_isnull:
         df = df[df[column_name].notnull()]
 
@@ -1850,15 +1843,6 @@ def _pandas_map_condition_rows(
         domain_kwargs=domain_kwargs,
     )
 
-    ###
-    # NOTE: 20201111 - JPC - in the map_series / map_condition_series world (pandas), we
-    # currently handle filter_column_isnull differently than other map_fn / map_condition
-    # cases.
-    ###
-    filter_column_isnull = kwargs.get(
-        "filter_column_isnull", getattr(cls, "filter_column_isnull", False)
-    )
-
     if "column" in accessor_domain_kwargs:
         column_name = accessor_domain_kwargs["column"]
 
@@ -1867,6 +1851,14 @@ def _pandas_map_condition_rows(
                 message=f'Error: The column "{column_name}" in BatchData does not exist.'
             )
 
+        ###
+        # NOTE: 20201111 - JPC - in the map_series / map_condition_series world (pandas), we
+        # currently handle filter_column_isnull differently than other map_fn / map_condition
+        # cases.
+        ###
+        filter_column_isnull = kwargs.get(
+            "filter_column_isnull", getattr(cls, "filter_column_isnull", False)
+        )
         if filter_column_isnull:
             df = df[df[column_name].notnull()]
 


Test patch: