Skip to content

Commit fbbcc10

Browse files
authored
TST/DEPR: fix bunch of Panel deprecation warnings (pandas-dev#15965)
* TST: remove using .to_datetime w/Timestamp as deprecated * TST: split some sorting tests * fix isnan comparison deprecation warning * TST/DEPR: catch Panel deprecation warnings * close files in tests pickles with compression
1 parent 838e09c commit fbbcc10

27 files changed

+667
-554
lines changed

pandas/tests/computation/test_eval.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import warnings
2+
from warnings import catch_warnings
23
import operator
34
from itertools import product
45

@@ -1130,11 +1131,12 @@ def test_bool_ops_with_constants(self):
11301131
self.assertEqual(res, exp)
11311132

11321133
def test_panel_fails(self):
1133-
x = Panel(randn(3, 4, 5))
1134-
y = Series(randn(10))
1135-
with pytest.raises(NotImplementedError):
1136-
self.eval('x + y',
1137-
local_dict={'x': x, 'y': y})
1134+
with catch_warnings(record=True):
1135+
x = Panel(randn(3, 4, 5))
1136+
y = Series(randn(10))
1137+
with pytest.raises(NotImplementedError):
1138+
self.eval('x + y',
1139+
local_dict={'x': x, 'y': y})
11381140

11391141
def test_4d_ndarray_fails(self):
11401142
x = randn(3, 4, 5, 6)

pandas/tests/frame/test_reshape.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import print_function
44

5+
from warnings import catch_warnings
56
from datetime import datetime
67
import itertools
78

@@ -53,11 +54,12 @@ def test_pivot(self):
5354
self.assertEqual(pivoted.index.name, 'index')
5455
self.assertEqual(pivoted.columns.names, (None, 'columns'))
5556

56-
# pivot multiple columns
57-
wp = tm.makePanel()
58-
lp = wp.to_frame()
59-
df = lp.reset_index()
60-
assert_frame_equal(df.pivot('major', 'minor'), lp.unstack())
57+
with catch_warnings(record=True):
58+
# pivot multiple columns
59+
wp = tm.makePanel()
60+
lp = wp.to_frame()
61+
df = lp.reset_index()
62+
assert_frame_equal(df.pivot('major', 'minor'), lp.unstack())
6163

6264
def test_pivot_duplicates(self):
6365
data = DataFrame({'a': ['bar', 'bar', 'foo', 'foo', 'foo'],

pandas/tests/frame/test_subclass.py

+20-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import print_function
44

5+
from warnings import catch_warnings
56
import numpy as np
67

78
from pandas import DataFrame, Series, MultiIndex, Panel
@@ -128,24 +129,25 @@ def test_indexing_sliced(self):
128129
def test_to_panel_expanddim(self):
129130
# GH 9762
130131

131-
class SubclassedFrame(DataFrame):
132-
133-
@property
134-
def _constructor_expanddim(self):
135-
return SubclassedPanel
136-
137-
class SubclassedPanel(Panel):
138-
pass
139-
140-
index = MultiIndex.from_tuples([(0, 0), (0, 1), (0, 2)])
141-
df = SubclassedFrame({'X': [1, 2, 3], 'Y': [4, 5, 6]}, index=index)
142-
result = df.to_panel()
143-
self.assertTrue(isinstance(result, SubclassedPanel))
144-
expected = SubclassedPanel([[[1, 2, 3]], [[4, 5, 6]]],
145-
items=['X', 'Y'], major_axis=[0],
146-
minor_axis=[0, 1, 2],
147-
dtype='int64')
148-
tm.assert_panel_equal(result, expected)
132+
with catch_warnings(record=True):
133+
class SubclassedFrame(DataFrame):
134+
135+
@property
136+
def _constructor_expanddim(self):
137+
return SubclassedPanel
138+
139+
class SubclassedPanel(Panel):
140+
pass
141+
142+
index = MultiIndex.from_tuples([(0, 0), (0, 1), (0, 2)])
143+
df = SubclassedFrame({'X': [1, 2, 3], 'Y': [4, 5, 6]}, index=index)
144+
result = df.to_panel()
145+
self.assertTrue(isinstance(result, SubclassedPanel))
146+
expected = SubclassedPanel([[[1, 2, 3]], [[4, 5, 6]]],
147+
items=['X', 'Y'], major_axis=[0],
148+
minor_axis=[0, 1, 2],
149+
dtype='int64')
150+
tm.assert_panel_equal(result, expected)
149151

150152
def test_subclass_attr_err_propagation(self):
151153
# GH 11808

pandas/tests/groupby/test_groupby.py

+59-52
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import print_function
33

4+
from warnings import catch_warnings
45
from string import ascii_lowercase
56
from datetime import datetime
67
from numpy import nan
@@ -814,12 +815,14 @@ def f(grp):
814815
assert_series_equal(result, e)
815816

816817
def test_get_group(self):
817-
wp = tm.makePanel()
818-
grouped = wp.groupby(lambda x: x.month, axis='major')
818+
with catch_warnings(record=True):
819+
wp = tm.makePanel()
820+
grouped = wp.groupby(lambda x: x.month, axis='major')
819821

820-
gp = grouped.get_group(1)
821-
expected = wp.reindex(major=[x for x in wp.major_axis if x.month == 1])
822-
assert_panel_equal(gp, expected)
822+
gp = grouped.get_group(1)
823+
expected = wp.reindex(
824+
major=[x for x in wp.major_axis if x.month == 1])
825+
assert_panel_equal(gp, expected)
823826

824827
# GH 5267
825828
# be datelike friendly
@@ -1317,16 +1320,17 @@ def test_multi_iter_frame(self):
13171320
pass
13181321

13191322
def test_multi_iter_panel(self):
1320-
wp = tm.makePanel()
1321-
grouped = wp.groupby([lambda x: x.month, lambda x: x.weekday()],
1322-
axis=1)
1323-
1324-
for (month, wd), group in grouped:
1325-
exp_axis = [x
1326-
for x in wp.major_axis
1327-
if x.month == month and x.weekday() == wd]
1328-
expected = wp.reindex(major=exp_axis)
1329-
assert_panel_equal(group, expected)
1323+
with catch_warnings(record=True):
1324+
wp = tm.makePanel()
1325+
grouped = wp.groupby([lambda x: x.month, lambda x: x.weekday()],
1326+
axis=1)
1327+
1328+
for (month, wd), group in grouped:
1329+
exp_axis = [x
1330+
for x in wp.major_axis
1331+
if x.month == month and x.weekday() == wd]
1332+
expected = wp.reindex(major=exp_axis)
1333+
assert_panel_equal(group, expected)
13301334

13311335
def test_multi_func(self):
13321336
col1 = self.df['A']
@@ -1387,25 +1391,26 @@ def test_groupby_multiple_columns(self):
13871391

13881392
def _check_op(op):
13891393

1390-
result1 = op(grouped)
1391-
1392-
expected = defaultdict(dict)
1393-
for n1, gp1 in data.groupby('A'):
1394-
for n2, gp2 in gp1.groupby('B'):
1395-
expected[n1][n2] = op(gp2.loc[:, ['C', 'D']])
1396-
expected = dict((k, DataFrame(v))
1397-
for k, v in compat.iteritems(expected))
1398-
expected = Panel.fromDict(expected).swapaxes(0, 1)
1399-
expected.major_axis.name, expected.minor_axis.name = 'A', 'B'
1400-
1401-
# a little bit crude
1402-
for col in ['C', 'D']:
1403-
result_col = op(grouped[col])
1404-
exp = expected[col]
1405-
pivoted = result1[col].unstack()
1406-
pivoted2 = result_col.unstack()
1407-
assert_frame_equal(pivoted.reindex_like(exp), exp)
1408-
assert_frame_equal(pivoted2.reindex_like(exp), exp)
1394+
with catch_warnings(record=True):
1395+
result1 = op(grouped)
1396+
1397+
expected = defaultdict(dict)
1398+
for n1, gp1 in data.groupby('A'):
1399+
for n2, gp2 in gp1.groupby('B'):
1400+
expected[n1][n2] = op(gp2.loc[:, ['C', 'D']])
1401+
expected = dict((k, DataFrame(v))
1402+
for k, v in compat.iteritems(expected))
1403+
expected = Panel.fromDict(expected).swapaxes(0, 1)
1404+
expected.major_axis.name, expected.minor_axis.name = 'A', 'B'
1405+
1406+
# a little bit crude
1407+
for col in ['C', 'D']:
1408+
result_col = op(grouped[col])
1409+
exp = expected[col]
1410+
pivoted = result1[col].unstack()
1411+
pivoted2 = result_col.unstack()
1412+
assert_frame_equal(pivoted.reindex_like(exp), exp)
1413+
assert_frame_equal(pivoted2.reindex_like(exp), exp)
14091414

14101415
_check_op(lambda x: x.sum())
14111416
_check_op(lambda x: x.mean())
@@ -2980,8 +2985,9 @@ def test_dictify(self):
29802985

29812986
def test_sparse_friendly(self):
29822987
sdf = self.df[['C', 'D']].to_sparse()
2983-
panel = tm.makePanel()
2984-
tm.add_nans(panel)
2988+
with catch_warnings(record=True):
2989+
panel = tm.makePanel()
2990+
tm.add_nans(panel)
29852991

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

29993005
def test_panel_groupby(self):
3000-
self.panel = tm.makePanel()
3001-
tm.add_nans(self.panel)
3002-
grouped = self.panel.groupby({'ItemA': 0, 'ItemB': 0, 'ItemC': 1},
3003-
axis='items')
3004-
agged = grouped.mean()
3005-
agged2 = grouped.agg(lambda x: x.mean('items'))
3006+
with catch_warnings(record=True):
3007+
self.panel = tm.makePanel()
3008+
tm.add_nans(self.panel)
3009+
grouped = self.panel.groupby({'ItemA': 0, 'ItemB': 0, 'ItemC': 1},
3010+
axis='items')
3011+
agged = grouped.mean()
3012+
agged2 = grouped.agg(lambda x: x.mean('items'))
30063013

3007-
tm.assert_panel_equal(agged, agged2)
3014+
tm.assert_panel_equal(agged, agged2)
30083015

3009-
self.assert_index_equal(agged.items, Index([0, 1]))
3016+
self.assert_index_equal(agged.items, Index([0, 1]))
30103017

3011-
grouped = self.panel.groupby(lambda x: x.month, axis='major')
3012-
agged = grouped.mean()
3018+
grouped = self.panel.groupby(lambda x: x.month, axis='major')
3019+
agged = grouped.mean()
30133020

3014-
exp = Index(sorted(list(set(self.panel.major_axis.month))))
3015-
self.assert_index_equal(agged.major_axis, exp)
3021+
exp = Index(sorted(list(set(self.panel.major_axis.month))))
3022+
self.assert_index_equal(agged.major_axis, exp)
30163023

3017-
grouped = self.panel.groupby({'A': 0, 'B': 0, 'C': 1, 'D': 1},
3018-
axis='minor')
3019-
agged = grouped.mean()
3020-
self.assert_index_equal(agged.minor_axis, Index([0, 1]))
3024+
grouped = self.panel.groupby({'A': 0, 'B': 0, 'C': 1, 'D': 1},
3025+
axis='minor')
3026+
agged = grouped.mean()
3027+
self.assert_index_equal(agged.minor_axis, Index([0, 1]))
30213028

30223029
def test_groupby_2d_malformed(self):
30233030
d = DataFrame(index=lrange(2))

pandas/tests/indexes/datetimes/test_tools.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1533,7 +1533,8 @@ def units_from_epochs():
15331533
return list(range(5))
15341534

15351535

1536-
@pytest.fixture(params=[epoch_1960(), epoch_1960().to_datetime(),
1536+
@pytest.fixture(params=[epoch_1960(),
1537+
epoch_1960().to_pydatetime(),
15371538
epoch_1960().to_datetime64(),
15381539
str(epoch_1960())])
15391540
def epochs(request):

pandas/tests/indexing/common.py

+37-20
Original file line numberDiff line numberDiff line change
@@ -37,54 +37,61 @@ def setUp(self):
3737
self.frame_ints = DataFrame(np.random.randn(4, 4),
3838
index=lrange(0, 8, 2),
3939
columns=lrange(0, 12, 3))
40-
self.panel_ints = Panel(np.random.rand(4, 4, 4),
41-
items=lrange(0, 8, 2),
42-
major_axis=lrange(0, 12, 3),
43-
minor_axis=lrange(0, 16, 4))
40+
with catch_warnings(record=True):
41+
self.panel_ints = Panel(np.random.rand(4, 4, 4),
42+
items=lrange(0, 8, 2),
43+
major_axis=lrange(0, 12, 3),
44+
minor_axis=lrange(0, 16, 4))
4445

4546
self.series_uints = Series(np.random.rand(4),
4647
index=UInt64Index(lrange(0, 8, 2)))
4748
self.frame_uints = DataFrame(np.random.randn(4, 4),
4849
index=UInt64Index(lrange(0, 8, 2)),
4950
columns=UInt64Index(lrange(0, 12, 3)))
50-
self.panel_uints = Panel(np.random.rand(4, 4, 4),
51-
items=UInt64Index(lrange(0, 8, 2)),
52-
major_axis=UInt64Index(lrange(0, 12, 3)),
53-
minor_axis=UInt64Index(lrange(0, 16, 4)))
51+
with catch_warnings(record=True):
52+
self.panel_uints = Panel(np.random.rand(4, 4, 4),
53+
items=UInt64Index(lrange(0, 8, 2)),
54+
major_axis=UInt64Index(lrange(0, 12, 3)),
55+
minor_axis=UInt64Index(lrange(0, 16, 4)))
5456

5557
self.series_labels = Series(np.random.randn(4), index=list('abcd'))
5658
self.frame_labels = DataFrame(np.random.randn(4, 4),
5759
index=list('abcd'), columns=list('ABCD'))
58-
self.panel_labels = Panel(np.random.randn(4, 4, 4),
59-
items=list('abcd'),
60-
major_axis=list('ABCD'),
61-
minor_axis=list('ZYXW'))
60+
with catch_warnings(record=True):
61+
self.panel_labels = Panel(np.random.randn(4, 4, 4),
62+
items=list('abcd'),
63+
major_axis=list('ABCD'),
64+
minor_axis=list('ZYXW'))
6265

6366
self.series_mixed = Series(np.random.randn(4), index=[2, 4, 'null', 8])
6467
self.frame_mixed = DataFrame(np.random.randn(4, 4),
6568
index=[2, 4, 'null', 8])
66-
self.panel_mixed = Panel(np.random.randn(4, 4, 4),
67-
items=[2, 4, 'null', 8])
69+
with catch_warnings(record=True):
70+
self.panel_mixed = Panel(np.random.randn(4, 4, 4),
71+
items=[2, 4, 'null', 8])
6872

6973
self.series_ts = Series(np.random.randn(4),
7074
index=date_range('20130101', periods=4))
7175
self.frame_ts = DataFrame(np.random.randn(4, 4),
7276
index=date_range('20130101', periods=4))
73-
self.panel_ts = Panel(np.random.randn(4, 4, 4),
74-
items=date_range('20130101', periods=4))
77+
with catch_warnings(record=True):
78+
self.panel_ts = Panel(np.random.randn(4, 4, 4),
79+
items=date_range('20130101', periods=4))
7580

7681
dates_rev = (date_range('20130101', periods=4)
7782
.sort_values(ascending=False))
7883
self.series_ts_rev = Series(np.random.randn(4),
7984
index=dates_rev)
8085
self.frame_ts_rev = DataFrame(np.random.randn(4, 4),
8186
index=dates_rev)
82-
self.panel_ts_rev = Panel(np.random.randn(4, 4, 4),
83-
items=dates_rev)
87+
with catch_warnings(record=True):
88+
self.panel_ts_rev = Panel(np.random.randn(4, 4, 4),
89+
items=dates_rev)
8490

8591
self.frame_empty = DataFrame({})
8692
self.series_empty = Series({})
87-
self.panel_empty = Panel({})
93+
with catch_warnings(record=True):
94+
self.panel_empty = Panel({})
8895

8996
# form agglomerates
9097
for o in self._objs:
@@ -255,8 +262,18 @@ def _print(result, error=None):
255262
continue
256263

257264
obj = d[t]
258-
if obj is not None:
265+
if obj is None:
266+
continue
267+
268+
def _call(obj=obj):
259269
obj = obj.copy()
260270

261271
k2 = key2
262272
_eq(t, o, a, obj, key1, k2)
273+
274+
# Panel deprecations
275+
if isinstance(obj, Panel):
276+
with catch_warnings(record=True):
277+
_call()
278+
else:
279+
_call()

0 commit comments

Comments
 (0)