Skip to content

Commit ecd11e4

Browse files
committed
TST: some panel fixes. More robust label-based slicing for MultiIndex
1 parent 137af52 commit ecd11e4

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

pandas/core/indexing.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,12 @@ def _is_list_like(obj):
398398

399399
def _is_label_slice(labels, obj):
400400
def crit(x):
401-
if x in labels:
401+
try:
402+
_ = labels.get_loc(x)
402403
return False
403-
else:
404+
except KeyError:
404405
return isinstance(x, int) or x is None
406+
405407
return not crit(obj.start) or not crit(obj.stop)
406408

407409
def _need_slice(obj):

pandas/stats/fama_macbeth.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __init__(self, y, x, intercept=True, nw_lags=None,
3939
time_effects=time_effects, x_effects=x_effects, cluster=cluster,
4040
dropped_dummies=dropped_dummies, verbose=verbose)
4141

42-
self._cols = self._ols_result._x.items
42+
self._cols = self._ols_result._x.columns
4343

4444
@cache_readonly
4545
def _beta_raw(self):

pandas/stats/ols.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from pandas.core.api import DataFrame, Series
1313
from pandas.core.index import MultiIndex
14-
from pandas.core.panel import Panel, LongPanel
14+
from pandas.core.panel import Panel
1515
from pandas.util.decorators import cache_readonly
1616
import pandas.stats.common as common
1717
import pandas.stats.math as math
@@ -32,6 +32,8 @@ class OLS(object):
3232
nw_lags: None or int
3333
Number of Newey-West lags.
3434
"""
35+
_panel_model = False
36+
3537
def __init__(self, y, x, intercept=True, weights=None, nw_lags=None,
3638
nw_overlap=False):
3739
import scikits.statsmodels.api as sm
@@ -757,7 +759,7 @@ def _cum_xx(self, x):
757759
cum_xx = []
758760

759761
slicer = lambda df, dt: df.truncate(dt, dt).values
760-
if isinstance(x, DataFrame) and not isinstance(x, LongPanel):
762+
if not self._panel_model:
761763
_get_index = x.index.get_loc
762764
def slicer(df, dt):
763765
i = _get_index(dt)
@@ -782,7 +784,7 @@ def _cum_xy(self, x, y):
782784
cum_xy = []
783785

784786
x_slicer = lambda df, dt: df.truncate(dt, dt).values
785-
if isinstance(x, DataFrame) and not isinstance(x, LongPanel):
787+
if not self._panel_model:
786788
_get_index = x.index.get_loc
787789
def x_slicer(df, dt):
788790
i = _get_index(dt)

pandas/stats/plm.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from pandas.core.series import Series
1616
from pandas.core.sparse import SparsePanel
1717
from pandas.stats.ols import OLS, MovingOLS
18-
import pandas.stats.common as common
18+
import pandas.stats.common as com
1919
import pandas.stats.math as math
2020
from pandas.util.decorators import cache_readonly
2121

@@ -24,6 +24,8 @@ class PanelOLS(OLS):
2424
2525
See ols function docs
2626
"""
27+
_panel_model = True
28+
2729
def __init__(self, y, x, weights=None, intercept=True, nw_lags=None,
2830
entity_effects=False, time_effects=False, x_effects=None,
2931
cluster=None, dropped_dummies=None, verbose=False,
@@ -39,14 +41,14 @@ def __init__(self, y, x, weights=None, intercept=True, nw_lags=None,
3941
self._time_effects = time_effects
4042
self._x_effects = x_effects
4143
self._dropped_dummies = dropped_dummies or {}
42-
self._cluster = common._get_cluster_type(cluster)
44+
self._cluster = com._get_cluster_type(cluster)
4345
self._verbose = verbose
4446

4547
(self._x, self._x_trans,
4648
self._x_filtered, self._y,
4749
self._y_trans) = self._prepare_data()
4850

49-
self._index = self._x.major_axis
51+
self._index = self._x.index.levels[0]
5052

5153
self._T = len(self._index)
5254

@@ -470,6 +472,8 @@ class MovingPanelOLS(MovingOLS, PanelOLS):
470472
471473
See ols function docs
472474
"""
475+
_panel_model = True
476+
473477
def __init__(self, y, x, weights=None,
474478
window_type='expanding', window=None,
475479
min_periods=None,
@@ -499,7 +503,7 @@ def __init__(self, y, x, weights=None,
499503
self._set_window(window_type, window, min_periods)
500504

501505
if min_obs is None:
502-
min_obs = len(self._x.items) + 1
506+
min_obs = len(self._x.columns) + 1
503507

504508
self._min_obs = min_obs
505509

@@ -553,7 +557,7 @@ def _var_beta_raw(self):
553557
x = self._x
554558
y = self._y
555559

556-
dates = x.major_axis
560+
dates = x.index.levels[0]
557561

558562
cluster_axis = None
559563
if self._cluster == 'time':
@@ -639,7 +643,7 @@ def _enough_obs(self):
639643
# XXX: what's the best way to determine where to start?
640644
# TODO: write unit tests for this
641645

642-
rank_threshold = len(self._x.items) + 1
646+
rank_threshold = len(self._x.columns) + 1
643647
if self._min_obs < rank_threshold: # pragma: no cover
644648
warnings.warn('min_obs is smaller than rank of X matrix')
645649

@@ -763,7 +767,7 @@ def _var_beta_panel(y, x, beta, xx, rmse, cluster_axis,
763767
nw_lags = 0
764768

765769
xox = 0
766-
for i in range(len(x.major_axis)):
770+
for i in range(len(x.index.levels[0])):
767771
xox += math.newey_west(m[i : i + 1], nw_lags,
768772
nobs, df, nw_overlap)
769773

0 commit comments

Comments
 (0)