fix: Bad date type in email text report for table chart (#20119)

* fix bad date type in email text report for table chart

* fix test, pylint issue

* add test case for date type
This commit is contained in:
Smart-Codi
2022-07-07 18:31:50 +01:00
committed by GitHub
parent 962252030b
commit e7b965a3b2
2 changed files with 74 additions and 2 deletions

View File

@@ -14,6 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import logging
import re
import urllib.request
from typing import Any, Dict, Optional
@@ -23,6 +24,10 @@ import numpy as np
import pandas as pd
import simplejson
from superset.utils.core import GenericDataType
logger = logging.getLogger(__name__)
negative_number_re = re.compile(r"^-[0-9.]+$")
# This regex will match if the string starts with:
@@ -102,11 +107,22 @@ def get_chart_dataframe(
return None
result = simplejson.loads(content.decode("utf-8"))
# need to convert float value to string to show full long number
pd.set_option("display.float_format", lambda x: str(x))
df = pd.DataFrame.from_dict(result["result"][0]["data"])
try:
# if any column type is equal to 2, need to convert data into
# datetime timestamp for that column.
if GenericDataType.TEMPORAL in result["result"][0]["coltypes"]:
for i in range(len(result["result"][0]["coltypes"])):
if result["result"][0]["coltypes"][i] == GenericDataType.TEMPORAL:
df[result["result"][0]["colnames"][i]] = df[
result["result"][0]["colnames"][i]
].astype("datetime64[ms]")
except BaseException as err:
logger.error(err)
# rebuild hierarchical columns and index
df.columns = pd.MultiIndex.from_tuples(
tuple(colname) if isinstance(colname, list) else (colname,)