Skip to content

Commit 0d2e1a7

Browse files
committed
TST: remove special regression window/cluster constants, misc testing
1 parent c729669 commit 0d2e1a7

File tree

9 files changed

+76
-73
lines changed

9 files changed

+76
-73
lines changed

RELEASE.rst

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pandas 0.6.0
7575
- Add `DataFrame.to_html` for formatting DataFrame to HTML (PR #387)
7676
- MaskedArray can be passed to DataFrame constructor and masked values will be
7777
converted to NaN (PR #396)
78+
- Add `DataFrame.boxplot` function (GH #368, others)
7879
7980
**Improvements to existing features**
8081

pandas/stats/common.py

+30-46
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,41 @@
1-
TIME = 0
2-
ENTITY = 1
3-
41
def _get_cluster_type(cluster_type):
5-
if cluster_type in (TIME, ENTITY, None):
2+
cluster_type = _WINDOW_TYPES.get(cluster_type, cluster_type)
3+
if cluster_type is None:
64
return cluster_type
75

8-
elif isinstance(cluster_type, basestring):
9-
cluster_type_up = cluster_type.upper()
10-
11-
if cluster_type_up == 'ENTITY':
12-
return ENTITY
13-
elif cluster_type_up == 'TIME':
14-
return TIME
6+
cluster_type_up = cluster_type.upper()
157

16-
raise Exception('Unrecognized clustering type: %s' % cluster_type)
8+
if cluster_type_up == 'ENTITY':
9+
return 'entity'
10+
elif cluster_type_up == 'TIME':
11+
return 'time'
12+
else: # pragma: no cover
13+
raise Exception('Unrecognized cluster type: %s' % cluster_type)
1714

18-
FULL_SAMPLE = 0
19-
ROLLING = 1
20-
EXPANDING = 2
21-
22-
def _get_window_type(window_type):
23-
if window_type in (FULL_SAMPLE, ROLLING, EXPANDING):
24-
return window_type
25-
elif isinstance(window_type, basestring):
26-
window_type_up = window_type.upper()
15+
_CLUSTER_TYPES = {
16+
0 : 'time',
17+
1 : 'entity'
18+
}
2719

28-
if window_type_up in ('FULL SAMPLE', 'FULL_SAMPLE'):
29-
return FULL_SAMPLE
30-
elif window_type_up == 'ROLLING':
31-
return ROLLING
32-
elif window_type_up == 'EXPANDING':
33-
return EXPANDING
20+
_WINDOW_TYPES = {
21+
0 : 'full_sample',
22+
1 : 'rolling',
23+
2 : 'expanding'
24+
}
3425

35-
raise Exception('Unrecognized window type: %s' % window_type)
3626

37-
def _get_window_type_name(window_type):
38-
names = {
39-
0 : 'full sample',
40-
1 : 'rolling',
41-
2 : 'expanding'
42-
}
43-
return names[window_type]
27+
def _get_window_type(window_type):
28+
window_type = _WINDOW_TYPES.get(window_type, window_type)
29+
window_type_up = window_type.upper()
30+
31+
if window_type_up in ('FULL SAMPLE', 'FULL_SAMPLE'):
32+
return 'full_sample'
33+
elif window_type_up == 'ROLLING':
34+
return 'rolling'
35+
elif window_type_up == 'EXPANDING':
36+
return 'expanding'
37+
else: # pragma: no cover
38+
raise Exception('Unrecognized window type: %s' % window_type)
4439

4540
def banner(text, width=80):
4641
"""
@@ -52,14 +47,3 @@ def banner(text, width=80):
5247
right = toFill - left
5348

5449
return '%s%s%s' % ('-' * left, text, '-' * right)
55-
56-
def f_stat_to_dict(result):
57-
f_stat, shape, p_value = result
58-
59-
result = {}
60-
result['f-stat'] = f_stat
61-
result['DF X'] = shape[0]
62-
result['DF Resid'] = shape[1]
63-
result['p-value'] = p_value
64-
65-
return result

pandas/stats/fama_macbeth.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def __init__(self, y, x, window_type='rolling', window=10,
155155

156156
@property
157157
def _is_rolling(self):
158-
return self._window_type == common.ROLLING
158+
return self._window_type == 'rolling'
159159

160160
def _calc_stats(self):
161161
mean_betas = []

pandas/stats/interface.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ def ols(**kwargs):
9494

9595
if window_type is None:
9696
if window is None:
97-
window_type = common.FULL_SAMPLE
97+
window_type = 'full_sample'
9898
else:
99-
window_type = common.ROLLING
99+
window_type = 'rolling'
100100
else:
101101
window_type = common._get_window_type(window_type)
102102

103-
if window_type != common.FULL_SAMPLE:
104-
kwargs['window_type'] = common._get_window_type_name(window_type)
103+
if window_type != 'full_sample':
104+
kwargs['window_type'] = common._get_window_type(window_type)
105105

106106
y = kwargs.get('y')
107107
x = kwargs.get('x')
@@ -113,7 +113,7 @@ def ols(**kwargs):
113113
if isinstance(x, (Panel, LongPanel)):
114114
panel = True
115115

116-
if window_type == common.FULL_SAMPLE:
116+
if window_type == 'full_sample':
117117
for rolling_field in ('window_type', 'window', 'min_periods'):
118118
if rolling_field in kwargs:
119119
del kwargs[rolling_field]

pandas/stats/math.py

-9
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,3 @@ def calc_F(R, r, beta, var_beta, nobs, df):
121121

122122
return F, (q, nobs - df), p_value
123123

124-
def chain_dot(*matrices):
125-
"""
126-
Returns the dot product of the given matrices.
127-
128-
Parameters
129-
----------
130-
matrices: argument list of ndarray
131-
"""
132-
return reduce(lambda x, y: np.dot(y, x), matrices[::-1])

pandas/stats/ols.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def _f_stat_raw(self):
184184
@cache_readonly
185185
def f_stat(self):
186186
"""Returns the f-stat value."""
187-
return common.f_stat_to_dict(self._f_stat_raw)
187+
return f_stat_to_dict(self._f_stat_raw)
188188

189189
def f_test(self, hypothesis):
190190
"""Runs the F test, given a joint hypothesis. The hypothesis is
@@ -239,7 +239,7 @@ def f_test(self, hypothesis):
239239
result = math.calc_F(R, r, self._beta_raw, self._var_beta_raw,
240240
self._nobs, self.df)
241241

242-
return common.f_stat_to_dict(result)
242+
return f_stat_to_dict(result)
243243

244244
@cache_readonly
245245
def _p_value_raw(self):
@@ -584,7 +584,7 @@ def df_resid(self):
584584
@cache_readonly
585585
def f_stat(self):
586586
"""Returns the f-stat value."""
587-
f_stat_dicts = dict((date, common.f_stat_to_dict(f_stat))
587+
f_stat_dicts = dict((date, f_stat_to_dict(f_stat))
588588
for date, f_stat in zip(self.beta.index,
589589
self._f_stat_raw))
590590

@@ -672,7 +672,7 @@ def y_predict(self):
672672

673673
@property
674674
def _is_rolling(self):
675-
return self._window_type == common.ROLLING
675+
return self._window_type == 'rolling'
676676

677677
@cache_readonly
678678
def _beta_raw(self):
@@ -1226,3 +1226,16 @@ def _y_converter(y):
12261226
return np.array([y])
12271227
else:
12281228
return y
1229+
1230+
1231+
def f_stat_to_dict(result):
1232+
f_stat, shape, p_value = result
1233+
1234+
result = {}
1235+
result['f-stat'] = f_stat
1236+
result['DF X'] = shape[0]
1237+
result['DF Resid'] = shape[1]
1238+
result['p-value'] = p_value
1239+
1240+
return result
1241+

pandas/stats/plm.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,9 @@ def _rmse_raw(self):
362362
@cache_readonly
363363
def _var_beta_raw(self):
364364
cluster_axis = None
365-
if self._cluster == common.TIME:
365+
if self._cluster == 'time':
366366
cluster_axis = 0
367-
elif self._cluster == common.ENTITY:
367+
elif self._cluster == 'entity':
368368
cluster_axis = 1
369369

370370
x = self._x
@@ -547,9 +547,9 @@ def _var_beta_raw(self):
547547
dates = x.major_axis
548548

549549
cluster_axis = None
550-
if self._cluster == common.TIME:
550+
if self._cluster == 'time':
551551
cluster_axis = 0
552-
elif self._cluster == common.ENTITY:
552+
elif self._cluster == 'entity':
553553
cluster_axis = 1
554554

555555
nobs = self._nobs
@@ -663,8 +663,8 @@ class NonPooledPanelOLS(object):
663663
True if you want an intercept.
664664
nw_lags : None or int
665665
Number of Newey-West lags.
666-
window_type : int
667-
FULL_SAMPLE, ROLLING, EXPANDING. FULL_SAMPLE by default.
666+
window_type : {'full_sample', 'rolling', 'expanding'}
667+
'full_sample' by default
668668
window : int
669669
size of window (for rolling/expanding OLS)
670670
"""
@@ -690,7 +690,7 @@ class NonPooledPanelOLS(object):
690690
'y_predict'
691691
]
692692

693-
def __init__(self, y, x, window_type=common.FULL_SAMPLE, window=None,
693+
def __init__(self, y, x, window_type='full_sample', window=None,
694694
min_periods=None, intercept=True, nw_lags=None,
695695
nw_overlap=False):
696696

pandas/stats/var.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pandas.core.panel import Panel
88
from pandas.core.series import Series
99
import pandas.stats.common as common
10-
from pandas.stats.math import chain_dot, inv
10+
from pandas.stats.math import inv
1111
from pandas.stats.ols import _combine_rhs
1212

1313
class VAR(object):
@@ -571,3 +571,13 @@ def _drop_incomplete_rows(array):
571571

572572
def _make_param_name(lag, name):
573573
return 'L%d.%s' % (lag, name)
574+
575+
def chain_dot(*matrices):
576+
"""
577+
Returns the dot product of the given matrices.
578+
579+
Parameters
580+
----------
581+
matrices: argument list of ndarray
582+
"""
583+
return reduce(lambda x, y: np.dot(y, x), matrices[::-1])

pandas/tests/test_multilevel.py

+4
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,10 @@ def test_partial_set(self):
679679
exp.ix[2000].values[:] = 5
680680
assert_frame_equal(df, exp)
681681

682+
# this works...for now
683+
df['A'].ix[14] = 5
684+
self.assertEquals(df['A'][14], 5)
685+
682686
def test_unstack_preserve_types(self):
683687
# GH #403
684688
self.ymd['E'] = 'foo'

0 commit comments

Comments
 (0)