<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def from_pandas(cls, data, columns=None, key_on='idx', name=None, series_key='data', grouped=False, records=False, **kwargs):
 """Load values from a pandas ``Series`` or ``DataFrame`` object Parameters data : pandas ``Series`` or ``DataFrame`` Pandas object to import data from. columns: list, default None DataFrame columns to convert to Data. Keys default to col names. If columns are given and on_index is False, x-axis data will default to the first column. key_on: string, default 'index' Value to key on for x-axis data. Defaults to index. name : string, default None Applies to the ``name`` attribute of the generated class. If ``None`` (default), then the ``name`` attribute of ``pd_obj`` is used if it exists, or ``'table'`` if it doesn't. series_key : string, default 'data' Applies only to ``Series``. If ``None`` (default), then defaults to data.name. For example, if ``series_key`` is ``'x'``, then the entries of the ``values`` list grouped: boolean, default False Pass true for an extra grouping parameter records: boolean, defaule False Requires Pandas 0.12 or greater. Writes the Pandas DataFrame using the df.to_json(orient='records') formatting. **kwargs : dict Additional arguments passed to the :class:`Data` constructor. """

        # Note: There's an experimental JSON encoder floating around in
        # pandas land that hasn't made it into the main branch. This
        # function should be revisited if it ever does.
        if not pd:
            raise LoadError('pandas could not be imported')
        if not hasattr(data, 'index'):
            raise ValueError('Please load a Pandas object.')

        if name:
            vega_data = cls(name=name, **kwargs)
        else:
            vega_data = cls(name='table', **kwargs)

        pd_obj = data.copy()
        if columns:
            pd_obj = data[columns]
        if key_on != 'idx':
            pd_obj.index = data[key_on]
        if records:
            # The worst
            vega_data.values = json.loads(pd_obj.to_json(orient='records'))
            return vega_data

        vega_data.values = []

        if isinstance(pd_obj, pd.Series):
            data_key = data.name or series_key
            for i, v in pd_obj.iteritems():
                value = {}
                value['idx'] = cls.serialize(i)
                value['col'] = data_key
                value['val'] = cls.serialize(v)
                vega_data.values.append(value)

        elif isinstance(pd_obj, pd.DataFrame):
            # We have to explicitly convert the column names to strings
            # because the json serializer doesn't allow for integer keys.
            for i, row in pd_obj.iterrows():
                for num, (k, v) in enumerate(row.iteritems()):
                    value = {}
                    value['idx'] = cls.serialize(i)
                    value['col'] = cls.serialize(k)
                    value['val'] = cls.serialize(v)
                    if grouped:
                        value['group'] = num
                    vega_data.values.append(value)
        else:
            raise ValueError('cannot load from data type '
                             + type(pd_obj).__name__)
        return vega_data