|
1 |
| -""" Factory methods to create N-D panels """ |
2 |
| - |
3 |
| -import pandas.lib as lib |
4 |
| - |
5 |
| - |
6 |
| -def create_nd_panel_factory(klass_name, axis_orders, axis_slices, slicer, axis_aliases=None, stat_axis=2): |
7 |
| - """ manufacture a n-d class: |
8 |
| -
|
9 |
| - parameters |
10 |
| - ---------- |
11 |
| - klass_name : the klass name |
12 |
| - axis_orders : the names of the axes in order (highest to lowest) |
13 |
| - axis_slices : a dictionary that defines how the axes map to the sliced axis |
14 |
| - slicer : the class representing a slice of this panel |
15 |
| - axis_aliases: a dictionary defining aliases for various axes |
16 |
| - default = { major : major_axis, minor : minor_axis } |
17 |
| - stat_axis : the default statistic axis |
18 |
| - default = 2 |
19 |
| - het_axis : the info axis |
20 |
| -
|
21 |
| -
|
22 |
| - returns |
23 |
| - ------- |
24 |
| - a class object reprsenting this panel |
25 |
| -
|
26 |
| -
|
27 |
| - """ |
28 |
| - |
29 |
| - # if slicer is a name, get the object |
30 |
| - if isinstance(slicer, basestring): |
31 |
| - import pandas |
32 |
| - try: |
33 |
| - slicer = getattr(pandas, slicer) |
34 |
| - except: |
35 |
| - raise Exception("cannot create this slicer [%s]" % slicer) |
36 |
| - |
37 |
| - # build the klass |
38 |
| - klass = type(klass_name, (slicer,), {}) |
39 |
| - |
40 |
| - # add the class variables |
41 |
| - klass._AXIS_ORDERS = axis_orders |
42 |
| - klass._AXIS_NUMBERS = dict([(a, i) for i, a in enumerate(axis_orders)]) |
43 |
| - klass._AXIS_ALIASES = axis_aliases or dict() |
44 |
| - klass._AXIS_NAMES = dict([(i, a) for i, a in enumerate(axis_orders)]) |
45 |
| - klass._AXIS_SLICEMAP = axis_slices |
46 |
| - klass._AXIS_LEN = len(axis_orders) |
47 |
| - klass._default_stat_axis = stat_axis |
48 |
| - klass._het_axis = 0 |
49 |
| - klass._info_axis = axis_orders[klass._het_axis] |
50 |
| - |
51 |
| - klass._constructor_sliced = slicer |
52 |
| - |
53 |
| - # add the axes |
54 |
| - for i, a in enumerate(axis_orders): |
55 |
| - setattr(klass, a, lib.AxisProperty(i)) |
56 |
| - |
57 |
| - #### define the methods #### |
58 |
| - def __init__(self, *args, **kwargs): |
59 |
| - if not (kwargs.get('data') or len(args)): |
60 |
| - raise Exception( |
61 |
| - "must supply at least a data argument to [%s]" % klass_name) |
62 |
| - if 'copy' not in kwargs: |
63 |
| - kwargs['copy'] = False |
64 |
| - if 'dtype' not in kwargs: |
65 |
| - kwargs['dtype'] = None |
66 |
| - self._init_data(*args, **kwargs) |
67 |
| - klass.__init__ = __init__ |
68 |
| - |
69 |
| - def _get_plane_axes(self, axis): |
70 |
| - |
71 |
| - axis = self._get_axis_name(axis) |
72 |
| - index = self._AXIS_ORDERS.index(axis) |
73 |
| - |
74 |
| - planes = [] |
75 |
| - if index: |
76 |
| - planes.extend(self._AXIS_ORDERS[0:index]) |
77 |
| - if index != self._AXIS_LEN: |
78 |
| - planes.extend(self._AXIS_ORDERS[index + 1:]) |
79 |
| - |
80 |
| - return [getattr(self, p) for p in planes] |
81 |
| - klass._get_plane_axes = _get_plane_axes |
82 |
| - |
83 |
| - def _combine(self, other, func, axis=0): |
84 |
| - if isinstance(other, klass): |
85 |
| - return self._combine_with_constructor(other, func) |
86 |
| - return super(klass, self)._combine(other, func, axis=axis) |
87 |
| - klass._combine = _combine |
88 |
| - |
89 |
| - def _combine_with_constructor(self, other, func): |
90 |
| - |
91 |
| - # combine labels to form new axes |
92 |
| - new_axes = [] |
93 |
| - for a in self._AXIS_ORDERS: |
94 |
| - new_axes.append(getattr(self, a) + getattr(other, a)) |
95 |
| - |
96 |
| - # reindex: could check that everything's the same size, but forget it |
97 |
| - d = dict([(a, ax) for a, ax in zip(self._AXIS_ORDERS, new_axes)]) |
98 |
| - d['copy'] = False |
99 |
| - this = self.reindex(**d) |
100 |
| - other = other.reindex(**d) |
101 |
| - |
102 |
| - result_values = func(this.values, other.values) |
103 |
| - |
104 |
| - return self._constructor(result_values, **d) |
105 |
| - klass._combine_with_constructor = _combine_with_constructor |
106 |
| - |
107 |
| - # set as NonImplemented operations which we don't support |
108 |
| - for f in ['to_frame', 'to_excel', 'to_sparse', 'groupby', 'join', 'filter', 'dropna', 'shift', 'take']: |
109 |
| - def func(self, *args, **kwargs): |
110 |
| - raise NotImplementedError |
111 |
| - setattr(klass, f, func) |
112 |
| - |
113 |
| - # add the aggregate operations |
114 |
| - klass._add_aggregate_operations() |
115 |
| - |
116 |
| - return klass |
| 1 | +""" Factory methods to create N-D panels """ |
| 2 | + |
| 3 | +import pandas.lib as lib |
| 4 | + |
| 5 | + |
| 6 | +def create_nd_panel_factory(klass_name, axis_orders, axis_slices, slicer, axis_aliases=None, stat_axis=2): |
| 7 | + """ manufacture a n-d class: |
| 8 | +
|
| 9 | + parameters |
| 10 | + ---------- |
| 11 | + klass_name : the klass name |
| 12 | + axis_orders : the names of the axes in order (highest to lowest) |
| 13 | + axis_slices : a dictionary that defines how the axes map to the sliced axis |
| 14 | + slicer : the class representing a slice of this panel |
| 15 | + axis_aliases: a dictionary defining aliases for various axes |
| 16 | + default = { major : major_axis, minor : minor_axis } |
| 17 | + stat_axis : the default statistic axis |
| 18 | + default = 2 |
| 19 | + het_axis : the info axis |
| 20 | +
|
| 21 | +
|
| 22 | + returns |
| 23 | + ------- |
| 24 | + a class object reprsenting this panel |
| 25 | +
|
| 26 | +
|
| 27 | + """ |
| 28 | + |
| 29 | + # if slicer is a name, get the object |
| 30 | + if isinstance(slicer, basestring): |
| 31 | + import pandas |
| 32 | + try: |
| 33 | + slicer = getattr(pandas, slicer) |
| 34 | + except: |
| 35 | + raise Exception("cannot create this slicer [%s]" % slicer) |
| 36 | + |
| 37 | + # build the klass |
| 38 | + klass = type(klass_name, (slicer,), {}) |
| 39 | + |
| 40 | + # add the class variables |
| 41 | + klass._AXIS_ORDERS = axis_orders |
| 42 | + klass._AXIS_NUMBERS = dict([(a, i) for i, a in enumerate(axis_orders)]) |
| 43 | + klass._AXIS_ALIASES = axis_aliases or dict() |
| 44 | + klass._AXIS_NAMES = dict([(i, a) for i, a in enumerate(axis_orders)]) |
| 45 | + klass._AXIS_SLICEMAP = axis_slices |
| 46 | + klass._AXIS_LEN = len(axis_orders) |
| 47 | + klass._default_stat_axis = stat_axis |
| 48 | + klass._het_axis = 0 |
| 49 | + klass._info_axis = axis_orders[klass._het_axis] |
| 50 | + |
| 51 | + klass._constructor_sliced = slicer |
| 52 | + |
| 53 | + # add the axes |
| 54 | + for i, a in enumerate(axis_orders): |
| 55 | + setattr(klass, a, lib.AxisProperty(i)) |
| 56 | + |
| 57 | + #### define the methods #### |
| 58 | + def __init__(self, *args, **kwargs): |
| 59 | + if not (kwargs.get('data') or len(args)): |
| 60 | + raise Exception( |
| 61 | + "must supply at least a data argument to [%s]" % klass_name) |
| 62 | + if 'copy' not in kwargs: |
| 63 | + kwargs['copy'] = False |
| 64 | + if 'dtype' not in kwargs: |
| 65 | + kwargs['dtype'] = None |
| 66 | + self._init_data(*args, **kwargs) |
| 67 | + klass.__init__ = __init__ |
| 68 | + |
| 69 | + def _get_plane_axes(self, axis): |
| 70 | + |
| 71 | + axis = self._get_axis_name(axis) |
| 72 | + index = self._AXIS_ORDERS.index(axis) |
| 73 | + |
| 74 | + planes = [] |
| 75 | + if index: |
| 76 | + planes.extend(self._AXIS_ORDERS[0:index]) |
| 77 | + if index != self._AXIS_LEN: |
| 78 | + planes.extend(self._AXIS_ORDERS[index + 1:]) |
| 79 | + |
| 80 | + return [getattr(self, p) for p in planes] |
| 81 | + klass._get_plane_axes = _get_plane_axes |
| 82 | + |
| 83 | + def _combine(self, other, func, axis=0): |
| 84 | + if isinstance(other, klass): |
| 85 | + return self._combine_with_constructor(other, func) |
| 86 | + return super(klass, self)._combine(other, func, axis=axis) |
| 87 | + klass._combine = _combine |
| 88 | + |
| 89 | + def _combine_with_constructor(self, other, func): |
| 90 | + |
| 91 | + # combine labels to form new axes |
| 92 | + new_axes = [] |
| 93 | + for a in self._AXIS_ORDERS: |
| 94 | + new_axes.append(getattr(self, a) + getattr(other, a)) |
| 95 | + |
| 96 | + # reindex: could check that everything's the same size, but forget it |
| 97 | + d = dict([(a, ax) for a, ax in zip(self._AXIS_ORDERS, new_axes)]) |
| 98 | + d['copy'] = False |
| 99 | + this = self.reindex(**d) |
| 100 | + other = other.reindex(**d) |
| 101 | + |
| 102 | + result_values = func(this.values, other.values) |
| 103 | + |
| 104 | + return self._constructor(result_values, **d) |
| 105 | + klass._combine_with_constructor = _combine_with_constructor |
| 106 | + |
| 107 | + # set as NonImplemented operations which we don't support |
| 108 | + for f in ['to_frame', 'to_excel', 'to_sparse', 'groupby', 'join', 'filter', 'dropna', 'shift', 'take']: |
| 109 | + def func(self, *args, **kwargs): |
| 110 | + raise NotImplementedError |
| 111 | + setattr(klass, f, func) |
| 112 | + |
| 113 | + # add the aggregate operations |
| 114 | + klass._add_aggregate_operations() |
| 115 | + |
| 116 | + return klass |
0 commit comments