Exemple #1
0
 def run_binary_test(self, df, other, assert_func, test_flex=False,
                     numexpr_ops=set(['gt', 'lt', 'ge', 'le', 'eq', 'ne'])):
     """
     tests solely that the result is the same whether or not numexpr is
     enabled.  Need to test whether the function does the correct thing
     elsewhere.
     """
     expr._MIN_ELEMENTS = 0
     expr.set_test_mode(True)
     operations = ['gt', 'lt', 'ge', 'le', 'eq', 'ne']
     for arith in operations:
         if test_flex:
             op = lambda x, y: getattr(df, arith)(y)
             op.__name__ = arith
         else:
             op = getattr(operator, arith)
         expr.set_use_numexpr(False)
         expected = op(df, other)
         expr.set_use_numexpr(True)
         expr.get_test_result()
         result = op(df, other)
         used_numexpr = expr.get_test_result()
         try:
             if arith in numexpr_ops:
                 assert used_numexpr, "Did not use numexpr as expected."
             else:
                 assert not used_numexpr, "Used numexpr unexpectedly."
             assert_func(expected, result)
         except Exception:
             pprint_thing("Failed test with operation %r" % arith)
             pprint_thing("test_flex was %r" % test_flex)
             raise
Exemple #2
0
    def run_arithmetic_test(self, df, other, assert_func, check_dtype=False,
                            test_flex=True):
        expr._MIN_ELEMENTS = 0
        operations = ['add', 'sub', 'mul', 'mod', 'truediv', 'floordiv', 'pow']
        if not compat.PY3:
            operations.append('div')
        for arith in operations:
            operator_name = arith
            if arith == 'div':
                operator_name = 'truediv'

            if test_flex:
                op = lambda x, y: getattr(df, arith)(y)
                op.__name__ = arith
            else:
                op = getattr(operator, operator_name)
            expr.set_use_numexpr(False)
            expected = op(df, other)
            expr.set_use_numexpr(True)
            result = op(df, other)
            try:
                if check_dtype:
                    if arith == 'truediv':
                        assert expected.dtype.kind == 'f'
                assert_func(expected, result)
            except Exception:
                pprint_thing("Failed test with operator %r" % op.__name__)
                raise
Exemple #3
0
 def __unicode__(self):
     """Print a generic n-ary operator and its operands using infix
     notation"""
     # recurse over the operands
     parened = ('({0})'.format(pprint_thing(opr))
                for opr in self.operands)
     return pprint_thing(' {0} '.format(self.op).join(parened))
Exemple #4
0
    def run_arithmetic_test(self, df, other, assert_func, check_dtype=False,
                            test_flex=True):
        expr._MIN_ELEMENTS = 0
        operations = ['add', 'sub', 'mul', 'mod', 'truediv', 'floordiv', 'pow']
        if not compat.PY3:
            operations.append('div')
        for arith in operations:
            operator_name = arith
            if arith == 'div':
                operator_name = 'truediv'

            if test_flex:
                op = lambda x, y: getattr(df, arith)(y)
                op.__name__ = arith
            else:
                op = getattr(operator, operator_name)
            expr.set_use_numexpr(False)
            expected = op(df, other)
            expr.set_use_numexpr(True)
            result = op(df, other)
            try:
                if check_dtype:
                    if arith == 'truediv':
                        assert expected.dtype.kind == 'f'
                assert_func(expected, result)
            except Exception:
                pprint_thing("Failed test with operator %r" % op.__name__)
                raise
 def run_binary(self, df, other, assert_func, test_flex=False,
                numexpr_ops=set(['gt', 'lt', 'ge', 'le', 'eq', 'ne'])):
     """
     tests solely that the result is the same whether or not numexpr is
     enabled.  Need to test whether the function does the correct thing
     elsewhere.
     """
     expr._MIN_ELEMENTS = 0
     expr.set_test_mode(True)
     operations = ['gt', 'lt', 'ge', 'le', 'eq', 'ne']
     for arith in operations:
         if test_flex:
             op = lambda x, y: getattr(df, arith)(y)
             op.__name__ = arith
         else:
             op = getattr(operator, arith)
         expr.set_use_numexpr(False)
         expected = op(df, other)
         expr.set_use_numexpr(True)
         expr.get_test_result()
         result = op(df, other)
         used_numexpr = expr.get_test_result()
         try:
             if arith in numexpr_ops:
                 assert used_numexpr, "Did not use numexpr as expected."
             else:
                 assert not used_numexpr, "Used numexpr unexpectedly."
             assert_func(expected, result)
         except Exception:
             pprint_thing("Failed test with operation %r" % arith)
             pprint_thing("test_flex was %r" % test_flex)
             raise
Exemple #6
0
 def __unicode__(self):
     """Print a generic n-ary operator and its operands using infix
     notation"""
     # recurse over the operands
     parened = ('({0})'.format(pprint_thing(opr))
                for opr in self.operands)
     return pprint_thing(' {0} '.format(self.op).join(parened))
Exemple #7
0
 def _print(result, error=None):
     if error is not None:
         error = str(error)
     v = ("%-16.16s [%-16.16s]: [typ->%-8.8s,obj->%-8.8s,"
          "key1->(%-4.4s),key2->(%-4.4s),axis->%s] %s" %
          (name, result, t, o, method1, method2, a, error or ''))
     if _verbose:
         pprint_thing(v)
Exemple #8
0
 def _print(result, error=None):
     if error is not None:
         error = str(error)
     v = ("%-16.16s [%-16.16s]: [typ->%-8.8s,obj->%-8.8s,"
          "key1->(%-4.4s),key2->(%-4.4s),axis->%s] %s" %
          (name, result, t, o, method1, method2, a, error or ''))
     if _verbose:
         pprint_thing(v)
Exemple #9
0
    def test_copy_index_name_checking(self):
        # don't want to be able to modify the index stored elsewhere after
        # making a copy

        self.ts.index.name = None
        self.assertIsNone(self.ts.index.name)
        self.assertIs(self.ts, self.ts)

        cp = self.ts.copy()
        cp.index.name = 'foo'
        printing.pprint_thing(self.ts.index.name)
        self.assertIsNone(self.ts.index.name)
Exemple #10
0
def test_repr_binary_type():
    import string
    letters = string.ascii_letters
    btype = compat.binary_type
    try:
        raw = btype(letters, encoding=cf.get_option('display.encoding'))
    except TypeError:
        raw = btype(letters)
    b = compat.text_type(compat.bytes_to_str(raw))
    res = printing.pprint_thing(b, quote_strings=True)
    tm.assert_equal(res, repr(b))
    res = printing.pprint_thing(b, quote_strings=False)
    tm.assert_equal(res, b)
Exemple #11
0
def test_repr_binary_type():
    import string
    letters = string.ascii_letters
    btype = compat.binary_type
    try:
        raw = btype(letters, encoding=cf.get_option('display.encoding'))
    except TypeError:
        raw = btype(letters)
    b = compat.text_type(compat.bytes_to_str(raw))
    res = printing.pprint_thing(b, quote_strings=True)
    tm.assert_equal(res, repr(b))
    res = printing.pprint_thing(b, quote_strings=False)
    tm.assert_equal(res, b)
Exemple #12
0
def _replot_ax(ax, freq, kwargs):
    data = getattr(ax, '_plot_data', None)

    # clear current axes and data
    ax._plot_data = []
    ax.clear()

    _decorate_axes(ax, freq, kwargs)

    lines = []
    labels = []
    if data is not None:
        for series, plotf, kwds in data:
            series = series.copy()
            idx = series.index.asfreq(freq, how='S')
            series.index = idx
            ax._plot_data.append((series, plotf, kwds))

            # for tsplot
            if isinstance(plotf, compat.string_types):
                from pandas.tools.plotting import _plot_klass
                plotf = _plot_klass[plotf]._plot

            lines.append(plotf(ax, series.index._mpl_repr(),
                               series.values, **kwds)[0])
            labels.append(pprint_thing(series.name))

    return lines, labels
Exemple #13
0
 def check_keys_split(self, decoded):
     "checks that dict has only the appropriate keys for orient='split'"
     bad_keys = set(decoded.keys()).difference(set(self._split_keys))
     if bad_keys:
         bad_keys = ", ".join(bad_keys)
         raise ValueError(u("JSON data had unexpected key(s): %s") %
                          pprint_thing(bad_keys))
Exemple #14
0
def _replot_ax(ax, freq, kwargs):
    data = getattr(ax, '_plot_data', None)

    # clear current axes and data
    ax._plot_data = []
    ax.clear()

    _decorate_axes(ax, freq, kwargs)

    lines = []
    labels = []
    if data is not None:
        for series, plotf, kwds in data:
            series = series.copy()
            idx = series.index.asfreq(freq, how='S')
            series.index = idx
            ax._plot_data.append((series, plotf, kwds))

            # for tsplot
            if isinstance(plotf, compat.string_types):
                from pandas.tools.plotting import _plot_klass
                plotf = _plot_klass[plotf]._plot

            lines.append(plotf(ax, series.index._mpl_repr(),
                               series.values, **kwds)[0])
            labels.append(pprint_thing(series.name))

    return lines, labels
Exemple #15
0
def _convert_expression(expr):
    """Convert an object to an expression.

    Thus function converts an object to an expression (a unicode string) and
    checks to make sure it isn't empty after conversion. This is used to
    convert operators to their string representation for recursive calls to
    :func:`~pandas.eval`.

    Parameters
    ----------
    expr : object
        The object to be converted to a string.

    Returns
    -------
    s : unicode
        The string representation of an object.

    Raises
    ------
    ValueError
      * If the expression is empty.
    """
    s = pprint_thing(expr)
    _check_expression(s)
    return s
Exemple #16
0
 def check_keys_split(self, decoded):
     "checks that dict has only the appropriate keys for orient='split'"
     bad_keys = set(decoded.keys()).difference(set(self._split_keys))
     if bad_keys:
         bad_keys = ", ".join(bad_keys)
         raise ValueError(u("JSON data had unexpected key(s): %s") %
                          pprint_thing(bad_keys))
Exemple #17
0
def _convert_expression(expr):
    """Convert an object to an expression.

    Thus function converts an object to an expression (a unicode string) and
    checks to make sure it isn't empty after conversion. This is used to
    convert operators to their string representation for recursive calls to
    :func:`~pandas.eval`.

    Parameters
    ----------
    expr : object
        The object to be converted to a string.

    Returns
    -------
    s : unicode
        The string representation of an object.

    Raises
    ------
    ValueError
      * If the expression is empty.
    """
    s = pprint_thing(expr)
    _check_expression(s)
    return s
Exemple #18
0
    def __unicode__(self):
        """
        Return a string representation for this object.

        Invoked by unicode(df) in py2 only. Yields a Unicode String in both
        py2/py3.
        """
        prepr = pprint_thing(self, escape_chars=("\t", "\r", "\n"), quote_strings=True)
        return "%s(%s, dtype='%s')" % (type(self).__name__, prepr, self.dtype)
Exemple #19
0
    def __unicode__(self):
        """
        Return a string representation for this object.

        Invoked by unicode(df) in py2 only. Yields a Unicode String in both
        py2/py3.
        """
        prepr = pprint_thing(self, escape_chars=('\t', '\r', '\n'),
                             quote_strings=True)
        return "%s(%s, dtype='%s')" % (type(self).__name__, prepr, self.dtype)
Exemple #20
0
    def run_arithmetic(self,
                       df,
                       other,
                       assert_func,
                       check_dtype=False,
                       test_flex=True):
        expr._MIN_ELEMENTS = 0
        operations = ['add', 'sub', 'mul', 'mod', 'truediv', 'floordiv', 'pow']
        if not compat.PY3:
            operations.append('div')
        for arith in operations:

            # numpy >= 1.11 doesn't handle integers
            # raised to integer powers
            # https://github.com/pandas-dev/pandas/issues/15363
            if arith == 'pow' and not _np_version_under1p11:
                continue

            operator_name = arith
            if arith == 'div':
                operator_name = 'truediv'

            if test_flex:
                op = lambda x, y: getattr(df, arith)(y)
                op.__name__ = arith
            else:
                op = getattr(operator, operator_name)
            expr.set_use_numexpr(False)
            expected = op(df, other)
            expr.set_use_numexpr(True)

            result = op(df, other)
            try:
                if check_dtype:
                    if arith == 'truediv':
                        assert expected.dtype.kind == 'f'
                assert_func(expected, result)
            except Exception:
                pprint_thing("Failed test with operator %r" % op.__name__)
                raise
    def run_arithmetic(self, df, other, assert_func, check_dtype=False,
                       test_flex=True):
        expr._MIN_ELEMENTS = 0
        operations = ['add', 'sub', 'mul', 'mod', 'truediv', 'floordiv', 'pow']
        if not compat.PY3:
            operations.append('div')
        for arith in operations:

            # numpy >= 1.11 doesn't handle integers
            # raised to integer powers
            # https://github.com/pandas-dev/pandas/issues/15363
            if arith == 'pow' and not _np_version_under1p11:
                continue

            operator_name = arith
            if arith == 'div':
                operator_name = 'truediv'

            if test_flex:
                op = lambda x, y: getattr(df, arith)(y)
                op.__name__ = arith
            else:
                op = getattr(operator, operator_name)
            expr.set_use_numexpr(False)
            expected = op(df, other)
            expr.set_use_numexpr(True)

            result = op(df, other)
            try:
                if check_dtype:
                    if arith == 'truediv':
                        assert expected.dtype.kind == 'f'
                assert_func(expected, result)
            except Exception:
                pprint_thing("Failed test with operator %r" % op.__name__)
                raise
Exemple #22
0
        def writerow(self, row):
            def _check_as_is(x):
                return (self.quoting == csv.QUOTE_NONNUMERIC and
                        is_number(x)) or isinstance(x, str)

            row = [x if _check_as_is(x)
                   else pprint_thing(x).encode("utf-8") for x in row]

            self.writer.writerow([s for s in row])
            # Fetch UTF-8 output from the queue ...
            data = self.queue.getvalue()
            data = data.decode("utf-8")
            # ... and reencode it into the target encoding
            data = self.encoder.encode(data)
            # write to the target stream
            self.stream.write(data)
            # empty queue
            self.queue.truncate(0)
Exemple #23
0
        def writerow(self, row):
            def _check_as_is(x):
                return (self.quoting == csv.QUOTE_NONNUMERIC and
                        is_number(x)) or isinstance(x, str)

            row = [x if _check_as_is(x)
                   else pprint_thing(x).encode("utf-8") for x in row]

            self.writer.writerow([s for s in row])
            # Fetch UTF-8 output from the queue ...
            data = self.queue.getvalue()
            data = data.decode("utf-8")
            # ... and reencode it into the target encoding
            data = self.encoder.encode(data)
            # write to the target stream
            self.stream.write(data)
            # empty queue
            self.queue.truncate(0)
Exemple #24
0
    def summary(self, name=None):
        """
        return a summarized representation
        """
        formatter = self._formatter_func
        if len(self) > 0:
            index_summary = ", %s to %s" % (formatter(self[0]), formatter(self[-1]))
        else:
            index_summary = ""

        if name is None:
            name = type(self).__name__
        result = "%s: %s entries%s" % (printing.pprint_thing(name), len(self), index_summary)
        if self.freq:
            result += "\nFreq: %s" % self.freqstr

        # display as values, not quoted
        result = result.replace("'", "")
        return result
Exemple #25
0
    def summary(self, name=None):
        """
        return a summarized representation
        """
        formatter = self._formatter_func
        if len(self) > 0:
            index_summary = ', %s to %s' % (formatter(
                self[0]), formatter(self[-1]))
        else:
            index_summary = ''

        if name is None:
            name = type(self).__name__
        result = '%s: %s entries%s' % (printing.pprint_thing(name), len(self),
                                       index_summary)
        if self.freq:
            result += '\nFreq: %s' % self.freqstr

        # display as values, not quoted
        result = result.replace("'", "")
        return result
Exemple #26
0
 def __unicode__(self):
     operands = map(str, self.operands)
     return pprint_thing('{0}({1})'.format(self.op, ','.join(operands)))
Exemple #27
0
 def __unicode__(self):
     contents = '\n'.join(repr(c) for c in self._chunks)
     return '%s\n%s' % (object.__repr__(self), pprint_thing(contents))
Exemple #28
0
 def __unicode__(self):
     return pprint_thing('{0}({1})'.format(self.op, self.operand))
Exemple #29
0
    def convert(self):
        """Convert an expression for evaluation.

        Defaults to return the expression as a string.
        """
        return printing.pprint_thing(self.expr)
Exemple #30
0
 def __unicode__(self):
     return printing.pprint_thing(self.terms)
Exemple #31
0
    def test_arith_flex_frame(self):
        ops = ['add', 'sub', 'mul', 'div', 'truediv', 'pow', 'floordiv', 'mod']
        if not compat.PY3:
            aliases = {}
        else:
            aliases = {'div': 'truediv'}

        for op in ops:
            try:
                alias = aliases.get(op, op)
                f = getattr(operator, alias)
                result = getattr(self.frame, op)(2 * self.frame)
                exp = f(self.frame, 2 * self.frame)
                assert_frame_equal(result, exp)

                # vs mix float
                result = getattr(self.mixed_float, op)(2 * self.mixed_float)
                exp = f(self.mixed_float, 2 * self.mixed_float)
                assert_frame_equal(result, exp)
                _check_mixed_float(result, dtype=dict(C=None))

                # vs mix int
                if op in ['add', 'sub', 'mul']:
                    result = getattr(self.mixed_int, op)(2 + self.mixed_int)
                    exp = f(self.mixed_int, 2 + self.mixed_int)

                    # no overflow in the uint
                    dtype = None
                    if op in ['sub']:
                        dtype = dict(B='uint64', C=None)
                    elif op in ['add', 'mul']:
                        dtype = dict(C=None)
                    assert_frame_equal(result, exp)
                    _check_mixed_int(result, dtype=dtype)

                    # rops
                    r_f = lambda x, y: f(y, x)
                    result = getattr(self.frame, 'r' + op)(2 * self.frame)
                    exp = r_f(self.frame, 2 * self.frame)
                    assert_frame_equal(result, exp)

                    # vs mix float
                    result = getattr(self.mixed_float, op)(
                        2 * self.mixed_float)
                    exp = f(self.mixed_float, 2 * self.mixed_float)
                    assert_frame_equal(result, exp)
                    _check_mixed_float(result, dtype=dict(C=None))

                    result = getattr(self.intframe, op)(2 * self.intframe)
                    exp = f(self.intframe, 2 * self.intframe)
                    assert_frame_equal(result, exp)

                    # vs mix int
                    if op in ['add', 'sub', 'mul']:
                        result = getattr(self.mixed_int, op)(
                            2 + self.mixed_int)
                        exp = f(self.mixed_int, 2 + self.mixed_int)

                        # no overflow in the uint
                        dtype = None
                        if op in ['sub']:
                            dtype = dict(B='uint64', C=None)
                        elif op in ['add', 'mul']:
                            dtype = dict(C=None)
                        assert_frame_equal(result, exp)
                        _check_mixed_int(result, dtype=dtype)
            except:
                printing.pprint_thing("Failing operation %r" % op)
                raise

            # ndim >= 3
            ndim_5 = np.ones(self.frame.shape + (3, 4, 5))
            msg = "Unable to coerce to Series/DataFrame"
            with assertRaisesRegexp(ValueError, msg):
                f(self.frame, ndim_5)

            with assertRaisesRegexp(ValueError, msg):
                getattr(self.frame, op)(ndim_5)

        # res_add = self.frame.add(self.frame)
        # res_sub = self.frame.sub(self.frame)
        # res_mul = self.frame.mul(self.frame)
        # res_div = self.frame.div(2 * self.frame)

        # assert_frame_equal(res_add, self.frame + self.frame)
        # assert_frame_equal(res_sub, self.frame - self.frame)
        # assert_frame_equal(res_mul, self.frame * self.frame)
        # assert_frame_equal(res_div, self.frame / (2 * self.frame))

        const_add = self.frame.add(1)
        assert_frame_equal(const_add, self.frame + 1)

        # corner cases
        result = self.frame.add(self.frame[:0])
        assert_frame_equal(result, self.frame * np.nan)

        result = self.frame[:0].add(self.frame)
        assert_frame_equal(result, self.frame * np.nan)
        with assertRaisesRegexp(NotImplementedError, 'fill_value'):
            self.frame.add(self.frame.iloc[0], fill_value=3)
        with assertRaisesRegexp(NotImplementedError, 'fill_value'):
            self.frame.add(self.frame.iloc[0], axis='index', fill_value=3)
Exemple #32
0
def andrews_curves(frame,
                   class_column,
                   ax=None,
                   samples=200,
                   color=None,
                   colormap=None,
                   **kwds):
    """
    Generates a matplotlib plot of Andrews curves, for visualising clusters of
    multivariate data.

    Andrews curves have the functional form:

    f(t) = x_1/sqrt(2) + x_2 sin(t) + x_3 cos(t) +
           x_4 sin(2t) + x_5 cos(2t) + ...

    Where x coefficients correspond to the values of each dimension and t is
    linearly spaced between -pi and +pi. Each row of frame then corresponds to
    a single curve.

    Parameters:
    -----------
    frame : DataFrame
        Data to be plotted, preferably normalized to (0.0, 1.0)
    class_column : Name of the column containing class names
    ax : matplotlib axes object, default None
    samples : Number of points to plot in each curve
    color: list or tuple, optional
        Colors to use for the different classes
    colormap : str or matplotlib colormap object, default None
        Colormap to select colors from. If string, load colormap with that name
        from matplotlib.
    kwds: keywords
        Options to pass to matplotlib plotting method

    Returns:
    --------
    ax: Matplotlib axis object

    """
    from math import sqrt, pi
    import matplotlib.pyplot as plt

    def function(amplitudes):
        def f(t):
            x1 = amplitudes[0]
            result = x1 / sqrt(2.0)

            # Take the rest of the coefficients and resize them
            # appropriately. Take a copy of amplitudes as otherwise numpy
            # deletes the element from amplitudes itself.
            coeffs = np.delete(np.copy(amplitudes), 0)
            coeffs.resize(int((coeffs.size + 1) / 2), 2)

            # Generate the harmonics and arguments for the sin and cos
            # functions.
            harmonics = np.arange(0, coeffs.shape[0]) + 1
            trig_args = np.outer(harmonics, t)

            result += np.sum(coeffs[:, 0, np.newaxis] * np.sin(trig_args) +
                             coeffs[:, 1, np.newaxis] * np.cos(trig_args),
                             axis=0)
            return result

        return f

    n = len(frame)
    class_col = frame[class_column]
    classes = frame[class_column].drop_duplicates()
    df = frame.drop(class_column, axis=1)
    t = np.linspace(-pi, pi, samples)
    used_legends = set([])

    color_values = _get_standard_colors(num_colors=len(classes),
                                        colormap=colormap,
                                        color_type='random',
                                        color=color)
    colors = dict(zip(classes, color_values))
    if ax is None:
        ax = plt.gca(xlim=(-pi, pi))
    for i in range(n):
        row = df.iloc[i].values
        f = function(row)
        y = f(t)
        kls = class_col.iat[i]
        label = pprint_thing(kls)
        if label not in used_legends:
            used_legends.add(label)
            ax.plot(t, y, color=colors[kls], label=label, **kwds)
        else:
            ax.plot(t, y, color=colors[kls], **kwds)

    ax.legend(loc='upper right')
    ax.grid()
    return ax
Exemple #33
0
 def __unicode__(self):
     return '%s\nFill: %s\n%s' % (printing.pprint_thing(self),
                                  printing.pprint_thing(self.fill_value),
                                  printing.pprint_thing(self.sp_index))
Exemple #34
0
 def __unicode__(self):
     return pprint_thing("[Condition : [{0}]]".format(self.condition))
Exemple #35
0
 def raw(self):
     return pprint_thing('{0}(name={1!r}, type={2})'
                         ''.format(self.__class__.__name__, self.name,
                                   self.type))
Exemple #36
0
 def __unicode__(self):
     return pprint_thing('{0}({1})'.format(self.op, self.operand))
        with pd.option_context('display.max_rows', 9,
                            'display.max_columns', 10):
            # out vertical bounds can not result in exanded repr
            assert(not has_expanded_repr(df10))
            assert(has_vertically_truncated_repr(df10))

    # width=None in terminal, auto detection
    with pd.option_context('display.max_columns', 100, 'display.max_rows',
                        term_width * 20, 'display.width', None):
        df = mkframe((term_width // 7) - 2)
        print(term_width // 7)
        print(repr(df))
        #assert(not has_expanded_repr(df))
        df = mkframe((term_width // 7) + 2)
        print(repr(df))
        printing.pprint_thing(df._repr_fits_horizontal_())
        #assert(has_expanded_repr(df))
        print(repr(df))

print("Failure on line 481, but unsure why. I think it is expecting the df to " +
      "be too large to fit, but missing whitespace fixes that problem")
# CORRECTIONS: Unsure
print("-->  End: Test 5 test_repr_max_columns_max_rows <--")

# Test  6 test_repr_truncation
print("--> Begin: Test 6 test_repr_truncation <--")
max_len = 20
with pd.option_context("display.max_colwidth", max_len):
    df = pd.DataFrame({'A': np.random.randn(10),
                       'B': [tm.rands(np.random.randint(
                                max_len - 1, max_len + 1)) for i in range(10)
Exemple #38
0
 def __unicode__(self):
     return pprint_thing(self, quote_strings=True,
                         escape_chars=('\t', '\r', '\n'))
Exemple #39
0
    def test_arith_flex_frame(self):
        ops = ["add", "sub", "mul", "div", "truediv", "pow", "floordiv", "mod"]
        if not compat.PY3:
            aliases = {}
        else:
            aliases = {"div": "truediv"}

        for op in ops:
            try:
                alias = aliases.get(op, op)
                f = getattr(operator, alias)
                result = getattr(self.frame, op)(2 * self.frame)
                exp = f(self.frame, 2 * self.frame)
                assert_frame_equal(result, exp)

                # vs mix float
                result = getattr(self.mixed_float, op)(2 * self.mixed_float)
                exp = f(self.mixed_float, 2 * self.mixed_float)
                assert_frame_equal(result, exp)
                _check_mixed_float(result, dtype=dict(C=None))

                # vs mix int
                if op in ["add", "sub", "mul"]:
                    result = getattr(self.mixed_int, op)(2 + self.mixed_int)
                    exp = f(self.mixed_int, 2 + self.mixed_int)

                    # overflow in the uint
                    dtype = None
                    if op in ["sub"]:
                        dtype = dict(B="object", C=None)
                    elif op in ["add", "mul"]:
                        dtype = dict(C=None)
                    assert_frame_equal(result, exp)
                    _check_mixed_int(result, dtype=dtype)

                    # rops
                    r_f = lambda x, y: f(y, x)
                    result = getattr(self.frame, "r" + op)(2 * self.frame)
                    exp = r_f(self.frame, 2 * self.frame)
                    assert_frame_equal(result, exp)

                    # vs mix float
                    result = getattr(self.mixed_float, op)(2 * self.mixed_float)
                    exp = f(self.mixed_float, 2 * self.mixed_float)
                    assert_frame_equal(result, exp)
                    _check_mixed_float(result, dtype=dict(C=None))

                    result = getattr(self.intframe, op)(2 * self.intframe)
                    exp = f(self.intframe, 2 * self.intframe)
                    assert_frame_equal(result, exp)

                    # vs mix int
                    if op in ["add", "sub", "mul"]:
                        result = getattr(self.mixed_int, op)(2 + self.mixed_int)
                        exp = f(self.mixed_int, 2 + self.mixed_int)

                        # overflow in the uint
                        dtype = None
                        if op in ["sub"]:
                            dtype = dict(B="object", C=None)
                        elif op in ["add", "mul"]:
                            dtype = dict(C=None)
                        assert_frame_equal(result, exp)
                        _check_mixed_int(result, dtype=dtype)
            except:
                printing.pprint_thing("Failing operation %r" % op)
                raise

            # ndim >= 3
            ndim_5 = np.ones(self.frame.shape + (3, 4, 5))
            with assertRaisesRegexp(ValueError, "shape"):
                f(self.frame, ndim_5)

            with assertRaisesRegexp(ValueError, "shape"):
                getattr(self.frame, op)(ndim_5)

        # res_add = self.frame.add(self.frame)
        # res_sub = self.frame.sub(self.frame)
        # res_mul = self.frame.mul(self.frame)
        # res_div = self.frame.div(2 * self.frame)

        # assert_frame_equal(res_add, self.frame + self.frame)
        # assert_frame_equal(res_sub, self.frame - self.frame)
        # assert_frame_equal(res_mul, self.frame * self.frame)
        # assert_frame_equal(res_div, self.frame / (2 * self.frame))

        const_add = self.frame.add(1)
        assert_frame_equal(const_add, self.frame + 1)

        # corner cases
        result = self.frame.add(self.frame[:0])
        assert_frame_equal(result, self.frame * np.nan)

        result = self.frame[:0].add(self.frame)
        assert_frame_equal(result, self.frame * np.nan)
        with assertRaisesRegexp(NotImplementedError, "fill_value"):
            self.frame.add(self.frame.iloc[0], fill_value=3)
        with assertRaisesRegexp(NotImplementedError, "fill_value"):
            self.frame.add(self.frame.iloc[0], axis="index", fill_value=3)
Exemple #40
0
 def __unicode__(self):
     return pprint_thing(self.name)
Exemple #41
0
 def __unicode__(self):
     return pprint_thing("[Filter : [{0}] -> "
                         "[{1}]".format(self.filter[0], self.filter[1]))
Exemple #42
0
 def __unicode__(self):
     return '%s\nFill: %s\n%s' % (printing.pprint_thing(self),
                                  printing.pprint_thing(self.fill_value),
                                  printing.pprint_thing(self.sp_index))
Exemple #43
0
 def __unicode__(self):
     if self.terms is not None:
         return pprint_thing(self.terms)
     return pprint_thing(self.expr)
Exemple #44
0
def parallel_coordinates(frame,
                         class_column,
                         cols=None,
                         ax=None,
                         color=None,
                         use_columns=False,
                         xticks=None,
                         colormap=None,
                         axvlines=True,
                         axvlines_kwds=None,
                         sort_labels=False,
                         **kwds):
    """Parallel coordinates plotting.

    Parameters
    ----------
    frame: DataFrame
    class_column: str
        Column name containing class names
    cols: list, optional
        A list of column names to use
    ax: matplotlib.axis, optional
        matplotlib axis object
    color: list or tuple, optional
        Colors to use for the different classes
    use_columns: bool, optional
        If true, columns will be used as xticks
    xticks: list or tuple, optional
        A list of values to use for xticks
    colormap: str or matplotlib colormap, default None
        Colormap to use for line colors.
    axvlines: bool, optional
        If true, vertical lines will be added at each xtick
    axvlines_kwds: keywords, optional
        Options to be passed to axvline method for vertical lines
    sort_labels: bool, False
        Sort class_column labels, useful when assigning colours

        .. versionadded:: 0.20.0

    kwds: keywords
        Options to pass to matplotlib plotting method

    Returns
    -------
    ax: matplotlib axis object

    Examples
    --------
    >>> from pandas import read_csv
    >>> from pandas.tools.plotting import parallel_coordinates
    >>> from matplotlib import pyplot as plt
    >>> df = read_csv('https://raw.github.com/pandas-dev/pandas/master'
                      '/pandas/tests/data/iris.csv')
    >>> parallel_coordinates(df, 'Name', color=('#556270',
                             '#4ECDC4', '#C7F464'))
    >>> plt.show()
    """
    if axvlines_kwds is None:
        axvlines_kwds = {'linewidth': 1, 'color': 'black'}
    import matplotlib.pyplot as plt

    n = len(frame)
    classes = frame[class_column].drop_duplicates()
    class_col = frame[class_column]

    if cols is None:
        df = frame.drop(class_column, axis=1)
    else:
        df = frame[cols]

    used_legends = set([])

    ncols = len(df.columns)

    # determine values to use for xticks
    if use_columns is True:
        if not np.all(np.isreal(list(df.columns))):
            raise ValueError('Columns must be numeric to be used as xticks')
        x = df.columns
    elif xticks is not None:
        if not np.all(np.isreal(xticks)):
            raise ValueError('xticks specified must be numeric')
        elif len(xticks) != ncols:
            raise ValueError('Length of xticks must match number of columns')
        x = xticks
    else:
        x = lrange(ncols)

    if ax is None:
        ax = plt.gca()

    color_values = _get_standard_colors(num_colors=len(classes),
                                        colormap=colormap,
                                        color_type='random',
                                        color=color)

    if sort_labels:
        classes = sorted(classes)
        color_values = sorted(color_values)
    colors = dict(zip(classes, color_values))

    for i in range(n):
        y = df.iloc[i].values
        kls = class_col.iat[i]
        label = pprint_thing(kls)
        if label not in used_legends:
            used_legends.add(label)
            ax.plot(x, y, color=colors[kls], label=label, **kwds)
        else:
            ax.plot(x, y, color=colors[kls], **kwds)

    if axvlines:
        for i in x:
            ax.axvline(i, **axvlines_kwds)

    ax.set_xticks(x)
    ax.set_xticklabels(df.columns)
    ax.set_xlim(x[0], x[-1])
    ax.legend(loc='upper right')
    ax.grid()
    return ax
Exemple #45
0
 def __unicode__(self):
     operands = map(str, self.operands)
     return pprint_thing('{0}({1})'.format(self.op, ','.join(operands)))
Exemple #46
0
 def raiseException(df):
     pprint_thing('----------------------------------------')
     pprint_thing(df.to_string())
     raise TypeError
Exemple #47
0
 def __unicode__(self):
     return printing.pprint_thing(self.terms)
Exemple #48
0
 def __unicode__(self):
     return pprint_thing("[Filter : [{0}] -> "
                         "[{1}]".format(self.filter[0], self.filter[1]))
Exemple #49
0
 def raiseException(df):
     pprint_thing('----------------------------------------')
     pprint_thing(df.to_string())
     raise TypeError
Exemple #50
0
 def __unicode__(self):
     if self.terms is not None:
         return pprint_thing(self.terms)
     return pprint_thing(self.expr)
Exemple #51
0
    def convert(self):
        """Convert an expression for evaluation.

        Defaults to return the expression as a string.
        """
        return printing.pprint_thing(self.expr)
Exemple #52
0
 def __unicode__(self):
     return pprint_thing("[Condition : [{0}]]".format(self.condition))
Exemple #53
0
 def __unicode__(self):
     contents = '\n'.join(repr(c) for c in self._chunks)
     return '%s\n%s' % (object.__repr__(self), pprint_thing(contents))
Exemple #54
0
def _print_as_set(s):
    return '{%s}' % ', '.join([pprint_thing(el) for el in s])
Exemple #55
0
    def test_arith_flex_frame(self):
        ops = ['add', 'sub', 'mul', 'div', 'truediv', 'pow', 'floordiv', 'mod']
        if not compat.PY3:
            aliases = {}
        else:
            aliases = {'div': 'truediv'}

        for op in ops:
            try:
                alias = aliases.get(op, op)
                f = getattr(operator, alias)
                result = getattr(self.frame, op)(2 * self.frame)
                exp = f(self.frame, 2 * self.frame)
                assert_frame_equal(result, exp)

                # vs mix float
                result = getattr(self.mixed_float, op)(2 * self.mixed_float)
                exp = f(self.mixed_float, 2 * self.mixed_float)
                assert_frame_equal(result, exp)
                _check_mixed_float(result, dtype=dict(C=None))

                # vs mix int
                if op in ['add', 'sub', 'mul']:
                    result = getattr(self.mixed_int, op)(2 + self.mixed_int)
                    exp = f(self.mixed_int, 2 + self.mixed_int)

                    # no overflow in the uint
                    dtype = None
                    if op in ['sub']:
                        dtype = dict(B='uint64', C=None)
                    elif op in ['add', 'mul']:
                        dtype = dict(C=None)
                    assert_frame_equal(result, exp)
                    _check_mixed_int(result, dtype=dtype)

                    # rops
                    r_f = lambda x, y: f(y, x)
                    result = getattr(self.frame, 'r' + op)(2 * self.frame)
                    exp = r_f(self.frame, 2 * self.frame)
                    assert_frame_equal(result, exp)

                    # vs mix float
                    result = getattr(self.mixed_float,
                                     op)(2 * self.mixed_float)
                    exp = f(self.mixed_float, 2 * self.mixed_float)
                    assert_frame_equal(result, exp)
                    _check_mixed_float(result, dtype=dict(C=None))

                    result = getattr(self.intframe, op)(2 * self.intframe)
                    exp = f(self.intframe, 2 * self.intframe)
                    assert_frame_equal(result, exp)

                    # vs mix int
                    if op in ['add', 'sub', 'mul']:
                        result = getattr(self.mixed_int,
                                         op)(2 + self.mixed_int)
                        exp = f(self.mixed_int, 2 + self.mixed_int)

                        # no overflow in the uint
                        dtype = None
                        if op in ['sub']:
                            dtype = dict(B='uint64', C=None)
                        elif op in ['add', 'mul']:
                            dtype = dict(C=None)
                        assert_frame_equal(result, exp)
                        _check_mixed_int(result, dtype=dtype)
            except:
                printing.pprint_thing("Failing operation %r" % op)
                raise

            # ndim >= 3
            ndim_5 = np.ones(self.frame.shape + (3, 4, 5))
            msg = "Unable to coerce to Series/DataFrame"
            with assertRaisesRegexp(ValueError, msg):
                f(self.frame, ndim_5)

            with assertRaisesRegexp(ValueError, msg):
                getattr(self.frame, op)(ndim_5)

        # res_add = self.frame.add(self.frame)
        # res_sub = self.frame.sub(self.frame)
        # res_mul = self.frame.mul(self.frame)
        # res_div = self.frame.div(2 * self.frame)

        # assert_frame_equal(res_add, self.frame + self.frame)
        # assert_frame_equal(res_sub, self.frame - self.frame)
        # assert_frame_equal(res_mul, self.frame * self.frame)
        # assert_frame_equal(res_div, self.frame / (2 * self.frame))

        const_add = self.frame.add(1)
        assert_frame_equal(const_add, self.frame + 1)

        # corner cases
        result = self.frame.add(self.frame[:0])
        assert_frame_equal(result, self.frame * np.nan)

        result = self.frame[:0].add(self.frame)
        assert_frame_equal(result, self.frame * np.nan)
        with assertRaisesRegexp(NotImplementedError, 'fill_value'):
            self.frame.add(self.frame.iloc[0], fill_value=3)
        with assertRaisesRegexp(NotImplementedError, 'fill_value'):
            self.frame.add(self.frame.iloc[0], axis='index', fill_value=3)
Exemple #56
0
 def __unicode__(self):
     return pprint_thing(self, quote_strings=True,
                         escape_chars=('\t', '\r', '\n'))
Exemple #57
0
 def raw(self):
     return pprint_thing('{0}(name={1!r}, type={2})'
                         ''.format(self.__class__.__name__, self.name,
                                   self.type))
Exemple #58
0
def _print_as_set(s):
    return '{%s}' % ', '.join([pprint_thing(el) for el in s])
Exemple #59
0
 def __unicode__(self):
     return pprint_thing(self.name)