Project Name: altair-viz+altair
Language: Python
Statement Type: API

Source Code:
def sanitize_dataframe(df: pd.DataFrame) -> pd.DataFrame:  # noqa: C901
    """Sanitize a DataFrame to prepare it for serialization.

    * Make a copy
    * Convert RangeIndex columns to strings
    * Raise ValueError if column names are not strings
    * Raise ValueError if it has a hierarchical index.
    * Convert categoricals to strings.
    * Convert np.bool_ dtypes to Python bool objects
    * Convert np.int dtypes to Python int objects
    * Convert floats to objects and replace NaNs/infs with None.
    * Convert DateTime dtypes into appropriate string representations
    * Convert Nullable integers to objects and replace NaN with None
    * Convert Nullable boolean to objects and replace NaN with None
    * convert dedicated string column to objects and replace NaN with None
    * Raise a ValueError for TimeDelta dtypes
    """
    df = df.copy()

    if isinstance(df.columns, pd.RangeIndex):
        df.columns = df.columns.astype(str)

    for col_name in df.columns:
        if not isinstance(col_name, str):
            raise ValueError(
                "Dataframe contains invalid column name: {0!r}. "
                "Column names must be strings".format(col_name)
            )

    if isinstance(df.index, pd.MultiIndex):
        raise ValueError("Hierarchical indices not supported")
    if isinstance(df.columns, pd.MultiIndex):
        raise ValueError("Hierarchical indices not supported")

    def to_list_if_array(val):
        if isinstance(val, np.ndarray):
            return val.tolist()
        else:
            return val

    for dtype_item in df.dtypes.items():
        # We know that the column names are strings from the isinstance check
        # further above but mypy thinks it is of type Hashable and therefore does not
        # let us assign it to the col_name variable which is already of type str.
        col_name = cast(str, dtype_item[0])
        dtype = dtype_item[1]
        dtype_name = str(dtype)
        if dtype_name == "category":
            # Work around bug in to_json for categorical types in older versions
            # of pandas as they do not properly convert NaN values to null in to_json.
            # We can probably remove this part once we require pandas >= 1.0
            col = df[col_name].astype(object)
            df[col_name] = col.where(col.notnull(), None)
        elif dtype_name == "string":
            # dedicated string datatype (since 1.0)
            # https://pandas.pydata.org/pandas-docs/version/1.0.0/whatsnew/v1.0.0.html#dedicated-string-data-type
            col = df[col_name].astype(object)
            df[col_name] = col.where(col.notnull(), None)
        elif dtype_name == "bool":
            # convert numpy bools to objects; np.bool is not JSON serializable
            df[col_name] = df[col_name].astype(object)
        elif dtype_name == "boolean":
            # dedicated boolean datatype (since 1.0)
            # https://pandas.io/docs/user_guide/boolean.html
            col = df[col_name].astype(object)
            df[col_name] = col.where(col.notnull(), None)
        elif dtype_name.startswith("datetime") or dtype_name.startswith("timestamp"):
            # Convert datetimes to strings. This needs to be a full ISO string
            # with time, which is why we cannot use ``col.astype(str)``.
            # This is because Javascript parses date-only times in UTC, but
            # parses full ISO-8601 dates as local time, and dates in Vega and
            # Vega-Lite are displayed in local time by default.
            # (see https://github.com/altair-viz/altair/issues/1027)
            df[col_name] = (
                df[col_name].apply(lambda x: x.isoformat()).replace("NaT", "")
            )
        elif dtype_name.startswith("timedelta"):
            raise ValueError(
                'Field "{col_name}" has type "{dtype}" which is '
                "not supported by Altair. Please convert to "
                "either a timestamp or a numerical value."
                "".format(col_name=col_name, dtype=dtype)
            )
        elif dtype_name.startswith("geometry"):
            # geopandas >=0.6.1 uses the dtype geometry. Continue here
            # otherwise it will give an error on np.issubdtype(dtype, np.integer)
            continue
        elif (
            dtype_name
            in {
                "Int8",
                "Int16",
                "Int32",
                "Int64",
                "UInt8",
                "UInt16",
                "UInt32",
                "UInt64",
                "Float32",
                "Float64",
            }
        ):  # nullable integer datatypes (since 24.0) and nullable float datatypes (since 1.2.0)
            # https://pandas.pydata.org/pandas-docs/version/0.25/whatsnew/v0.24.0.html#optional-integer-na-support
            col = df[col_name].astype(object)
            df[col_name] = col.where(col.notnull(), None)
        elif numpy_is_subtype(dtype, np.integer):
            # convert integers to objects; np.int is not JSON serializable
            df[col_name] = df[col_name].astype(object)
        elif numpy_is_subtype(dtype, np.floating):
            # For floats, convert to Python float: np.float is not JSON serializable
            # Also convert NaN/inf values to null, as they are not JSON serializable
            col = df[col_name]
            bad_values = col.isnull() | np.isinf(col)
            df[col_name] = col.astype(object).where(~bad_values, None)
        elif dtype == object:
            # Convert numpy arrays saved as objects to lists
            # Arrays are not JSON serializable
            col = df[col_name].astype(object).apply(to_list_if_array)
            df[col_name] = col.where(col.notnull(), None)
    return df

sanitize_dataframe(df=           date  precipitation  temp_max  temp_min  wind  weather0    2012-01-01            0.0      12.8       5.0   4.7  drizzle1    2012-01-02           10.9      10.6       2.8   4.5     rain2    2012-01-03            0.8      11.7       7.2   2.3     rain3    2012-01-04           20.3      12.2       5.6   4.7     rain4    2012-01-05            1.3       8.9       2.8   6.1     rain...         ...            ...       ...       ...   ...      ...1456 2015-12-27            8.6       4.4       1.7   2.9      fog1457 2015-12-28            1.5       5.0       1.7   1.3      fog1458 2015-12-29            0.0       7.2       0.6   2.6      fog1459 2015-12-30            0.0       5.6      -1.0   3.4      sun1460 2015-12-31            0.0       5.6      -2.1   3.5      sun[1461 rows x 6 columns])

Selected Statement: col = df[col_name].astype(object).apply(to_list_if_array)

Function Input:
df:            date  precipitation  temp_max  temp_min  wind  weather0    2012-01-01            0.0      12.8       5.0   4.7  drizzle1    2012-01-02           10.9      10.6       2.8   4.5     rain2    2012-01-03            0.8      11.7       7.2   2.3     rain3    2012-01-04           20.3      12.2       5.6   4.7     rain4    2012-01-05            1.3       8.9       2.8   6.1     rain...         ...            ...       ...       ...   ...      ...1456 2015-12-27            8.6       4.4       1.7   2.9      fog1457 2015-12-28            1.5       5.0       1.7   1.3      fog1458 2015-12-29            0.0       7.2       0.6   2.6      fog1459 2015-12-30            0.0       5.6      -1.0   3.4      sun1460 2015-12-31            0.0       5.6      -2.1   3.5      sun[1461 rows x 6 columns]

Variable Values Before Statement:
to_list_if_array: <function sanitize_dataframe.<locals>.to_list_if_array at 0x7fc2dcd75700>

Value After Statement Execution: 0       drizzle1          rain2          rain3          rain4          rain         ...   1456        fog1457        fog1458        fog1459        sun1460        sunName: weather, Length: 1461, dtype: object

Variable States During Runtime (line, value):
df: 1
           date  precipitation  temp_max  temp_min  wind  weather0    2012-01-01            0.0      12.8       5.0   4.7  drizzle1    2012-01-02           10.9      10.6       2.8   4.5     rain2    2012-01-03            0.8      11.7       7.2   2.3     rain3    2012-01-04           20.3      12.2       5.6   4.7     rain4    2012-01-05            1.3       8.9       2.8   6.1     rain...         ...            ...       ...       ...   ...      ...1456 2015-12-27            8.6       4.4       1.7   2.9      fog1457 2015-12-28            1.5       5.0       1.7   1.3      fog1458 2015-12-29            0.0       7.2       0.6   2.6      fog1459 2015-12-30            0.0       5.6      -1.0   3.4      sun1460 2015-12-31            0.0       5.6      -2.1   3.5      sun[1461 rows x 6 columns]
74
                     date  precipitation  temp_max  temp_min  wind  weather0     2012-01-01T00:00:00            0.0      12.8       5.0   4.7  drizzle1     2012-01-02T00:00:00           10.9      10.6       2.8   4.5     rain2     2012-01-03T00:00:00            0.8      11.7       7.2   2.3     rain3     2012-01-04T00:00:00           20.3      12.2       5.6   4.7     rain4     2012-01-05T00:00:00            1.3       8.9       2.8   6.1     rain...                   ...            ...       ...       ...   ...      ...1456  2015-12-27T00:00:00            8.6       4.4       1.7   2.9      fog1457  2015-12-28T00:00:00            1.5       5.0       1.7   1.3      fog1458  2015-12-29T00:00:00            0.0       7.2       0.6   2.6      fog1459  2015-12-30T00:00:00            0.0       5.6      -1.0   3.4      sun1460  2015-12-31T00:00:00            0.0       5.6      -2.1   3.5      sun[1461 rows x 6 columns]
114
                     date precipitation  temp_max  temp_min  wind  weather0     2012-01-01T00:00:00           0.0      12.8       5.0   4.7  drizzle1     2012-01-02T00:00:00          10.9      10.6       2.8   4.5     rain2     2012-01-03T00:00:00           0.8      11.7       7.2   2.3     rain3     2012-01-04T00:00:00          20.3      12.2       5.6   4.7     rain4     2012-01-05T00:00:00           1.3       8.9       2.8   6.1     rain...                   ...           ...       ...       ...   ...      ...1456  2015-12-27T00:00:00           8.6       4.4       1.7   2.9      fog1457  2015-12-28T00:00:00           1.5       5.0       1.7   1.3      fog1458  2015-12-29T00:00:00           0.0       7.2       0.6   2.6      fog1459  2015-12-30T00:00:00           0.0       5.6      -1.0   3.4      sun1460  2015-12-31T00:00:00           0.0       5.6      -2.1   3.5      sun[1461 rows x 6 columns]
114
                     date precipitation temp_max  temp_min  wind  weather0     2012-01-01T00:00:00           0.0     12.8       5.0   4.7  drizzle1     2012-01-02T00:00:00          10.9     10.6       2.8   4.5     rain2     2012-01-03T00:00:00           0.8     11.7       7.2   2.3     rain3     2012-01-04T00:00:00          20.3     12.2       5.6   4.7     rain4     2012-01-05T00:00:00           1.3      8.9       2.8   6.1     rain...                   ...           ...      ...       ...   ...      ...1456  2015-12-27T00:00:00           8.6      4.4       1.7   2.9      fog1457  2015-12-28T00:00:00           1.5      5.0       1.7   1.3      fog1458  2015-12-29T00:00:00           0.0      7.2       0.6   2.6      fog1459  2015-12-30T00:00:00           0.0      5.6      -1.0   3.4      sun1460  2015-12-31T00:00:00           0.0      5.6      -2.1   3.5      sun[1461 rows x 6 columns]
114
                     date precipitation temp_max temp_min  wind  weather0     2012-01-01T00:00:00           0.0     12.8      5.0   4.7  drizzle1     2012-01-02T00:00:00          10.9     10.6      2.8   4.5     rain2     2012-01-03T00:00:00           0.8     11.7      7.2   2.3     rain3     2012-01-04T00:00:00          20.3     12.2      5.6   4.7     rain4     2012-01-05T00:00:00           1.3      8.9      2.8   6.1     rain...                   ...           ...      ...      ...   ...      ...1456  2015-12-27T00:00:00           8.6      4.4      1.7   2.9      fog1457  2015-12-28T00:00:00           1.5      5.0      1.7   1.3      fog1458  2015-12-29T00:00:00           0.0      7.2      0.6   2.6      fog1459  2015-12-30T00:00:00           0.0      5.6     -1.0   3.4      sun1460  2015-12-31T00:00:00           0.0      5.6     -2.1   3.5      sun[1461 rows x 6 columns]
114
                     date precipitation temp_max temp_min wind  weather0     2012-01-01T00:00:00           0.0     12.8      5.0  4.7  drizzle1     2012-01-02T00:00:00          10.9     10.6      2.8  4.5     rain2     2012-01-03T00:00:00           0.8     11.7      7.2  2.3     rain3     2012-01-04T00:00:00          20.3     12.2      5.6  4.7     rain4     2012-01-05T00:00:00           1.3      8.9      2.8  6.1     rain...                   ...           ...      ...      ...  ...      ...1456  2015-12-27T00:00:00           8.6      4.4      1.7  2.9      fog1457  2015-12-28T00:00:00           1.5      5.0      1.7  1.3      fog1458  2015-12-29T00:00:00           0.0      7.2      0.6  2.6      fog1459  2015-12-30T00:00:00           0.0      5.6     -1.0  3.4      sun1460  2015-12-31T00:00:00           0.0      5.6     -2.1  3.5      sun[1461 rows x 6 columns]
col_name: 23
'date'
23
'precipitation'
23
'temp_max'
23
'temp_min'
23
'wind'
23
'weather'
45
'date'
45
'precipitation'
45
'temp_max'
45
'temp_min'
45
'wind'
45
'weather'
to_list_if_array: 35
<function sanitize_dataframe.<locals>.to_list_if_array at 0x7fc2dcd75700>
dtype_item: 41
('date', dtype('<M8[ns]'))
41
('precipitation', dtype('float64'))
41
('temp_max', dtype('float64'))
41
('temp_min', dtype('float64'))
41
('wind', dtype('float64'))
41
('weather', dtype('O'))
dtype: 46
dtype('<M8[ns]')
46
dtype('float64')
46
dtype('O')
dtype_name: 47
'datetime64[ns]'
47
'float64'
47
'object'
col: 112
0        0.01       10.92        0.83       20.34        1.3        ... 1456     8.61457     1.51458     0.01459     0.01460     0.0Name: precipitation, Length: 1461, dtype: float64
112
0       12.81       10.62       11.73       12.24        8.9        ... 1456     4.41457     5.01458     7.21459     5.61460     5.6Name: temp_max, Length: 1461, dtype: float64
112
0       5.01       2.82       7.23       5.64       2.8       ... 1456    1.71457    1.71458    0.61459   -1.01460   -2.1Name: temp_min, Length: 1461, dtype: float64
112
0       4.71       4.52       2.33       4.74       6.1       ... 1456    2.91457    1.31458    2.61459    3.41460    3.5Name: wind, Length: 1461, dtype: float64
118
0       drizzle1          rain2          rain3          rain4          rain         ...   1456        fog1457        fog1458        fog1459        sun1460        sunName: weather, Length: 1461, dtype: object
bad_values: 113
0       False1       False2       False3       False4       False        ...  1456    False1457    False1458    False1459    False1460    FalseName: precipitation, Length: 1461, dtype: bool
113
0       False1       False2       False3       False4       False        ...  1456    False1457    False1458    False1459    False1460    FalseName: temp_max, Length: 1461, dtype: bool
113
0       False1       False2       False3       False4       False        ...  1456    False1457    False1458    False1459    False1460    FalseName: temp_min, Length: 1461, dtype: bool
113
0       False1       False2       False3       False4       False        ...  1456    False1457    False1458    False1459    False1460    FalseName: wind, Length: 1461, dtype: bool