使用Pandas可视化自定义Python数组对象中的数据
我正在尝试实现一个具有数组接口的python对象,我想看看是否可以使用pandas来可视化对象中的数据。我基本上是在尝试这个例子。numpy版本是使用Pandas可视化自定义Python数组对象中的数据,python,arrays,pandas,numpy,Python,Arrays,Pandas,Numpy,我正在尝试实现一个具有数组接口的python对象,我想看看是否可以使用pandas来可视化对象中的数据。我基本上是在尝试这个例子。numpy版本是 import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD')) 现在我想做这样的事情 import pandas as pd import <my_module> as np
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))
现在我想做这样的事情
import pandas as pd
import <my_module> as np
a = <array object from my module of size (6,4) with python float values>
df = pd.DataFrame(a, index=dates, columns=list('ABCD'))
因此,从技术上讲,我所尝试的应该是可行的,我希望,如果可以使用np.array创建一个numpy数组对象,pandas应该能够处理该对象,就像它处理传入numpy数组时的情况一样。相反,它似乎试图强制执行数组大小为0的条件。这是熊猫虫还是我在尝试不可行的东西
很抱歉发了这么长的帖子,但这是我唯一能传达完整问题的方法。我真的很感激任何能帮我弄清楚到底发生了什么的回复
编辑
这是我认为需要的改变。有了这个,我似乎得到了我想要的。希望熊猫队的人能回应
else:
try:
arr = np.array(data, dtype=dtype, copy=copy)
except (ValueError, TypeError) as e:
exc = TypeError('DataFrame constructor called with '
'incompatible data and dtype: %s' % e)
raise_with_traceback(exc)
if isinstance(arr, (np.ndarray, Series, Index)):
if data.dtype.names:
data_columns = list(data.dtype.names)
data = dict((k, data[k]) for k in data_columns)
if columns is None:
columns = data_columns
mgr = self._init_dict(data, index, columns, dtype=dtype)
elif getattr(data, 'name', None):
mgr = self._init_dict({data.name: data}, index, columns,
dtype=dtype)
else:
mgr = self._init_ndarray(data, index, columns, dtype=dtype,
copy=copy)
elif arr.ndim == 0 and index is not None and columns is not None:
if isinstance(data, compat.string_types) and dtype is None:
dtype = np.object_
if dtype is None:
dtype, data = _infer_dtype_from_scalar(data)
values = np.empty((len(index), len(columns)), dtype=dtype)
values.fill(data)
mgr = self._init_ndarray(values, index, columns, dtype=dtype,
copy=False)
else:
raise PandasError('DataFrame constructor not properly called!')
>>> import numpy as np
>>> import <my_module> as gnp
>>> a = <array object from my_module of size (6,4) with python float values>
>>> anp = np.array(a)
>>> print(anp)
array([[ 1.65596968, -2.39189608, -2.92223678, -1.0312887 ],
[ 0.41918582, 2.35760521, 3.97045546, 1.06558325],
[-0.29084453, 1.43759343, 2.94189788, 0.07185253],
[-1.51164217, 0.9233231 , 2.05859341, 2.62394528],
[ 0.12106421, -1.03187945, 2.36255711, 0.86845971],
[-1.39774121, 5.17955785, -1.05458446, -2.10051246]])
def __init__(self, data=None, index=None, columns=None, dtype=None,
copy=False):
elif isinstance(data, (np.ndarray, Series, Index)):
....
else:
try:
arr = np.array(data, dtype=dtype, copy=copy)
except (ValueError, TypeError) as e:
exc = TypeError('DataFrame constructor called with '
'incompatible data and dtype: %s' % e)
raise_with_traceback(exc)
if arr.ndim == 0 and index is not None and columns is not None:
....
else:
raise PandasError('DataFrame constructor not properly called!')
else:
try:
arr = np.array(data, dtype=dtype, copy=copy)
except (ValueError, TypeError) as e:
exc = TypeError('DataFrame constructor called with '
'incompatible data and dtype: %s' % e)
raise_with_traceback(exc)
if isinstance(arr, (np.ndarray, Series, Index)):
if data.dtype.names:
data_columns = list(data.dtype.names)
data = dict((k, data[k]) for k in data_columns)
if columns is None:
columns = data_columns
mgr = self._init_dict(data, index, columns, dtype=dtype)
elif getattr(data, 'name', None):
mgr = self._init_dict({data.name: data}, index, columns,
dtype=dtype)
else:
mgr = self._init_ndarray(data, index, columns, dtype=dtype,
copy=copy)
elif arr.ndim == 0 and index is not None and columns is not None:
if isinstance(data, compat.string_types) and dtype is None:
dtype = np.object_
if dtype is None:
dtype, data = _infer_dtype_from_scalar(data)
values = np.empty((len(index), len(columns)), dtype=dtype)
values.fill(data)
mgr = self._init_ndarray(values, index, columns, dtype=dtype,
copy=False)
else:
raise PandasError('DataFrame constructor not properly called!')