Skip to content

TST/DEPR: fix bunch of Panel deprecation warnings #15965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 11, 2017
12 changes: 7 additions & 5 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import warnings
from warnings import catch_warnings
import operator
from itertools import product

Expand Down Expand Up @@ -1130,11 +1131,12 @@ def test_bool_ops_with_constants(self):
self.assertEqual(res, exp)

def test_panel_fails(self):
x = Panel(randn(3, 4, 5))
y = Series(randn(10))
with pytest.raises(NotImplementedError):
self.eval('x + y',
local_dict={'x': x, 'y': y})
with catch_warnings(record=True):
x = Panel(randn(3, 4, 5))
y = Series(randn(10))
with pytest.raises(NotImplementedError):
self.eval('x + y',
local_dict={'x': x, 'y': y})

def test_4d_ndarray_fails(self):
x = randn(3, 4, 5, 6)
Expand Down
12 changes: 7 additions & 5 deletions pandas/tests/frame/test_reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import print_function

from warnings import catch_warnings
from datetime import datetime
import itertools

Expand Down Expand Up @@ -53,11 +54,12 @@ def test_pivot(self):
self.assertEqual(pivoted.index.name, 'index')
self.assertEqual(pivoted.columns.names, (None, 'columns'))

# pivot multiple columns
wp = tm.makePanel()
lp = wp.to_frame()
df = lp.reset_index()
assert_frame_equal(df.pivot('major', 'minor'), lp.unstack())
with catch_warnings(record=True):
# pivot multiple columns
wp = tm.makePanel()
lp = wp.to_frame()
df = lp.reset_index()
assert_frame_equal(df.pivot('major', 'minor'), lp.unstack())

def test_pivot_duplicates(self):
data = DataFrame({'a': ['bar', 'bar', 'foo', 'foo', 'foo'],
Expand Down
38 changes: 20 additions & 18 deletions pandas/tests/frame/test_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import print_function

from warnings import catch_warnings
import numpy as np

from pandas import DataFrame, Series, MultiIndex, Panel
Expand Down Expand Up @@ -128,24 +129,25 @@ def test_indexing_sliced(self):
def test_to_panel_expanddim(self):
# GH 9762

class SubclassedFrame(DataFrame):

@property
def _constructor_expanddim(self):
return SubclassedPanel

class SubclassedPanel(Panel):
pass

index = MultiIndex.from_tuples([(0, 0), (0, 1), (0, 2)])
df = SubclassedFrame({'X': [1, 2, 3], 'Y': [4, 5, 6]}, index=index)
result = df.to_panel()
self.assertTrue(isinstance(result, SubclassedPanel))
expected = SubclassedPanel([[[1, 2, 3]], [[4, 5, 6]]],
items=['X', 'Y'], major_axis=[0],
minor_axis=[0, 1, 2],
dtype='int64')
tm.assert_panel_equal(result, expected)
with catch_warnings(record=True):
class SubclassedFrame(DataFrame):

@property
def _constructor_expanddim(self):
return SubclassedPanel

class SubclassedPanel(Panel):
pass

index = MultiIndex.from_tuples([(0, 0), (0, 1), (0, 2)])
df = SubclassedFrame({'X': [1, 2, 3], 'Y': [4, 5, 6]}, index=index)
result = df.to_panel()
self.assertTrue(isinstance(result, SubclassedPanel))
expected = SubclassedPanel([[[1, 2, 3]], [[4, 5, 6]]],
items=['X', 'Y'], major_axis=[0],
minor_axis=[0, 1, 2],
dtype='int64')
tm.assert_panel_equal(result, expected)

def test_subclass_attr_err_propagation(self):
# GH 11808
Expand Down
111 changes: 59 additions & 52 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import print_function

from warnings import catch_warnings
from string import ascii_lowercase
from datetime import datetime
from numpy import nan
Expand Down Expand Up @@ -814,12 +815,14 @@ def f(grp):
assert_series_equal(result, e)

def test_get_group(self):
wp = tm.makePanel()
grouped = wp.groupby(lambda x: x.month, axis='major')
with catch_warnings(record=True):
wp = tm.makePanel()
grouped = wp.groupby(lambda x: x.month, axis='major')

gp = grouped.get_group(1)
expected = wp.reindex(major=[x for x in wp.major_axis if x.month == 1])
assert_panel_equal(gp, expected)
gp = grouped.get_group(1)
expected = wp.reindex(
major=[x for x in wp.major_axis if x.month == 1])
assert_panel_equal(gp, expected)

# GH 5267
# be datelike friendly
Expand Down Expand Up @@ -1317,16 +1320,17 @@ def test_multi_iter_frame(self):
pass

def test_multi_iter_panel(self):
wp = tm.makePanel()
grouped = wp.groupby([lambda x: x.month, lambda x: x.weekday()],
axis=1)

for (month, wd), group in grouped:
exp_axis = [x
for x in wp.major_axis
if x.month == month and x.weekday() == wd]
expected = wp.reindex(major=exp_axis)
assert_panel_equal(group, expected)
with catch_warnings(record=True):
wp = tm.makePanel()
grouped = wp.groupby([lambda x: x.month, lambda x: x.weekday()],
axis=1)

for (month, wd), group in grouped:
exp_axis = [x
for x in wp.major_axis
if x.month == month and x.weekday() == wd]
expected = wp.reindex(major=exp_axis)
assert_panel_equal(group, expected)

def test_multi_func(self):
col1 = self.df['A']
Expand Down Expand Up @@ -1387,25 +1391,26 @@ def test_groupby_multiple_columns(self):

def _check_op(op):

result1 = op(grouped)

expected = defaultdict(dict)
for n1, gp1 in data.groupby('A'):
for n2, gp2 in gp1.groupby('B'):
expected[n1][n2] = op(gp2.loc[:, ['C', 'D']])
expected = dict((k, DataFrame(v))
for k, v in compat.iteritems(expected))
expected = Panel.fromDict(expected).swapaxes(0, 1)
expected.major_axis.name, expected.minor_axis.name = 'A', 'B'

# a little bit crude
for col in ['C', 'D']:
result_col = op(grouped[col])
exp = expected[col]
pivoted = result1[col].unstack()
pivoted2 = result_col.unstack()
assert_frame_equal(pivoted.reindex_like(exp), exp)
assert_frame_equal(pivoted2.reindex_like(exp), exp)
with catch_warnings(record=True):
result1 = op(grouped)

expected = defaultdict(dict)
for n1, gp1 in data.groupby('A'):
for n2, gp2 in gp1.groupby('B'):
expected[n1][n2] = op(gp2.loc[:, ['C', 'D']])
expected = dict((k, DataFrame(v))
for k, v in compat.iteritems(expected))
expected = Panel.fromDict(expected).swapaxes(0, 1)
expected.major_axis.name, expected.minor_axis.name = 'A', 'B'

# a little bit crude
for col in ['C', 'D']:
result_col = op(grouped[col])
exp = expected[col]
pivoted = result1[col].unstack()
pivoted2 = result_col.unstack()
assert_frame_equal(pivoted.reindex_like(exp), exp)
assert_frame_equal(pivoted2.reindex_like(exp), exp)

_check_op(lambda x: x.sum())
_check_op(lambda x: x.mean())
Expand Down Expand Up @@ -2980,8 +2985,9 @@ def test_dictify(self):

def test_sparse_friendly(self):
sdf = self.df[['C', 'D']].to_sparse()
panel = tm.makePanel()
tm.add_nans(panel)
with catch_warnings(record=True):
panel = tm.makePanel()
tm.add_nans(panel)

def _check_work(gp):
gp.mean()
Expand All @@ -2997,27 +3003,28 @@ def _check_work(gp):
# _check_work(panel.groupby(lambda x: x.month, axis=1))

def test_panel_groupby(self):
self.panel = tm.makePanel()
tm.add_nans(self.panel)
grouped = self.panel.groupby({'ItemA': 0, 'ItemB': 0, 'ItemC': 1},
axis='items')
agged = grouped.mean()
agged2 = grouped.agg(lambda x: x.mean('items'))
with catch_warnings(record=True):
self.panel = tm.makePanel()
tm.add_nans(self.panel)
grouped = self.panel.groupby({'ItemA': 0, 'ItemB': 0, 'ItemC': 1},
axis='items')
agged = grouped.mean()
agged2 = grouped.agg(lambda x: x.mean('items'))

tm.assert_panel_equal(agged, agged2)
tm.assert_panel_equal(agged, agged2)

self.assert_index_equal(agged.items, Index([0, 1]))
self.assert_index_equal(agged.items, Index([0, 1]))

grouped = self.panel.groupby(lambda x: x.month, axis='major')
agged = grouped.mean()
grouped = self.panel.groupby(lambda x: x.month, axis='major')
agged = grouped.mean()

exp = Index(sorted(list(set(self.panel.major_axis.month))))
self.assert_index_equal(agged.major_axis, exp)
exp = Index(sorted(list(set(self.panel.major_axis.month))))
self.assert_index_equal(agged.major_axis, exp)

grouped = self.panel.groupby({'A': 0, 'B': 0, 'C': 1, 'D': 1},
axis='minor')
agged = grouped.mean()
self.assert_index_equal(agged.minor_axis, Index([0, 1]))
grouped = self.panel.groupby({'A': 0, 'B': 0, 'C': 1, 'D': 1},
axis='minor')
agged = grouped.mean()
self.assert_index_equal(agged.minor_axis, Index([0, 1]))

def test_groupby_2d_malformed(self):
d = DataFrame(index=lrange(2))
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,8 @@ def units_from_epochs():
return list(range(5))


@pytest.fixture(params=[epoch_1960(), epoch_1960().to_datetime(),
@pytest.fixture(params=[epoch_1960(),
epoch_1960().to_pydatetime(),
epoch_1960().to_datetime64(),
str(epoch_1960())])
def epochs(request):
Expand Down
57 changes: 37 additions & 20 deletions pandas/tests/indexing/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,54 +37,61 @@ def setUp(self):
self.frame_ints = DataFrame(np.random.randn(4, 4),
index=lrange(0, 8, 2),
columns=lrange(0, 12, 3))
self.panel_ints = Panel(np.random.rand(4, 4, 4),
items=lrange(0, 8, 2),
major_axis=lrange(0, 12, 3),
minor_axis=lrange(0, 16, 4))
with catch_warnings(record=True):
self.panel_ints = Panel(np.random.rand(4, 4, 4),
items=lrange(0, 8, 2),
major_axis=lrange(0, 12, 3),
minor_axis=lrange(0, 16, 4))

self.series_uints = Series(np.random.rand(4),
index=UInt64Index(lrange(0, 8, 2)))
self.frame_uints = DataFrame(np.random.randn(4, 4),
index=UInt64Index(lrange(0, 8, 2)),
columns=UInt64Index(lrange(0, 12, 3)))
self.panel_uints = Panel(np.random.rand(4, 4, 4),
items=UInt64Index(lrange(0, 8, 2)),
major_axis=UInt64Index(lrange(0, 12, 3)),
minor_axis=UInt64Index(lrange(0, 16, 4)))
with catch_warnings(record=True):
self.panel_uints = Panel(np.random.rand(4, 4, 4),
items=UInt64Index(lrange(0, 8, 2)),
major_axis=UInt64Index(lrange(0, 12, 3)),
minor_axis=UInt64Index(lrange(0, 16, 4)))

self.series_labels = Series(np.random.randn(4), index=list('abcd'))
self.frame_labels = DataFrame(np.random.randn(4, 4),
index=list('abcd'), columns=list('ABCD'))
self.panel_labels = Panel(np.random.randn(4, 4, 4),
items=list('abcd'),
major_axis=list('ABCD'),
minor_axis=list('ZYXW'))
with catch_warnings(record=True):
self.panel_labels = Panel(np.random.randn(4, 4, 4),
items=list('abcd'),
major_axis=list('ABCD'),
minor_axis=list('ZYXW'))

self.series_mixed = Series(np.random.randn(4), index=[2, 4, 'null', 8])
self.frame_mixed = DataFrame(np.random.randn(4, 4),
index=[2, 4, 'null', 8])
self.panel_mixed = Panel(np.random.randn(4, 4, 4),
items=[2, 4, 'null', 8])
with catch_warnings(record=True):
self.panel_mixed = Panel(np.random.randn(4, 4, 4),
items=[2, 4, 'null', 8])

self.series_ts = Series(np.random.randn(4),
index=date_range('20130101', periods=4))
self.frame_ts = DataFrame(np.random.randn(4, 4),
index=date_range('20130101', periods=4))
self.panel_ts = Panel(np.random.randn(4, 4, 4),
items=date_range('20130101', periods=4))
with catch_warnings(record=True):
self.panel_ts = Panel(np.random.randn(4, 4, 4),
items=date_range('20130101', periods=4))

dates_rev = (date_range('20130101', periods=4)
.sort_values(ascending=False))
self.series_ts_rev = Series(np.random.randn(4),
index=dates_rev)
self.frame_ts_rev = DataFrame(np.random.randn(4, 4),
index=dates_rev)
self.panel_ts_rev = Panel(np.random.randn(4, 4, 4),
items=dates_rev)
with catch_warnings(record=True):
self.panel_ts_rev = Panel(np.random.randn(4, 4, 4),
items=dates_rev)

self.frame_empty = DataFrame({})
self.series_empty = Series({})
self.panel_empty = Panel({})
with catch_warnings(record=True):
self.panel_empty = Panel({})

# form agglomerates
for o in self._objs:
Expand Down Expand Up @@ -255,8 +262,18 @@ def _print(result, error=None):
continue

obj = d[t]
if obj is not None:
if obj is None:
continue

def _call(obj=obj):
obj = obj.copy()

k2 = key2
_eq(t, o, a, obj, key1, k2)

# Panel deprecations
if isinstance(obj, Panel):
with catch_warnings(record=True):
_call()
else:
_call()
Loading