You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2021/02/03 23:38:48 UTC

[GitHub] [superset] ktmud commented on a change in pull request #12933: fix(viz): improve dtype inference logic

ktmud commented on a change in pull request #12933:
URL: https://github.com/apache/superset/pull/12933#discussion_r569828426



##########
File path: superset/utils/core.py
##########
@@ -1401,19 +1407,48 @@ def get_column_names_from_metrics(metrics: List[Metric]) -> List[str]:
     return columns
 
 
-def serialize_pandas_dtypes(dtypes: List[np.dtype]) -> List[GenericDataType]:
-    """Serialize pandas/numpy dtypes to JavaScript types"""
-    mapping = {
-        "object": GenericDataType.STRING,
-        "category": GenericDataType.STRING,
-        "datetime64[ns]": GenericDataType.TEMPORAL,
-        "int64": GenericDataType.NUMERIC,
-        "in32": GenericDataType.NUMERIC,
-        "float64": GenericDataType.NUMERIC,
-        "float32": GenericDataType.NUMERIC,
-        "bool": GenericDataType.BOOLEAN,
+def extract_dataframe_dtypes(df: pd.DataFrame) -> List[GenericDataType]:
+    """Serialize pandas/numpy dtypes to generic types"""
+
+    def _check_dtype(series: pd.Series) -> Optional[GenericDataType]:
+        if is_numeric_dtype(series):
+            return GenericDataType.NUMERIC
+        elif is_bool_dtype(series):
+            return GenericDataType.BOOLEAN
+        elif is_datetime64_any_dtype(series):
+            return GenericDataType.TEMPORAL
+        return None
+
+    type_map: Dict[Type[Any], GenericDataType] = {
+        date: GenericDataType.TEMPORAL,
+        datetime: GenericDataType.TEMPORAL,
+        pd.Timestamp: GenericDataType.TEMPORAL,
+        pd.DatetimeTZDtype: GenericDataType.TEMPORAL,
+        str: GenericDataType.STRING,
+        int: GenericDataType.NUMERIC,
+        float: GenericDataType.NUMERIC,
+        bool: GenericDataType.BOOLEAN,
     }
-    return [mapping.get(str(x), GenericDataType.STRING) for x in dtypes]
+
+    generic_dtypes: List[GenericDataType] = []
+    for idx, column in enumerate(df.columns):
+        generic_dtype = GenericDataType.STRING
+        series = df[column]
+        if is_object_dtype(df[column]):

Review comment:
       Have you tried [`pandas.api.types.infer_dtype`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.api.types.infer_dtype.html)?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org