Python pandas.api.types.is_list_like() Examples

The following are 17 code examples of pandas.api.types.is_list_like(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pandas.api.types , or try the search function .
Example #1
Source File: tidy_verbs.py    From plydata with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(
            self,
            names_from,
            values_from,
            id_cols=None,
            names_prefix='',
            names_sep='_',
            values_fill=None,
            values_fn=mean_if_many
    ):
        def as_list_like(x):
            if x is not None and not pdtypes.is_list_like(x):
                return [x]
            return x

        self.id_cols = as_list_like(id_cols)
        self.names_from = as_list_like(names_from)
        self.values_from = as_list_like(values_from)
        self.names_prefix = names_prefix
        self.names_sep = names_sep
        self.values_fill = values_fill
        self.values_fn = values_fn 
Example #2
Source File: tidy_verbs.py    From plydata with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(
            self,
            col,
            into,
            regex=r'([A-Za-z0-9]+)',
            remove=True,
            convert=False,
    ):
        if isinstance(regex, str):
            self.regex = re.compile(regex)
        elif hasattrs(regex, ('split', 'match')):
            self.regex = regex
        else:
            raise TypeError(
                "Unknown type `{}` used to describe a regular "
                "expression.".format(type(regex))
            )
        if not pdtypes.is_list_like(into):
            into = [into]

        self.col = col
        self.into = into
        self.regex = regex
        self.remove = remove
        self.convert = convert 
Example #3
Source File: utils.py    From koalas with Apache License 2.0 6 votes vote down vote up
def name_like_string(name: Union[str, Tuple]) -> str:
    """
    Return the name-like strings from str or tuple of str

    Examples
    --------
    >>> name = 'abc'
    >>> name_like_string(name)
    'abc'

    >>> name = ('abc',)
    >>> name_like_string(name)
    'abc'

    >>> name = ('a', 'b', 'c')
    >>> name_like_string(name)
    '(a, b, c)'
    """
    if is_list_like(name):
        name = tuple([str(n) for n in name])
    else:
        name = (str(name),)
    return ("(%s)" % ", ".join(name)) if len(name) > 1 else name[0] 
Example #4
Source File: parser.py    From cyberpandas with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def to_ipaddress(values):
    """Convert values to IPArray

    Parameters
    ----------
    values : int, str, bytes, or sequence of those

    Returns
    -------
    addresses : IPArray

    Examples
    --------
    Parse strings
    >>> to_ipaddress(['192.168.1.1',
    ...               '2001:0db8:85a3:0000:0000:8a2e:0370:7334'])
    <IPArray(['192.168.1.1', '0:8a2e:370:7334:2001:db8:85a3:0'])>

    Or integers
    >>> to_ipaddress([3232235777,
                      42540766452641154071740215577757643572])
    <IPArray(['192.168.1.1', '0:8a2e:370:7334:2001:db8:85a3:0'])>

    Or packed binary representations
    >>> to_ipaddress([b'\xc0\xa8\x01\x01',
                      b' \x01\r\xb8\x85\xa3\x00\x00\x00\x00\x8a.\x03ps4'])
    <IPArray(['192.168.1.1', '0:8a2e:370:7334:2001:db8:85a3:0'])>
    """
    from . import IPArray

    if not is_list_like(values):
        values = [values]

    return IPArray(_to_ip_array(values)) 
Example #5
Source File: indexing.py    From modin with Apache License 2.0 5 votes vote down vote up
def _check_dtypes(self, locator):
        is_int = is_integer(locator)
        is_int_slice = is_integer_slice(locator)
        is_int_list = is_list_like(locator) and all(map(is_integer, locator))
        is_bool_arr = is_boolean_array(locator)

        if not any([is_int, is_int_slice, is_int_list, is_bool_arr]):
            raise ValueError(_ILOC_INT_ONLY_ERROR) 
Example #6
Source File: indexing.py    From modin with Apache License 2.0 5 votes vote down vote up
def _compute_lookup(self, row_loc, col_loc):
        if is_list_like(row_loc) and len(row_loc) == 1:
            if (
                isinstance(self.qc.index.values[0], np.datetime64)
                and type(row_loc[0]) != np.datetime64
            ):
                row_loc = [pandas.to_datetime(row_loc[0])]

        if isinstance(row_loc, slice):
            row_lookup = self.qc.index.get_indexer_for(
                self.qc.index.to_series().loc[row_loc]
            )
        elif isinstance(self.qc.index, pandas.MultiIndex):
            row_lookup = self.qc.index.get_locs(row_loc)
        elif is_boolean_array(row_loc):
            # If passed in a list of booleans, we return the index of the true values
            row_lookup = [i for i, row_val in enumerate(row_loc) if row_val]
        else:
            row_lookup = self.qc.index.get_indexer_for(row_loc)
        if isinstance(col_loc, slice):
            col_lookup = self.qc.columns.get_indexer_for(
                self.qc.columns.to_series().loc[col_loc]
            )
        elif isinstance(self.qc.columns, pandas.MultiIndex):
            col_lookup = self.qc.columns.get_locs(col_loc)
        elif is_boolean_array(col_loc):
            # If passed in a list of booleans, we return the index of the true values
            col_lookup = [i for i, col_val in enumerate(col_loc) if col_val]
        else:
            col_lookup = self.qc.columns.get_indexer_for(col_loc)
        return row_lookup, col_lookup 
Example #7
Source File: indexing.py    From modin with Apache License 2.0 5 votes vote down vote up
def is_boolean_array(x):
    return is_list_like(x) and all(map(is_bool, x)) 
Example #8
Source File: indexing.py    From modin with Apache License 2.0 5 votes vote down vote up
def is_2d(x):
    return is_list_like(x) or is_slice(x) 
Example #9
Source File: _generic.py    From kartothek with MIT License 5 votes vote down vote up
def _handle_timelike_values(array_value_type, value, value_dtype, strict_date_types):
    if is_list_like(value):
        value = [pd.Timestamp(val).to_datetime64() for val in value]
    else:
        value = pd.Timestamp(value).to_datetime64()
    value_dtype = pd.Series(value).dtype
    return value, value_dtype 
Example #10
Source File: setitem.py    From mars with Apache License 2.0 5 votes vote down vote up
def __call__(self, target: DataFrame, value):
        inputs = [target]
        if np.isscalar(value):
            value_dtype = np.array(value).dtype
        else:
            if isinstance(value, (pd.Series, SERIES_TYPE)):
                value = asseries(value)
                inputs.append(value)
                value_dtype = value.dtype
            elif is_list_like(value) or isinstance(value, TENSOR_TYPE):
                value = asseries(value, index=target.index)
                inputs.append(value)
                value_dtype = value.dtype
            else:  # pragma: no cover
                raise TypeError('Wrong value type, could be one of scalar, Series or tensor')

            if value.index_value.key != target.index_value.key:  # pragma: no cover
                raise NotImplementedError('Does not support setting value '
                                          'with different index for now')

        index_value = target.index_value
        dtypes = target.dtypes
        dtypes.loc[self._indexes] = value_dtype
        columns_value = parse_index(dtypes.index, store_data=True)
        ret = self.new_dataframe(inputs, shape=(target.shape[0], len(dtypes)),
                                 dtypes=dtypes, index_value=index_value,
                                 columns_value=columns_value)
        target.data = ret.data 
Example #11
Source File: isin.py    From mars with Apache License 2.0 5 votes vote down vote up
def isin(elements, values):
    # TODO(hetao): support more type combinations, for example, DataFrame.isin.
    if is_list_like(values):
        values = list(values)
    elif not isinstance(values, (SERIES_TYPE, TENSOR_TYPE, INDEX_TYPE)):
        raise TypeError('only list-like objects are allowed to be passed to isin(), '
                        'you passed a [{}]'.format(type(values)))
    if not isinstance(elements, SERIES_TYPE):  # pragma: no cover
        raise NotImplementedError('Unsupported parameter types: %s and %s' %
                                  (type(elements), type(values)))
    op = DataFrameIsin(values)
    return op(elements) 
Example #12
Source File: drop_duplicates.py    From mars with Apache License 2.0 5 votes vote down vote up
def _validate_subset(df, subset):
    if subset is None:
        return subset
    if not is_list_like(subset):
        subset = [subset]
    else:
        subset = list(subset)

    for s in subset:
        if s not in df.dtypes:
            raise KeyError(pd.Index([s]))

    return subset 
Example #13
Source File: indexing.py    From koalas with Apache License 2.0 4 votes vote down vote up
def __getitem__(self, key):
        if self._is_df:
            if not isinstance(key, tuple) or len(key) != 2:
                raise TypeError("Use DataFrame.at like .at[row_index, column_name]")
            row_sel, col_sel = key
        else:
            assert self._is_series, type(self._kdf_or_kser)
            if isinstance(key, tuple) and len(key) != 1:
                raise TypeError("Use Series.at like .at[row_index]")
            row_sel = key
            col_sel = self._kdf_or_kser._column_label

        if len(self._internal.index_map) == 1:
            if is_list_like(row_sel):
                raise ValueError("At based indexing on a single index can only have a single value")
            row_sel = (row_sel,)
        elif not isinstance(row_sel, tuple):
            raise ValueError("At based indexing on multi-index can only have tuple values")
        if not (
            isinstance(col_sel, str)
            or (isinstance(col_sel, tuple) and all(isinstance(col, str) for col in col_sel))
        ):
            raise ValueError("At based indexing on multi-index can only have tuple values")
        if isinstance(col_sel, str):
            col_sel = (col_sel,)

        cond = reduce(
            lambda x, y: x & y,
            [scol == row for scol, row in zip(self._internal.index_spark_columns, row_sel)],
        )
        pdf = (
            self._internal.spark_frame.drop(NATURAL_ORDER_COLUMN_NAME)
            .filter(cond)
            .select(self._internal.spark_column_for(col_sel))
            .toPandas()
        )

        if len(pdf) < 1:
            raise KeyError(name_like_string(row_sel))

        values = pdf.iloc[:, 0].values
        return (
            values
            if (len(row_sel) < len(self._internal.index_map) or len(values) > 1)
            else values[0]
        ) 
Example #14
Source File: series.py    From koalas with Apache License 2.0 4 votes vote down vote up
def clip(self, lower: Union[float, int] = None, upper: Union[float, int] = None) -> "Series":
        """
        Trim values at input threshold(s).

        Assigns values outside boundary to boundary values.

        Parameters
        ----------
        lower : float or int, default None
            Minimum threshold value. All values below this threshold will be set to it.
        upper : float or int, default None
            Maximum threshold value. All values above this threshold will be set to it.

        Returns
        -------
        Series
            Series with the values outside the clip boundaries replaced

        Examples
        --------
        >>> ks.Series([0, 2, 4]).clip(1, 3)
        0    1
        1    2
        2    3
        Name: 0, dtype: int64

        Notes
        -----
        One difference between this implementation and pandas is that running
        `pd.Series(['a', 'b']).clip(0, 1)` will crash with "TypeError: '<=' not supported between
        instances of 'str' and 'int'" while `ks.Series(['a', 'b']).clip(0, 1)` will output the
        original Series, simply ignoring the incompatible types.
        """
        if is_list_like(lower) or is_list_like(upper):
            raise ValueError(
                "List-like value are not supported for 'lower' and 'upper' at the " + "moment"
            )

        if lower is None and upper is None:
            return self

        if isinstance(self.spark.data_type, NumericType):
            scol = self.spark.column
            if lower is not None:
                scol = F.when(scol < lower, lower).otherwise(scol)
            if upper is not None:
                scol = F.when(scol > upper, upper).otherwise(scol)
            return self._with_new_scol(scol.alias(self._internal.data_spark_column_names[0]))
        else:
            return self 
Example #15
Source File: base.py    From koalas with Apache License 2.0 4 votes vote down vote up
def isin(self, values):
        """
        Check whether `values` are contained in Series.

        Return a boolean Series showing whether each element in the Series
        matches an element in the passed sequence of `values` exactly.

        Parameters
        ----------
        values : list or set
            The sequence of values to test.

        Returns
        -------
        isin : Series (bool dtype)

        Examples
        --------
        >>> s = ks.Series(['lama', 'cow', 'lama', 'beetle', 'lama',
        ...                'hippo'], name='animal')
        >>> s.isin(['cow', 'lama'])
        0     True
        1     True
        2     True
        3    False
        4     True
        5    False
        Name: animal, dtype: bool

        Passing a single string as ``s.isin('lama')`` will raise an error. Use
        a list of one element instead:

        >>> s.isin(['lama'])
        0     True
        1    False
        2     True
        3    False
        4     True
        5    False
        Name: animal, dtype: bool

        >>> s.rename("a").to_frame().set_index("a").index.isin(['lama'])
        Index([True, False, True, False, True, False], dtype='object', name='a')
        """
        if not is_list_like(values):
            raise TypeError(
                "only list-like objects are allowed to be passed"
                " to isin(), you passed a [{values_type}]".format(values_type=type(values).__name__)
            )

        return self._with_new_scol(self.spark.column.isin(list(values))).rename(self.name) 
Example #16
Source File: style.py    From vnpy_crypto with MIT License 4 votes vote down vote up
def bar(self, subset=None, axis=0, color='#d65f5f', width=100,
            align='left'):
        """
        Color the background ``color`` proptional to the values in each column.
        Excludes non-numeric data by default.

        Parameters
        ----------
        subset: IndexSlice, default None
            a valid slice for ``data`` to limit the style application to
        axis: int
        color: str or 2-tuple/list
            If a str is passed, the color is the same for both
            negative and positive numbers. If 2-tuple/list is used, the
            first element is the color_negative and the second is the
            color_positive (eg: ['#d65f5f', '#5fba7d'])
        width: float
            A number between 0 or 100. The largest value will cover ``width``
            percent of the cell's width
        align : {'left', 'zero',' mid'}, default 'left'
            - 'left' : the min value starts at the left of the cell
            - 'zero' : a value of zero is located at the center of the cell
            - 'mid' : the center of the cell is at (max-min)/2, or
              if values are all negative (positive) the zero is aligned
              at the right (left) of the cell

              .. versionadded:: 0.20.0

        Returns
        -------
        self : Styler
        """
        subset = _maybe_numeric_slice(self.data, subset)
        subset = _non_reducing_slice(subset)

        base = 'width: 10em; height: 80%;'

        if not(is_list_like(color)):
            color = [color, color]
        elif len(color) == 1:
            color = [color[0], color[0]]
        elif len(color) > 2:
            msg = ("Must pass `color` as string or a list-like"
                   " of length 2: [`color_negative`, `color_positive`]\n"
                   "(eg: color=['#d65f5f', '#5fba7d'])")
            raise ValueError(msg)

        if align == 'left':
            self.apply(self._bar_left, subset=subset, axis=axis, color=color,
                       width=width, base=base)
        elif align == 'zero':
            self.apply(self._bar_center_zero, subset=subset, axis=axis,
                       color=color, width=width, base=base)
        elif align == 'mid':
            self.apply(self._bar_center_mid, subset=subset, axis=axis,
                       color=color, width=width, base=base)
        else:
            msg = ("`align` must be one of {'left', 'zero',' mid'}")
            raise ValueError(msg)

        return self 
Example #17
Source File: tidy_verbs.py    From plydata with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def __init__(
            self,
            cols,
            names_to='name',
            values_to='value',
            names_prefix=None,
            names_sep=None,
            names_pattern=None,
            convert=False
    ):
        if pdtypes.is_list_like(names_to):
            if len(names_to) > 1 and not names_sep and not names_pattern:
                raise ValueError(
                    "If you supply multiple names in `names_to` you must "
                    "also supply one of `names_sep` or `names_pattern`."
                )
        else:
            names_to = [names_to]

        if names_sep:
            self._separate_verb = separate(
                separate._column_name,
                into=names_to,
                convert=convert
            )
        elif names_pattern:
            names_pattern = re.compile(names_pattern)
            self._extract_verb = extract(
                extract._column_name,
                into=names_to,
                regex=names_pattern,
                convert=convert
            )

        if isinstance(names_prefix, str):
            pattern = re.compile(r'^' + names_prefix)
            self._prefix_patterns = {
                name: pattern
                for name in names_to
            }
        elif isinstance(names_prefix, dict):
            self._prefix_patterns = {
                name: re.compile(r'^' + p)
                for name, p in names_prefix.items()
            }
        elif names_prefix:
            raise TypeError(
                "Got {} names_prefix should be a str or a dict".format(
                    type(names_prefix))
            )

        self.cols = cols
        self.names_to = names_to
        self.values_to = values_to
        self.names_prefix = names_prefix
        self.names_sep = names_sep
        self.names_pattern = names_pattern
        self._select_verb = select.from_columns(cols)