def to_numpy(self):
"""Return a numpy array with the content of a crosstab"""
# Set numpy table type based on the crosstab's type...
if isinstance(self, IntPivotCrosstab):
np_type = np.dtype(np.int32)
elif isinstance(self, PivotCrosstab):
np_type = np.dtype(np.float32)
# Initialize numpy table...
np_table = np.empty([len(self.row_ids), len(self.col_ids)], np_type)
np_table.fill(self.missing or 0)
# Fill and return numpy table...
for row_idx in xrange(len(self.row_ids)):
for col_idx in xrange(len(self.col_ids)):
try:
np_table[row_idx][col_idx] = self.values[
(self.row_ids[row_idx], self.col_ids[col_idx])
]
except KeyError:
pass
return np_table
# TODO: test.
评论列表
文章目录