本文整理匯總了Python中axes.Axes.from_arrays方法的典型用法代碼示例。如果您正苦於以下問題:Python Axes.from_arrays方法的具體用法?Python Axes.from_arrays怎麽用?Python Axes.from_arrays使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類axes.Axes
的用法示例。
在下文中一共展示了Axes.from_arrays方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: from_pandas
# 需要導入模塊: from axes import Axes [as 別名]
# 或者: from axes.Axes import from_arrays [as 別名]
def from_pandas(cls, data, dims=None):
""" Initialize a DimArray from pandas
data: pandas object (Series, DataFrame, Panel, Panel4D)
dims, optional: dimension (axis) names, otherwise look at ax.name for ax in data.axes
>>> import pandas as pd
>>> s = pd.Series([3,5,6], index=['a','b','c'])
>>> s.index.name = 'dim0'
>>> DimArray.from_pandas(s)
dimarray: 3 non-null elements (0 null)
dimensions: 'dim0'
0 / dim0 (3): a to c
array([3, 5, 6])
Also work with Multi-Index
>>> panel = pd.Panel(np.arange(2*3*4).reshape(2,3,4))
>>> b = panel.to_frame() # pandas' method to convert Panel to DataFrame via MultiIndex
>>> DimArray.from_pandas(b) # doctest: +SKIP
dimarray: 24 non-null elements (0 null)
dimensions: 'major,minor', 'x1'
0 / major,minor (12): (0, 0) to (2, 3)
1 / x1 (2): 0 to 1
...
"""
try:
import pandas as pd
except ImportError:
raise ImportError("pandas module is required to use this method")
axisnames = []
axes = []
for i, ax in enumerate(data.axes):
# axis name
name = ax.name
if dims is not None: name = dims[i]
if name is None: name = 'x%i'% (i)
# Multi-Index: make a Grouped Axis object
if isinstance(ax, pd.MultiIndex):
# level names
names = ax.names
for j, nm in enumerate(names):
if nm is None:
names[j] = '%s_%i'%(name,j)
miaxes = Axes.from_arrays(ax.levels, dims=names)
axis = GroupedAxis(*miaxes)
# Index: Make a simple Axis
else:
axis = Axis(ax.values, name)
axes.append(axis)
#axisnames, axes = zip(*[(ax.name, ax.values) for ax in data.axes])
return cls(data.values, axes=axes)