@@ -1664,6 +1664,76 @@ def test_multiindex_slicers_datetimelike(self):
1664
1664
result = df .loc [(idx ['2012-01-01 12:12:12' :'2012-01-03 12:12:12' ],1 ), idx ['A' ,'B' ]]
1665
1665
assert_frame_equal (result ,expected )
1666
1666
1667
+
1668
+ def test_multiindex_slicers_edges (self ):
1669
+
1670
+ # GH 8132
1671
+ # various edge cases
1672
+ df = DataFrame ({'A' : ['A0' ] * 5 + ['A1' ]* 5 + ['A2' ]* 5 ,
1673
+ 'B' : ['B0' ,'B0' ,'B1' ,'B1' ,'B2' ] * 3 ,
1674
+ 'DATE' : ["2013-06-11" ,
1675
+ "2013-07-02" ,
1676
+ "2013-07-09" ,
1677
+ "2013-07-30" ,
1678
+ "2013-08-06" ,
1679
+ "2013-06-11" ,
1680
+ "2013-07-02" ,
1681
+ "2013-07-09" ,
1682
+ "2013-07-30" ,
1683
+ "2013-08-06" ,
1684
+ "2013-09-03" ,
1685
+ "2013-10-01" ,
1686
+ "2013-07-09" ,
1687
+ "2013-08-06" ,
1688
+ "2013-09-03" ],
1689
+ 'VALUES' : [22 , 35 , 14 , 9 , 4 , 40 , 18 , 4 , 2 , 5 , 1 , 2 , 3 ,4 , 2 ]})
1690
+
1691
+ df ['DATE' ] = pd .to_datetime (df ['DATE' ])
1692
+ df1 = df .set_index (['A' , 'B' , 'DATE' ])
1693
+ df1 = df1 .sortlevel ()
1694
+ df2 = df .set_index ('DATE' )
1695
+
1696
+ # A1 - Get all values under "A0" and "A1"
1697
+ result = df1 .loc [(slice ('A1' )),:]
1698
+ expected = df1 .iloc [0 :10 ]
1699
+ assert_frame_equal (result , expected )
1700
+
1701
+ # A2 - Get all values from the start to "A2"
1702
+ result = df1 .loc [(slice ('A2' )),:]
1703
+ expected = df1
1704
+ assert_frame_equal (result , expected )
1705
+
1706
+ # A3 - Get all values under "B1" or "B2"
1707
+ result = df1 .loc [(slice (None ),slice ('B1' ,'B2' )),:]
1708
+ expected = df1 .iloc [[2 ,3 ,4 ,7 ,8 ,9 ,12 ,13 ,14 ]]
1709
+ assert_frame_equal (result , expected )
1710
+
1711
+ # A4 - Get all values between 2013-07-02 and 2013-07-09
1712
+ result = df1 .loc [(slice (None ),slice (None ),slice ('20130702' ,'20130709' )),:]
1713
+ expected = df1 .iloc [[1 ,2 ,6 ,7 ,12 ]]
1714
+ assert_frame_equal (result , expected )
1715
+
1716
+ # B1 - Get all values in B0 that are also under A0, A1 and A2
1717
+ result = df1 .loc [(slice ('A2' ),slice ('B0' )),:]
1718
+ expected = df1 .iloc [[0 ,1 ,5 ,6 ,10 ,11 ]]
1719
+ assert_frame_equal (result , expected )
1720
+
1721
+ # B2 - Get all values in B0, B1 and B2 (similar to what #2 is doing for the As)
1722
+ result = df1 .loc [(slice (None ),slice ('B2' )),:]
1723
+ expected = df1
1724
+ assert_frame_equal (result , expected )
1725
+
1726
+ # B3 - Get all values from B1 to B2 and up to 2013-08-06
1727
+ result = df1 .loc [(slice (None ),slice ('B1' ,'B2' ),slice ('2013-08-06' )),:]
1728
+ expected = df1 .iloc [[2 ,3 ,4 ,7 ,8 ,9 ,12 ,13 ]]
1729
+ assert_frame_equal (result , expected )
1730
+
1731
+ # B4 - Same as A4 but the start of the date slice is not a key.
1732
+ # shows indexing on a partial selection slice
1733
+ result = df1 .loc [(slice (None ),slice (None ),slice ('20130701' ,'20130709' )),:]
1734
+ expected = df1 .iloc [[1 ,2 ,6 ,7 ,12 ]]
1735
+ assert_frame_equal (result , expected )
1736
+
1667
1737
def test_per_axis_per_level_doc_examples (self ):
1668
1738
1669
1739
# test index maker
@@ -3831,11 +3901,11 @@ class TestSeriesNoneCoercion(tm.TestCase):
3831
3901
# For numeric series, we should coerce to NaN.
3832
3902
([1 , 2 , 3 ], [np .nan , 2 , 3 ]),
3833
3903
([1.0 , 2.0 , 3.0 ], [np .nan , 2.0 , 3.0 ]),
3834
-
3904
+
3835
3905
# For datetime series, we should coerce to NaT.
3836
3906
([datetime (2000 , 1 , 1 ), datetime (2000 , 1 , 2 ), datetime (2000 , 1 , 3 )],
3837
3907
[NaT , datetime (2000 , 1 , 2 ), datetime (2000 , 1 , 3 )]),
3838
-
3908
+
3839
3909
# For objects, we should preserve the None value.
3840
3910
(["foo" , "bar" , "baz" ], [None , "bar" , "baz" ]),
3841
3911
]
@@ -3851,7 +3921,7 @@ def test_coercion_with_setitem(self):
3851
3921
self .assert_numpy_array_equivalent (
3852
3922
start_series .values ,
3853
3923
expected_series .values , strict_nan = True )
3854
-
3924
+
3855
3925
def test_coercion_with_loc_setitem (self ):
3856
3926
for start_data , expected_result in self .EXPECTED_RESULTS :
3857
3927
start_series = Series (start_data )
@@ -3863,7 +3933,7 @@ def test_coercion_with_loc_setitem(self):
3863
3933
self .assert_numpy_array_equivalent (
3864
3934
start_series .values ,
3865
3935
expected_series .values , strict_nan = True )
3866
-
3936
+
3867
3937
def test_coercion_with_setitem_and_series (self ):
3868
3938
for start_data , expected_result in self .EXPECTED_RESULTS :
3869
3939
start_series = Series (start_data )
@@ -3875,7 +3945,7 @@ def test_coercion_with_setitem_and_series(self):
3875
3945
self .assert_numpy_array_equivalent (
3876
3946
start_series .values ,
3877
3947
expected_series .values , strict_nan = True )
3878
-
3948
+
3879
3949
def test_coercion_with_loc_and_series (self ):
3880
3950
for start_data , expected_result in self .EXPECTED_RESULTS :
3881
3951
start_series = Series (start_data )
@@ -3887,18 +3957,18 @@ def test_coercion_with_loc_and_series(self):
3887
3957
self .assert_numpy_array_equivalent (
3888
3958
start_series .values ,
3889
3959
expected_series .values , strict_nan = True )
3890
-
3960
+
3891
3961
3892
3962
class TestDataframeNoneCoercion (tm .TestCase ):
3893
3963
EXPECTED_SINGLE_ROW_RESULTS = [
3894
3964
# For numeric series, we should coerce to NaN.
3895
3965
([1 , 2 , 3 ], [np .nan , 2 , 3 ]),
3896
3966
([1.0 , 2.0 , 3.0 ], [np .nan , 2.0 , 3.0 ]),
3897
-
3967
+
3898
3968
# For datetime series, we should coerce to NaT.
3899
3969
([datetime (2000 , 1 , 1 ), datetime (2000 , 1 , 2 ), datetime (2000 , 1 , 3 )],
3900
3970
[NaT , datetime (2000 , 1 , 2 ), datetime (2000 , 1 , 3 )]),
3901
-
3971
+
3902
3972
# For objects, we should preserve the None value.
3903
3973
(["foo" , "bar" , "baz" ], [None , "bar" , "baz" ]),
3904
3974
]
0 commit comments