(df)
return Orange.data.Table.from_numpy(domain, X, None, M)
Convert a pandas.DataFrame to a Orange.data.Table instance.
Convert a pandas.DataFrame to a Orange.data.Table instance.
Convert a pandas . DataFrame to a Orange . data . Table instance .
def pandas_to_table(df):
    # type: (pd.DataFrame) -> Orange.data.Table
    """
    Convert a pandas.DataFrame to a Orange.data.Table instance.
    """
    index = df.index
    if not isinstance(index, pd.RangeIndex):
        df = df.reset_index()

    columns = []  # type: List[Tuple[Orange.data.Variable, np.ndarray]]

    for header, series in df.items():  # type: (Any, pd.Series)
        if pdtypes.is_categorical_dtype(series):
            coldata = series.values  # type: pd.Categorical
            categories = natural_sorted(str(c) for c in coldata.categories)
            var = Orange.data.DiscreteVariable.make(
                str(header), values=categories
            )
            # Remap the coldata into the var.values order/set
            coldata = pd.Categorical(
                coldata.astype("str"), categories=var.values
            )
            codes = coldata.codes
            assert np.issubdtype(codes.dtype, np.integer)
            orangecol = np.array(codes, dtype=np.float)
            orangecol[codes < 0] = np.nan
        elif pdtypes.is_datetime64_any_dtype(series):
            # Check that this converts tz local to UTC
            series = series.astype(np.dtype("M8[ns]"))
            coldata = series.values  # type: np.ndarray
            assert coldata.dtype == "M8[ns]"
            mask = np.isnat(coldata)
            orangecol = coldata.astype(np.int64) / 10 ** 9
            orangecol[mask] = np.nan
            var = Orange.data.TimeVariable.make(str(header))
            var.have_date = var.have_time = 1
        elif pdtypes.is_object_dtype(series):
            coldata = series.fillna('').values
            assert isinstance(coldata, np.ndarray)
            orangecol = coldata
            var = Orange.data.StringVariable.make(str(header))
        elif pdtypes.is_integer_dtype(series):
            coldata = series.values
            var = Orange.data.ContinuousVariable.make(str(header))
            var.number_of_decimals = 0
            orangecol = coldata.astype(np.float64)
        elif pdtypes.is_numeric_dtype(series):
            orangecol = series.values.astype(np.float64)
            var = Orange.data.ContinuousVariable.make(str(header))
        else:
            warnings.warn(
                "Column '{}' with dtype: {} skipped."
                .format(header, series.dtype),
                UserWarning
            )
            continue
        columns.append((var, orangecol))

    cols_x = [(var, col) for var, col in columns if var.is_primitive()]
    cols_m = [(var, col) for var, col in columns if not var.is_primitive()]

    variables = [v for v, _ in cols_x]
    if cols_x:
        X = np.column_stack([a for _, a in cols_x])
    else:
        X = np.empty((df.shape[0], 0), dtype=np.float)
    metas = [v for v, _ in cols_m]
    if cols_m:
        M = np.column_stack([a for _, a in cols_m])
    else:
        M = None

    domain = Orange.data.Domain(variables, metas=metas)
    return Orange.data.Table.from_numpy(domain, X, None, M)
def pandas_to_table ( df ) : # type: (pd.DataFrame) -> Orange.data.Table index = df . index if not isinstance ( index , pd . RangeIndex ) : df = df . reset_index ( ) columns = [ ] # type: List[Tuple[Orange.data.Variable, np.ndarray]] for header , series in df . items ( ) : # type: (Any, pd.Series) if pdtypes . is_categorical_dtype ( series ) : coldata = series . values # type: pd.Categorical categories = natural_sorted ( str ( c ) for c in coldata . categories ) var = Orange . data . DiscreteVariable . make ( str ( header ) , values = categories ) # Remap the coldata into the var.values order/set coldata = pd . Categorical ( coldata . astype ( "str" ) , categories = var . values ) codes = coldata . codes assert np . issubdtype ( codes . dtype , np . integer ) orangecol = np . array ( codes , dtype = np . float ) orangecol [ codes < 0 ] = np . nan elif pdtypes . is_datetime64_any_dtype ( series ) : # Check that this converts tz local to UTC series = series . astype ( np . dtype ( "M8[ns]" ) ) coldata = series . values # type: np.ndarray assert coldata . dtype == "M8[ns]" mask = np . isnat ( coldata ) orangecol = coldata . astype ( np . int64 ) / 10 ** 9 orangecol [ mask ] = np . nan var = Orange . data . TimeVariable . make ( str ( header ) ) var . have_date = var . have_time = 1 elif pdtypes . is_object_dtype ( series ) : coldata = series . fillna ( '' ) . values assert isinstance ( coldata , np . ndarray ) orangecol = coldata var = Orange . data . StringVariable . make ( str ( header ) ) elif pdtypes . is_integer_dtype ( series ) : coldata = series . values var = Orange . data . ContinuousVariable . make ( str ( header ) ) var . number_of_decimals = 0 orangecol = coldata . astype ( np . float64 ) elif pdtypes . is_numeric_dtype ( series ) : orangecol = series . values . astype ( np . float64 ) var = Orange . data . ContinuousVariable . make ( str ( header ) ) else : warnings . warn ( "Column '{}' with dtype: {} skipped." . format ( header , series . dtype ) , UserWarning ) continue columns . append ( ( var , orangecol ) ) cols_x = [ ( var , col ) for var , col in columns if var . is_primitive ( ) ] cols_m = [ ( var , col ) for var , col in columns if not var . is_primitive ( ) ] variables = [ v for v , _ in cols_x ] if cols_x : X = np . column_stack ( [ a for _ , a in cols_x ] ) else : X = np . empty ( ( df . shape [ 0 ] , 0 ) , dtype = np . float ) metas = [ v for v , _ in cols_m ] if cols_m : M = np . column_stack ( [ a for _ , a in cols_m ] ) else : M = None domain = Orange . data . Domain ( variables , metas = metas ) return Orange . data . Table . from_numpy ( domain , X , None , M )