Skip to content

Commit c39c1dd

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

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

pandas/core/indexing.py

Lines changed: 4 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 3 deletions
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
@@ -753,7 +755,7 @@ def _cum_xx(self, x):
753755
cum_xx = []
754756

755757
slicer = lambda df, dt: df.truncate(dt, dt).values
756-
if isinstance(x, DataFrame) and not isinstance(x, LongPanel):
758+
if not self._panel_model:
757759
_get_index = x.index.get_loc
758760
def slicer(df, dt):
759761
i = _get_index(dt)
@@ -778,7 +780,7 @@ def _cum_xy(self, x, y):
778780
cum_xy = []
779781

780782
x_slicer = lambda df, dt: df.truncate(dt, dt).values
781-
if isinstance(x, DataFrame) and not isinstance(x, LongPanel):
783+
if not self._panel_model:
782784
_get_index = x.index.get_loc
783785
def x_slicer(df, dt):
784786
i = _get_index(dt)

pandas/stats/plm.py

Lines changed: 11 additions & 7 deletions
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

@@ -461,6 +463,8 @@ class MovingPanelOLS(MovingOLS, PanelOLS):
461463
462464
See ols function docs
463465
"""
466+
_panel_model = True
467+
464468
def __init__(self, y, x, weights=None,
465469
window_type='expanding', window=None,
466470
min_periods=None,
@@ -490,7 +494,7 @@ def __init__(self, y, x, weights=None,
490494
self._set_window(window_type, window, min_periods)
491495

492496
if min_obs is None:
493-
min_obs = len(self._x.items) + 1
497+
min_obs = len(self._x.columns) + 1
494498

495499
self._min_obs = min_obs
496500

@@ -544,7 +548,7 @@ def _var_beta_raw(self):
544548
x = self._x
545549
y = self._y
546550

547-
dates = x.major_axis
551+
dates = x.index.levels[0]
548552

549553
cluster_axis = None
550554
if self._cluster == 'time':
@@ -630,7 +634,7 @@ def _enough_obs(self):
630634
# XXX: what's the best way to determine where to start?
631635
# TODO: write unit tests for this
632636

633-
rank_threshold = len(self._x.items) + 1
637+
rank_threshold = len(self._x.columns) + 1
634638
if self._min_obs < rank_threshold: # pragma: no cover
635639
warnings.warn('min_obs is smaller than rank of X matrix')
636640

@@ -754,7 +758,7 @@ def _var_beta_panel(y, x, beta, xx, rmse, cluster_axis,
754758
nw_lags = 0
755759

756760
xox = 0
757-
for i in range(len(x.major_axis)):
761+
for i in range(len(x.index.levels[0])):
758762
xox += math.newey_west(m[i : i + 1], nw_lags,
759763
nobs, df, nw_overlap)
760764

0 commit comments

Comments
 (0)