How to use the pandas.Index function in pandas

To help you get started, we’ve selected a few pandas examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pydata / xarray / test / test_xarray.py View on Github external
def test_pandas_data(self):
        v = self.cls(['x'], pd.Series([0, 1, 2], index=[3, 2, 1]))
        self.assertXArrayEqual(v, v[[0, 1, 2]])
        v = self.cls(['x'], pd.Index([0, 1, 2]))
        self.assertEqual(v[0].data, v.data[0])
github openforcefield / open-forcefield-group / nmr / code / analyze_trajectories.py View on Github external
data["S1"] = np.nan
data["C2"] = np.nan
data["S2"] = np.nan
data["CS"] = np.nan

tau_dict = {"1UBQ":8000.0, "1BU5":110.}


for name in ["1UBQ", "1BU5"]:
    ave = lambda x: weighted_ave(x, tau=tau_dict[name])
    traj = mdtraj.load("/home/kyleb/src/tjlane/scalar-couplings/nmr_trajectories/%s_protein.dcd" % name, top="/home/kyleb/src/tjlane/scalar-couplings/nmr_trajectories/%s_protein.pdb" % name)
    rid, indices = mdtraj.geometry.atom_sequence_finder(traj, ["H","N", "CA", "HA"], residue_offsets=[0, 0, 0, 0])
    rid += 1  # Fix for mdtraj issues
    phi = mdtraj.geometry.dihedral.compute_dihedrals(traj, indices)
    indices = ["%s_%d" % (name, i) for i in rid]
    data = data.reindex(data.index.union(pd.Index(indices)))
    data.C1[indices] = ave(cos(phi + phi0))
    data.S1[indices] = ave(sin(phi + phi0))
    data.C2[indices] = ave(cos(phi + phi0) ** 2.)
    data.S2[indices] = ave(sin(phi + phi0) ** 2.)
    data.CS[indices] = ave(sin(phi + phi0) * cos(phi + phi0))


data = data.dropna(axis=0)

y, X = dmatrices('expt ~ C1 + C2', data=data, return_type='dataframe')

model = sm.OLS(y, X)
results = model.fit()
print results.summary()

yhat = results.predict()
github mouradmourafiq / pandas2sklearn / pandas_sklearn / __init__.py View on Github external
def get_columns(self, usage, columns=None):
        """
        Returns a `data_frame.columns`.
        :param usage (str): should be a value from [ALL, INCLUDE, EXCLUDE].
                            this value only makes sense if attr `columns` is also set.
                            otherwise, should be used with default value ALL.
        :param columns: * if `usage` is all, this value is not used.
                        * if `usage` is INCLUDE, the `df` is restricted to the intersection
                          between `columns` and the `df.columns`
                        * if usage is EXCLUDE, returns the `df.columns` excluding these `columns`
        :return: `data_frame` columns, excluding `target_column` and `id_column` if given.
                 `data_frame` columns, including/excluding the `columns` depending on `usage`.
        """
        columns_excluded = pd.Index([])
        columns_included = self.df.columns

        if self.has_target():
            columns_excluded = pd.Index([self.target_column])

        if self.has_id():
            columns_excluded = columns_excluded.union(pd.Index([self.id_column]))

        if usage == self.INCLUDE:
            try:
                columns_included = columns_included.intersection(pd.Index(columns))
            except TypeError:
                pass
        elif usage == self.EXCLUDE:
            try:
                columns_excluded = columns_excluded.union(pd.Index(columns))
github kvh / ramp / ramp / folds.py View on Github external
def randomize(self):
        if self.seed is not None:
            np.random.seed(self.seed)
        neg = pd.Index(np.random.permutation(self.negatives))
        pos = pd.Index(np.random.permutation(self.positives))
        return neg, pos
github enigmampc / catalyst / zipline / history / history_container.py View on Github external
def add_sids(self, to_add):
        """
        Add new sids to the container.
        """
        self.sids = pd.Index(
            sorted(self.sids.union(_ensure_index(to_add))),
        )
        self._realign_sids()
github dask / dask / dask / dataframe / accessor.py View on Github external
def maybe_wrap_pandas(obj, x):
    if isinstance(x, np.ndarray):
        if isinstance(obj, pd.Series):
            return pd.Series(x, index=obj.index, dtype=x.dtype)
        return pd.Index(x)
    return x
github PyPSA / PyPSA / pypsa / io.py View on Github external
"""

    #exportable component types
    #what about None???? - nan is float?
    allowed_types = (float,int,bool) + string_types + tuple(np.typeDict.values())

    #first export network properties
    attrs = dict((attr, getattr(network, attr))
                 for attr in dir(network)
                 if (not attr.startswith("__") and
                     isinstance(getattr(network,attr), allowed_types)))
    exporter.save_attributes(attrs)

    #now export snapshots
    snapshots = pd.DataFrame(dict(weightings=network.snapshot_weightings),
                             index=pd.Index(network.snapshots, name="name"))
    exporter.save_snapshots(snapshots)

    exported_components = []
    for component in network.all_components - {"SubNetwork"}:

        list_name = network.components[component]["list_name"]
        attrs = network.components[component]["attrs"]

        df = network.df(component)
        pnl = network.pnl(component)

        if not export_standard_types and component in network.standard_type_components:
            df = df.drop(network.components[component]["standard_types"].index)

        # first do static attributes
        df.index.name = "name"
github enigmampc / catalyst / catalyst / finance / asset_restrictions.py View on Github external
def is_restricted(self, assets, dt):
        """
        Returns whether or not an asset or iterable of assets is restricted
        on a dt.
        """
        if isinstance(assets, Asset):
            return self._is_restricted_for_asset(assets, dt)

        is_restricted = partial(self._is_restricted_for_asset, dt=dt)
        return pd.Series(
            index=pd.Index(assets),
            data=vectorize(is_restricted, otypes=[bool])(assets)
        )
github aiguofer / gspread-pandas / gspread_pandas / util.py View on Github external
def parse_sheet_headers(vals, header_rows):
    """Parse headers from a sheet into df columns."""
    col_names = None
    if header_rows:
        headers = vals[:header_rows]
        if len(headers) > 0:
            if header_rows > 1:
                _fix_sheet_header_level(headers)
                col_names = pd.MultiIndex.from_arrays(headers)
            elif header_rows == 1:
                col_names = pd.Index(headers[0])

    return col_names
github hugadams / scikit-spectra / pyuvvis / core / deprecates / timespectra_RECENTDEPRECATE.py View on Github external
def _as_interval(timespectra, unit):#, unit=None):
    ''' Return columns as intervals as computed by datetime_convert function.  Not an instance method
    for calls from objects other than self.'''
    ### If current columns is DatetimeIndex, convert
    if timespectra._interval==False:
        return Index(datetime_convert(timespectra.columns, return_as=unit, cumsum=True))#, unit=unit)              

    ### If currently already intervals, convert to datetime, then convert that to new units
    else:
        newcols=_as_datetime(timespectra)
        return Index(datetime_convert(newcols, return_as=unit, cumsum=True))#, unit=unit)