3
3
4
4
from operator import methodcaller
5
5
from copy import copy , deepcopy
6
+ from warnings import catch_warnings
7
+
6
8
import pytest
7
9
import numpy as np
8
10
from numpy import nan
@@ -1570,17 +1572,18 @@ def test_to_xarray(self):
1570
1572
tm ._skip_if_no_xarray ()
1571
1573
from xarray import DataArray
1572
1574
1573
- p = tm .makePanel ()
1575
+ with catch_warnings (record = True ):
1576
+ p = tm .makePanel ()
1574
1577
1575
- result = p .to_xarray ()
1576
- self .assertIsInstance (result , DataArray )
1577
- self .assertEqual (len (result .coords ), 3 )
1578
- assert_almost_equal (list (result .coords .keys ()),
1579
- ['items' , 'major_axis' , 'minor_axis' ])
1580
- self .assertEqual (len (result .dims ), 3 )
1578
+ result = p .to_xarray ()
1579
+ self .assertIsInstance (result , DataArray )
1580
+ self .assertEqual (len (result .coords ), 3 )
1581
+ assert_almost_equal (list (result .coords .keys ()),
1582
+ ['items' , 'major_axis' , 'minor_axis' ])
1583
+ self .assertEqual (len (result .dims ), 3 )
1581
1584
1582
- # idempotency
1583
- assert_panel_equal (result .to_pandas (), p )
1585
+ # idempotency
1586
+ assert_panel_equal (result .to_pandas (), p )
1584
1587
1585
1588
1586
1589
class TestPanel4D (tm .TestCase , Generic ):
@@ -1590,15 +1593,12 @@ class TestPanel4D(tm.TestCase, Generic):
1590
1593
def test_sample (self ):
1591
1594
pytest .skip ("sample on Panel4D" )
1592
1595
1593
- def test_copy_and_deepcopy (self ):
1594
- pytest .skip ("copy_and_deepcopy on Panel4D" )
1595
-
1596
1596
def test_to_xarray (self ):
1597
1597
1598
1598
tm ._skip_if_no_xarray ()
1599
1599
from xarray import DataArray
1600
1600
1601
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1601
+ with catch_warnings ( record = True ):
1602
1602
p = tm .makePanel4D ()
1603
1603
1604
1604
result = p .to_xarray ()
@@ -1624,12 +1624,20 @@ def test_to_xarray(self):
1624
1624
'test_stat_unexpected_keyword' , 'test_api_compat' ,
1625
1625
'test_stat_non_defaults_args' ,
1626
1626
'test_clip' , 'test_truncate_out_of_bounds' , 'test_numpy_clip' ,
1627
- 'test_metadata_propagation' ]:
1627
+ 'test_metadata_propagation' , 'test_copy_and_deepcopy' ,
1628
+ 'test_sample' ]:
1629
+
1630
+ def f ():
1631
+ def tester (self ):
1632
+ with catch_warnings (record = True ):
1633
+ return getattr (super (TestPanel , self ), t )()
1634
+ return tester
1635
+
1636
+ setattr (TestPanel , t , f ())
1628
1637
1629
1638
def f ():
1630
1639
def tester (self ):
1631
- with tm .assert_produces_warning (FutureWarning ,
1632
- check_stacklevel = False ):
1640
+ with catch_warnings (record = True ):
1633
1641
return getattr (super (TestPanel4D , self ), t )()
1634
1642
return tester
1635
1643
@@ -1660,10 +1668,11 @@ def test_sample(sel):
1660
1668
with tm .assertRaises (ValueError ):
1661
1669
s .sample (n = 3 , weights = 'weight_column' )
1662
1670
1663
- panel = pd .Panel (items = [0 , 1 , 2 ], major_axis = [2 , 3 , 4 ],
1664
- minor_axis = [3 , 4 , 5 ])
1665
- with tm .assertRaises (ValueError ):
1666
- panel .sample (n = 1 , weights = 'weight_column' )
1671
+ with catch_warnings (record = True ):
1672
+ panel = Panel (items = [0 , 1 , 2 ], major_axis = [2 , 3 , 4 ],
1673
+ minor_axis = [3 , 4 , 5 ])
1674
+ with tm .assertRaises (ValueError ):
1675
+ panel .sample (n = 1 , weights = 'weight_column' )
1667
1676
1668
1677
with tm .assertRaises (ValueError ):
1669
1678
df .sample (n = 1 , weights = 'weight_column' , axis = 1 )
@@ -1726,14 +1735,15 @@ def test_sample(sel):
1726
1735
assert_frame_equal (sample1 , df [['colString' ]])
1727
1736
1728
1737
# Test default axes
1729
- p = pd .Panel (items = ['a' , 'b' , 'c' ], major_axis = [2 , 4 , 6 ],
1730
- minor_axis = [1 , 3 , 5 ])
1731
- assert_panel_equal (
1732
- p .sample (n = 3 , random_state = 42 ), p .sample (n = 3 , axis = 1 ,
1733
- random_state = 42 ))
1734
- assert_frame_equal (
1735
- df .sample (n = 3 , random_state = 42 ), df .sample (n = 3 , axis = 0 ,
1736
- random_state = 42 ))
1738
+ with catch_warnings (record = True ):
1739
+ p = Panel (items = ['a' , 'b' , 'c' ], major_axis = [2 , 4 , 6 ],
1740
+ minor_axis = [1 , 3 , 5 ])
1741
+ assert_panel_equal (
1742
+ p .sample (n = 3 , random_state = 42 ), p .sample (n = 3 , axis = 1 ,
1743
+ random_state = 42 ))
1744
+ assert_frame_equal (
1745
+ df .sample (n = 3 , random_state = 42 ), df .sample (n = 3 , axis = 0 ,
1746
+ random_state = 42 ))
1737
1747
1738
1748
# Test that function aligns weights with frame
1739
1749
df = DataFrame (
@@ -1763,34 +1773,37 @@ def test_squeeze(self):
1763
1773
tm .assert_series_equal (s .squeeze (), s )
1764
1774
for df in [tm .makeTimeDataFrame ()]:
1765
1775
tm .assert_frame_equal (df .squeeze (), df )
1766
- for p in [tm .makePanel ()]:
1767
- tm .assert_panel_equal (p .squeeze (), p )
1768
- with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
1776
+ with catch_warnings (record = True ):
1777
+ for p in [tm .makePanel ()]:
1778
+ tm .assert_panel_equal (p .squeeze (), p )
1779
+ with catch_warnings (record = True ):
1769
1780
for p4d in [tm .makePanel4D ()]:
1770
1781
tm .assert_panel4d_equal (p4d .squeeze (), p4d )
1771
1782
1772
1783
# squeezing
1773
1784
df = tm .makeTimeDataFrame ().reindex (columns = ['A' ])
1774
1785
tm .assert_series_equal (df .squeeze (), df ['A' ])
1775
1786
1776
- p = tm .makePanel ().reindex (items = ['ItemA' ])
1777
- tm .assert_frame_equal (p .squeeze (), p ['ItemA' ])
1787
+ with catch_warnings (record = True ):
1788
+ p = tm .makePanel ().reindex (items = ['ItemA' ])
1789
+ tm .assert_frame_equal (p .squeeze (), p ['ItemA' ])
1778
1790
1779
- p = tm .makePanel ().reindex (items = ['ItemA' ], minor_axis = ['A' ])
1780
- tm .assert_series_equal (p .squeeze (), p .loc ['ItemA' , :, 'A' ])
1791
+ p = tm .makePanel ().reindex (items = ['ItemA' ], minor_axis = ['A' ])
1792
+ tm .assert_series_equal (p .squeeze (), p .loc ['ItemA' , :, 'A' ])
1781
1793
1782
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1794
+ with catch_warnings ( record = True ):
1783
1795
p4d = tm .makePanel4D ().reindex (labels = ['label1' ])
1784
1796
tm .assert_panel_equal (p4d .squeeze (), p4d ['label1' ])
1785
1797
1786
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1798
+ with catch_warnings ( record = True ):
1787
1799
p4d = tm .makePanel4D ().reindex (labels = ['label1' ], items = ['ItemA' ])
1788
1800
tm .assert_frame_equal (p4d .squeeze (), p4d .loc ['label1' , 'ItemA' ])
1789
1801
1790
1802
# don't fail with 0 length dimensions GH11229 & GH8999
1791
- empty_series = pd .Series ([], name = 'five' )
1792
- empty_frame = pd .DataFrame ([empty_series ])
1793
- empty_panel = pd .Panel ({'six' : empty_frame })
1803
+ empty_series = Series ([], name = 'five' )
1804
+ empty_frame = DataFrame ([empty_series ])
1805
+ with catch_warnings (record = True ):
1806
+ empty_panel = Panel ({'six' : empty_frame })
1794
1807
1795
1808
[tm .assert_series_equal (empty_series , higher_dim .squeeze ())
1796
1809
for higher_dim in [empty_series , empty_frame , empty_panel ]]
@@ -1825,13 +1838,15 @@ def test_transpose(self):
1825
1838
tm .assert_series_equal (s .transpose (), s )
1826
1839
for df in [tm .makeTimeDataFrame ()]:
1827
1840
tm .assert_frame_equal (df .transpose ().transpose (), df )
1828
- for p in [tm .makePanel ()]:
1829
- tm .assert_panel_equal (p .transpose (2 , 0 , 1 )
1830
- .transpose (1 , 2 , 0 ), p )
1831
- tm .assertRaisesRegexp (TypeError , msg , p .transpose ,
1832
- 2 , 0 , 1 , axes = (2 , 0 , 1 ))
1833
1841
1834
- with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
1842
+ with catch_warnings (record = True ):
1843
+ for p in [tm .makePanel ()]:
1844
+ tm .assert_panel_equal (p .transpose (2 , 0 , 1 )
1845
+ .transpose (1 , 2 , 0 ), p )
1846
+ tm .assertRaisesRegexp (TypeError , msg , p .transpose ,
1847
+ 2 , 0 , 1 , axes = (2 , 0 , 1 ))
1848
+
1849
+ with catch_warnings (record = True ):
1835
1850
for p4d in [tm .makePanel4D ()]:
1836
1851
tm .assert_panel4d_equal (p4d .transpose (2 , 0 , 3 , 1 )
1837
1852
.transpose (1 , 3 , 0 , 2 ), p4d )
@@ -1853,12 +1868,13 @@ def test_numpy_transpose(self):
1853
1868
tm .assertRaisesRegexp (ValueError , msg ,
1854
1869
np .transpose , df , axes = 1 )
1855
1870
1856
- p = tm .makePanel ()
1857
- tm .assert_panel_equal (np .transpose (
1858
- np .transpose (p , axes = (2 , 0 , 1 )),
1859
- axes = (1 , 2 , 0 )), p )
1871
+ with catch_warnings (record = True ):
1872
+ p = tm .makePanel ()
1873
+ tm .assert_panel_equal (np .transpose (
1874
+ np .transpose (p , axes = (2 , 0 , 1 )),
1875
+ axes = (1 , 2 , 0 )), p )
1860
1876
1861
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1877
+ with catch_warnings ( record = True ):
1862
1878
p4d = tm .makePanel4D ()
1863
1879
tm .assert_panel4d_equal (np .transpose (
1864
1880
np .transpose (p4d , axes = (2 , 0 , 3 , 1 )),
@@ -1880,15 +1896,16 @@ def test_take(self):
1880
1896
tm .assert_frame_equal (out , expected )
1881
1897
1882
1898
indices = [- 3 , 2 , 0 , 1 ]
1883
- for p in [tm .makePanel ()]:
1884
- out = p .take (indices )
1885
- expected = Panel (data = p .values .take (indices , axis = 0 ),
1886
- items = p .items .take (indices ),
1887
- major_axis = p .major_axis ,
1888
- minor_axis = p .minor_axis )
1889
- tm .assert_panel_equal (out , expected )
1890
-
1891
- with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
1899
+ with catch_warnings (record = True ):
1900
+ for p in [tm .makePanel ()]:
1901
+ out = p .take (indices )
1902
+ expected = Panel (data = p .values .take (indices , axis = 0 ),
1903
+ items = p .items .take (indices ),
1904
+ major_axis = p .major_axis ,
1905
+ minor_axis = p .minor_axis )
1906
+ tm .assert_panel_equal (out , expected )
1907
+
1908
+ with catch_warnings (record = True ):
1892
1909
for p4d in [tm .makePanel4D ()]:
1893
1910
out = p4d .take (indices )
1894
1911
expected = Panel4D (data = p4d .values .take (indices , axis = 0 ),
@@ -1902,9 +1919,9 @@ def test_take_invalid_kwargs(self):
1902
1919
indices = [- 3 , 2 , 0 , 1 ]
1903
1920
s = tm .makeFloatSeries ()
1904
1921
df = tm .makeTimeDataFrame ()
1905
- p = tm .makePanel ()
1906
1922
1907
- with tm .assert_produces_warning (FutureWarning , check_stacklevel = False ):
1923
+ with catch_warnings (record = True ):
1924
+ p = tm .makePanel ()
1908
1925
p4d = tm .makePanel4D ()
1909
1926
1910
1927
for obj in (s , df , p , p4d ):
@@ -2011,8 +2028,9 @@ def test_equals(self):
2011
2028
self .assertTrue (e .equals (f ))
2012
2029
2013
2030
def test_describe_raises (self ):
2014
- with tm .assertRaises (NotImplementedError ):
2015
- tm .makePanel ().describe ()
2031
+ with catch_warnings (record = True ):
2032
+ with tm .assertRaises (NotImplementedError ):
2033
+ tm .makePanel ().describe ()
2016
2034
2017
2035
def test_pipe (self ):
2018
2036
df = DataFrame ({'A' : [1 , 2 , 3 ]})
@@ -2043,15 +2061,16 @@ def test_pipe_tuple_error(self):
2043
2061
df .A .pipe ((f , 'y' ), x = 1 , y = 0 )
2044
2062
2045
2063
def test_pipe_panel (self ):
2046
- wp = Panel ({'r1' : DataFrame ({"A" : [1 , 2 , 3 ]})})
2047
- f = lambda x , y : x + y
2048
- result = wp .pipe (f , 2 )
2049
- expected = wp + 2
2050
- assert_panel_equal (result , expected )
2051
-
2052
- result = wp .pipe ((f , 'y' ), x = 1 )
2053
- expected = wp + 1
2054
- assert_panel_equal (result , expected )
2055
-
2056
- with tm .assertRaises (ValueError ):
2057
- result = wp .pipe ((f , 'y' ), x = 1 , y = 1 )
2064
+ with catch_warnings (record = True ):
2065
+ wp = Panel ({'r1' : DataFrame ({"A" : [1 , 2 , 3 ]})})
2066
+ f = lambda x , y : x + y
2067
+ result = wp .pipe (f , 2 )
2068
+ expected = wp + 2
2069
+ assert_panel_equal (result , expected )
2070
+
2071
+ result = wp .pipe ((f , 'y' ), x = 1 )
2072
+ expected = wp + 1
2073
+ assert_panel_equal (result , expected )
2074
+
2075
+ with tm .assertRaises (ValueError ):
2076
+ result = wp .pipe ((f , 'y' ), x = 1 , y = 1 )
0 commit comments