@@ -3418,6 +3418,16 @@ def test_add_offset_nat(self):
34183418 with tm .assertRaises (ValueError ):
34193419 p + o
34203420
3421+ def test_sub_pdnat (self ):
3422+ # GH 13071
3423+ p = pd .Period ('2011-01' , freq = 'M' )
3424+ self .assertIs (p - pd .NaT , pd .NaT )
3425+ self .assertIs (pd .NaT - p , pd .NaT )
3426+
3427+ p = pd .Period ('NaT' , freq = 'M' )
3428+ self .assertIs (p - pd .NaT , pd .NaT )
3429+ self .assertIs (pd .NaT - p , pd .NaT )
3430+
34213431 def test_sub_offset (self ):
34223432 # freq is DateOffset
34233433 for freq in ['A' , '2A' , '3A' ]:
@@ -3614,6 +3624,48 @@ def test_pi_ops_array(self):
36143624 '2011-01-01 12:15:00' ], freq = 'S' , name = 'idx' )
36153625 self .assert_index_equal (result , exp )
36163626
3627+ def test_pi_sub_period (self ):
3628+ # GH 13071
3629+ idx = PeriodIndex (['2011-01' , '2011-02' , '2011-03' ,
3630+ '2011-04' ], freq = 'M' , name = 'idx' )
3631+
3632+ result = idx - pd .Period ('2012-01' , freq = 'M' )
3633+ exp = pd .Index ([- 12 , - 11 , - 10 , - 9 ], name = 'idx' )
3634+ tm .assert_index_equal (result , exp )
3635+
3636+ result = pd .Period ('2012-01' , freq = 'M' ) - idx
3637+ exp = pd .Index ([12 , 11 , 10 , 9 ], name = 'idx' )
3638+ tm .assert_index_equal (result , exp )
3639+
3640+ exp = pd .Index ([np .nan , np .nan , np .nan , np .nan ], name = 'idx' )
3641+ tm .assert_index_equal (idx - pd .Period ('NaT' , freq = 'M' ), exp )
3642+ tm .assert_index_equal (pd .Period ('NaT' , freq = 'M' ) - idx , exp )
3643+
3644+ def test_pi_sub_pdnat (self ):
3645+ # GH 13071
3646+ idx = PeriodIndex (['2011-01' , '2011-02' , 'NaT' ,
3647+ '2011-04' ], freq = 'M' , name = 'idx' )
3648+ exp = pd .TimedeltaIndex ([pd .NaT ] * 4 , name = 'idx' )
3649+ tm .assert_index_equal (pd .NaT - idx , exp )
3650+ tm .assert_index_equal (idx - pd .NaT , exp )
3651+
3652+ def test_pi_sub_period_nat (self ):
3653+ # GH 13071
3654+ idx = PeriodIndex (['2011-01' , 'NaT' , '2011-03' ,
3655+ '2011-04' ], freq = 'M' , name = 'idx' )
3656+
3657+ result = idx - pd .Period ('2012-01' , freq = 'M' )
3658+ exp = pd .Index ([- 12 , np .nan , - 10 , - 9 ], name = 'idx' )
3659+ tm .assert_index_equal (result , exp )
3660+
3661+ result = pd .Period ('2012-01' , freq = 'M' ) - idx
3662+ exp = pd .Index ([12 , np .nan , 10 , 9 ], name = 'idx' )
3663+ tm .assert_index_equal (result , exp )
3664+
3665+ exp = pd .Index ([np .nan , np .nan , np .nan , np .nan ], name = 'idx' )
3666+ tm .assert_index_equal (idx - pd .Period ('NaT' , freq = 'M' ), exp )
3667+ tm .assert_index_equal (pd .Period ('NaT' , freq = 'M' ) - idx , exp )
3668+
36173669
36183670class TestPeriodRepresentation (tm .TestCase ):
36193671 """
0 commit comments