Python 已成功打印包含60多个元素的.Series子类

Python 已成功打印包含60多个元素的.Series子类,python,pandas,jupyter,pretty-print,extending-classes,Python,Pandas,Jupyter,Pretty Print,Extending Classes,这可能是一个简单的解决办法,但我不知道如何做到这一点 我扩展了pandas.Series类,以便它可以包含用于我研究的数据集。以下是我到目前为止编写的代码: import pandas as pd import numpy as np from allantools import oadev class Tombstone(pd.Series): """An extension of ``pandas.Series``, which contains raw data from a

这可能是一个简单的解决办法,但我不知道如何做到这一点

我扩展了pandas.Series类,以便它可以包含用于我研究的数据集。以下是我到目前为止编写的代码:

import pandas as pd
import numpy as np
from allantools import oadev
class Tombstone(pd.Series):
    """An extension of ``pandas.Series``, which contains raw data from a
    tombstone test.

    Parameters
    ----------
    data : array-like of floats
        The raw data measured in volts from a lock-in amplifier. If no scale
        factor is provided, this data is presumed to be in units of °/h.
    rate : float
        The sampling rate in Hz
    start : float
        The unix time stamp of the start of the run. Used to create the index
        of the Tombstone object. This can be calculated by
        running ``time.time()`` or similar. If no value is passed, the index
        of the Tombstone object will be in hours since start.
    scale_factor : float
        The conversion factor between the lock-in amplifier voltage and deg/h,
        expressed in deg/h/V.

    Attributes
    ----------
    adev : 2-tuple of arrays of floats
        Returns the Allan deviation in degrees/hour in a 2-tuple. The first
        tuple is an array of floats representing the integration times. The
        second tuple is an array of floats representing the allan deviations.
    noise : float
        The calculated angular random walk in units of °/√h taken from the
        1-Hz point on the
        Allan variance curve.
    arw : float
        The calculated angular random walk in units of °/√h taken from the
        1-Hz point on the
        Allan deviation curve.
    drift : float
        The minimum allan deviation in units of °/h.
    """

    def __init__(self, data, rate, start=None, scale_factor=0, *args, **kwargs):

        if start:
            date_index = pd.date_range(
                start=start*1e9, periods=len(data),
                freq='%.3g ms' % (1000/rate), tz='UTC')
            date_index = date_index.tz_convert('America/Los_Angeles')
        else:
            date_index = np.arange(len(data))/60/60/rate
        super().__init__(data, date_index)
        if scale_factor:
            self.name = 'voltage'
        else:
            self.name = 'rotation'
        self.rate = rate

    @property
    def _constructor(self):
        return Tombstone

    @property
    def adev(self):
        tau, dev, _, _ = oadev(np.array(self), rate=self.rate,
                               data_type='freq')
        return tau, dev

    @property
    def noise(self):
        _, dev, _, _ = oadev(np.array(self), rate=self.rate, data_type='freq')
        return dev[0]/60

    # alias
    arw = noise

    @property
    def drift(self):
        tau, dev, _, _ = oadev(np.array(self), rate=self.rate,
                               data_type='freq')
        return min(dev)
我可以在Jupyter笔记本中运行此功能:

>>> t = Tombstone(np.random.rand(60), rate=10)
>>> t
0.000000    0.497036
0.000028    0.860914
0.000056    0.626183
0.000083    0.537434
0.000111    0.451693
...
上一学期的输出显示了预期的熊猫系列

但是当我将61个元素传递给构造函数时,我得到了一个错误

>>> t = Tombstone(np.random.rand(61), rate=10)
>>> t
TypeError: cannot concatenate a non-NDFrame object
即使使用大型数据集,我仍然可以毫无问题地运行命令:

>>> from matplotlib.pyplot import loglog, show
>>> t = Tombstone(np.random.rand(10000), rate=10)
>>> t.noise
>>> loglog(*t.adev); show()
但当我要求Jupyter笔记本漂亮地打印
t
时,我总是会出错

2017-09-13更新 在戳穿堆栈跟踪之后,问题似乎出在pandas试图用省略号连接前几个元素和最后几个元素时。运行下面的代码将复制堆栈跟踪的最后几行:

>>> pd.concat(t.iloc[10:], t.iloc[:-10])

TypeError                                 Traceback (most recent call last)
<ipython-input-12-86a3d2f95e07> in <module>()
----> 1 pd.concat(t.iloc[10:], t.iloc[:-10])

/Users/wheelerj/miniconda3/lib/python3.5/site-packages/pandas/tools/merge.py in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, copy)
   1332                        keys=keys, levels=levels, names=names,
   1333                        verify_integrity=verify_integrity,
-> 1334                        copy=copy)
   1335     return op.get_result()
   1336 

/Users/wheelerj/miniconda3/lib/python3.5/site-packages/pandas/tools/merge.py in __init__(self, objs, axis, join, join_axes, keys, levels, names, ignore_index, verify_integrity, copy)
   1389         for obj in objs:
   1390             if not isinstance(obj, NDFrame):
-> 1391                 raise TypeError("cannot concatenate a non-NDFrame object")
   1392 
   1393             # consolidate

TypeError: cannot concatenate a non-NDFrame object
pd.concat(t.iloc[10:],t.iloc[:-10]) TypeError回溯(最近一次调用上次) 在() ---->1 pd.concat(t.iloc[10:],t.iloc[:-10]) /concat中的Users/wheelerj/miniconda3/lib/python3.5/site-packages/pandas/tools/merge.py(对象、轴、连接、连接轴、忽略索引、键、级别、名称、验证完整性、复制) 1332键=键,级别=级别,名称=名称, 1333验证_完整性=验证_完整性, ->1334副本=副本) 1335返回操作获取结果() 1336 /Users/wheelerj/miniconda3/lib/python3.5/site-packages/pandas/tools/merge.py in_uuu_uinit_u(self、objs、axis、join、join_axes、key、levels、name、ignore_index、verify_integrity、copy) 1389对于objs中的obj: 1390如果不存在(obj,NDFrame): ->1391 raise TypeError(“无法连接非NDFrame对象”) 1392 1393#合并 TypeError:无法连接非NDFrame对象
我找到了一个修复程序,在我的情况下应该可以使用。我仍然认为可以通过将切片表示为NDFrame对象来解决这个问题。也许会有其他人知道

如果我重写了我的
墓碑
类中的
\uuuu repr\uuuu
内置函数

def __repr__(self):
    ret = 'Tombstone('
    ret += 'rate=%.3g' % self.rate
    # etc...
    ret += ')'
    return ret
我可以运行以下命令:

>>> t = Tombstone(np.random.rand(61), rate=10)
>>> t
Tombstone(rate=10)

我找到了一个修复程序,在我的情况下应该可以使用。我仍然认为可以通过将切片表示为NDFrame对象来解决这个问题。也许会有其他人知道

如果我重写了我的
墓碑
类中的
\uuuu repr\uuuu
内置函数

def __repr__(self):
    ret = 'Tombstone('
    ret += 'rate=%.3g' % self.rate
    # etc...
    ret += ')'
    return ret
我可以运行以下命令:

>>> t = Tombstone(np.random.rand(61), rate=10)
>>> t
Tombstone(rate=10)

我想问题出在您调用
super()。\uu init\uuu()
pd.Series.\uuuu init\uuuuu()
有许多您没有传递的附加参数。在我的例子中,我得到了
fastpath
参数集,但没有处理它

如果我这样调整你的
\uuuu init\uuuu()
,它似乎可以工作:

def __init__(self, data=None, index=None, rate=None, start=None, scale_factor=0, *args, **kwargs):
    if index is None and rate is not None:
        if start:
            date_index = pd.date_range(
                start=start*1e9, periods=len(data),
                freq='%.3g ms' % (1000/rate), tz='UTC')
            date_index = date_index.tz_convert('America/Los_Angeles')
        else:
            date_index = np.arange(len(data))/60/60/rate
    else:
        date_index=index
    super().__init__(data, date_index, *args, **kwargs)
    if scale_factor:
        self.name = 'voltage'
    else:
        self.name = 'rotation'
    self.rate = rate

您需要确保
take
并通过
iloc
索引返回您类型的对象(
Tombstone

我认为问题在于调用
super()
pd.Series.\uuuu init\uuuuu()
有许多您没有传递的附加参数。在我的例子中,我得到了
fastpath
参数集,但没有处理它

如果我这样调整你的
\uuuu init\uuuu()
,它似乎可以工作:

def __init__(self, data=None, index=None, rate=None, start=None, scale_factor=0, *args, **kwargs):
    if index is None and rate is not None:
        if start:
            date_index = pd.date_range(
                start=start*1e9, periods=len(data),
                freq='%.3g ms' % (1000/rate), tz='UTC')
            date_index = date_index.tz_convert('America/Los_Angeles')
        else:
            date_index = np.arange(len(data))/60/60/rate
    else:
        date_index=index
    super().__init__(data, date_index, *args, **kwargs)
    if scale_factor:
        self.name = 'voltage'
    else:
        self.name = 'rotation'
    self.rate = rate
您需要确保
take
并通过
iloc
索引返回您类型的对象(
Tombstone