def test_intersection_list(self):
     # GH#35876
     # values is not an Index -> no name -> retain "a"
     values = [pd.Timestamp("2020-01-01"), pd.Timestamp("2020-02-01")]
     idx = DatetimeIndex(values, name="a")
     res = idx.intersection(values)
     tm.assert_index_equal(res, idx)
Beispiel #2
0
    def test_intersection(self, tz, sort):
        # GH 4690 (with tz)
        base = date_range("6/1/2000", "6/30/2000", freq="D", name="idx")

        # if target has the same name, it is preserved
        rng2 = date_range("5/15/2000", "6/20/2000", freq="D", name="idx")
        expected2 = date_range("6/1/2000", "6/20/2000", freq="D", name="idx")

        # if target name is different, it will be reset
        rng3 = date_range("5/15/2000", "6/20/2000", freq="D", name="other")
        expected3 = date_range("6/1/2000", "6/20/2000", freq="D", name=None)

        rng4 = date_range("7/1/2000", "7/31/2000", freq="D", name="idx")
        expected4 = DatetimeIndex([], name="idx")

        for (rng, expected) in [
            (rng2, expected2),
            (rng3, expected3),
            (rng4, expected4),
        ]:
            result = base.intersection(rng)
            tm.assert_index_equal(result, expected)
            assert result.name == expected.name
            assert result.freq == expected.freq
            assert result.tz == expected.tz

        # non-monotonic
        base = DatetimeIndex(
            ["2011-01-05", "2011-01-04", "2011-01-02", "2011-01-03"], tz=tz, name="idx"
        )

        rng2 = DatetimeIndex(
            ["2011-01-04", "2011-01-02", "2011-02-02", "2011-02-03"], tz=tz, name="idx"
        )
        expected2 = DatetimeIndex(["2011-01-04", "2011-01-02"], tz=tz, name="idx")

        rng3 = DatetimeIndex(
            ["2011-01-04", "2011-01-02", "2011-02-02", "2011-02-03"],
            tz=tz,
            name="other",
        )
        expected3 = DatetimeIndex(["2011-01-04", "2011-01-02"], tz=tz, name=None)

        # GH 7880
        rng4 = date_range("7/1/2000", "7/31/2000", freq="D", tz=tz, name="idx")
        expected4 = DatetimeIndex([], tz=tz, name="idx")

        for (rng, expected) in [
            (rng2, expected2),
            (rng3, expected3),
            (rng4, expected4),
        ]:
            result = base.intersection(rng, sort=sort)
            if sort is None:
                expected = expected.sort_values()
            tm.assert_index_equal(result, expected)
            assert result.name == expected.name
            assert result.freq is None
            assert result.tz == expected.tz
Beispiel #3
0
 def get_previous_business_day(super_set: pd.DatetimeIndex,
                               sub_set: pd.DatetimeIndex):
     diff = sub_set.difference(super_set)
     while len(diff) > 0:
         new_dates = diff - pd.tseries.offsets.BDay(1)
         sub_set = new_dates.union(sub_set.intersection(super_set))
         diff = sub_set.difference(super_set)
     return sub_set
Beispiel #4
0
    def test_intersection(self, tz, sort):
        # GH 4690 (with tz)
        base = date_range('6/1/2000', '6/30/2000', freq='D', name='idx')

        # if target has the same name, it is preserved
        rng2 = date_range('5/15/2000', '6/20/2000', freq='D', name='idx')
        expected2 = date_range('6/1/2000', '6/20/2000', freq='D', name='idx')

        # if target name is different, it will be reset
        rng3 = date_range('5/15/2000', '6/20/2000', freq='D', name='other')
        expected3 = date_range('6/1/2000', '6/20/2000', freq='D', name=None)

        rng4 = date_range('7/1/2000', '7/31/2000', freq='D', name='idx')
        expected4 = DatetimeIndex([], name='idx')

        for (rng, expected) in [(rng2, expected2), (rng3, expected3),
                                (rng4, expected4)]:
            result = base.intersection(rng)
            tm.assert_index_equal(result, expected)
            assert result.name == expected.name
            assert result.freq == expected.freq
            assert result.tz == expected.tz

        # non-monotonic
        base = DatetimeIndex(
            ['2011-01-05', '2011-01-04', '2011-01-02', '2011-01-03'],
            tz=tz,
            name='idx')

        rng2 = DatetimeIndex(
            ['2011-01-04', '2011-01-02', '2011-02-02', '2011-02-03'],
            tz=tz,
            name='idx')
        expected2 = DatetimeIndex(['2011-01-04', '2011-01-02'],
                                  tz=tz,
                                  name='idx')

        rng3 = DatetimeIndex(
            ['2011-01-04', '2011-01-02', '2011-02-02', '2011-02-03'],
            tz=tz,
            name='other')
        expected3 = DatetimeIndex(['2011-01-04', '2011-01-02'],
                                  tz=tz,
                                  name=None)

        # GH 7880
        rng4 = date_range('7/1/2000', '7/31/2000', freq='D', tz=tz, name='idx')
        expected4 = DatetimeIndex([], tz=tz, name='idx')

        for (rng, expected) in [(rng2, expected2), (rng3, expected3),
                                (rng4, expected4)]:
            result = base.intersection(rng, sort=sort)
            if sort:
                expected = expected.sort_values()
            tm.assert_index_equal(result, expected)
            assert result.name == expected.name
            assert result.freq is None
            assert result.tz == expected.tz
Beispiel #5
0
    def test_intersection(self, tz, sort):
        # GH 4690 (with tz)
        base = date_range('6/1/2000', '6/30/2000', freq='D', name='idx')

        # if target has the same name, it is preserved
        rng2 = date_range('5/15/2000', '6/20/2000', freq='D', name='idx')
        expected2 = date_range('6/1/2000', '6/20/2000', freq='D', name='idx')

        # if target name is different, it will be reset
        rng3 = date_range('5/15/2000', '6/20/2000', freq='D', name='other')
        expected3 = date_range('6/1/2000', '6/20/2000', freq='D', name=None)

        rng4 = date_range('7/1/2000', '7/31/2000', freq='D', name='idx')
        expected4 = DatetimeIndex([], name='idx')

        for (rng, expected) in [(rng2, expected2), (rng3, expected3),
                                (rng4, expected4)]:
            result = base.intersection(rng)
            tm.assert_index_equal(result, expected)
            assert result.name == expected.name
            assert result.freq == expected.freq
            assert result.tz == expected.tz

        # non-monotonic
        base = DatetimeIndex(['2011-01-05', '2011-01-04',
                              '2011-01-02', '2011-01-03'],
                             tz=tz, name='idx')

        rng2 = DatetimeIndex(['2011-01-04', '2011-01-02',
                              '2011-02-02', '2011-02-03'],
                             tz=tz, name='idx')
        expected2 = DatetimeIndex(['2011-01-04', '2011-01-02'],
                                  tz=tz, name='idx')

        rng3 = DatetimeIndex(['2011-01-04', '2011-01-02',
                              '2011-02-02', '2011-02-03'],
                             tz=tz, name='other')
        expected3 = DatetimeIndex(['2011-01-04', '2011-01-02'],
                                  tz=tz, name=None)

        # GH 7880
        rng4 = date_range('7/1/2000', '7/31/2000', freq='D', tz=tz,
                          name='idx')
        expected4 = DatetimeIndex([], tz=tz, name='idx')

        for (rng, expected) in [(rng2, expected2), (rng3, expected3),
                                (rng4, expected4)]:
            result = base.intersection(rng, sort=sort)
            if sort is None:
                expected = expected.sort_values()
            tm.assert_index_equal(result, expected)
            assert result.name == expected.name
            assert result.freq is None
            assert result.tz == expected.tz
Beispiel #6
0
    def test_intersection(self):
        # GH 4690 (with tz)
        for tz in [None, 'Asia/Tokyo', 'US/Eastern', 'dateutil/US/Pacific']:
            base = date_range('6/1/2000', '6/30/2000', freq='D', name='idx')

            # if target has the same name, it is preserved
            rng2 = date_range('5/15/2000', '6/20/2000', freq='D', name='idx')
            expected2 = date_range('6/1/2000',
                                   '6/20/2000',
                                   freq='D',
                                   name='idx')

            # if target name is different, it will be reset
            rng3 = date_range('5/15/2000', '6/20/2000', freq='D', name='other')
            expected3 = date_range('6/1/2000',
                                   '6/20/2000',
                                   freq='D',
                                   name=None)

            rng4 = date_range('7/1/2000', '7/31/2000', freq='D', name='idx')
            expected4 = DatetimeIndex([], name='idx')

            for (rng, expected) in [(rng2, expected2), (rng3, expected3),
                                    (rng4, expected4)]:
                result = base.intersection(rng)
                tm.assert_index_equal(result, expected)
                self.assertEqual(result.name, expected.name)
                self.assertEqual(result.freq, expected.freq)
                self.assertEqual(result.tz, expected.tz)

            # non-monotonic
            base = DatetimeIndex(
                ['2011-01-05', '2011-01-04', '2011-01-02', '2011-01-03'],
                tz=tz,
                name='idx')

            rng2 = DatetimeIndex(
                ['2011-01-04', '2011-01-02', '2011-02-02', '2011-02-03'],
                tz=tz,
                name='idx')
            expected2 = DatetimeIndex(['2011-01-04', '2011-01-02'],
                                      tz=tz,
                                      name='idx')

            rng3 = DatetimeIndex(
                ['2011-01-04', '2011-01-02', '2011-02-02', '2011-02-03'],
                tz=tz,
                name='other')
            expected3 = DatetimeIndex(['2011-01-04', '2011-01-02'],
                                      tz=tz,
                                      name=None)

            # GH 7880
            rng4 = date_range('7/1/2000',
                              '7/31/2000',
                              freq='D',
                              tz=tz,
                              name='idx')
            expected4 = DatetimeIndex([], tz=tz, name='idx')

            for (rng, expected) in [(rng2, expected2), (rng3, expected3),
                                    (rng4, expected4)]:
                result = base.intersection(rng)
                tm.assert_index_equal(result, expected)
                self.assertEqual(result.name, expected.name)
                self.assertIsNone(result.freq)
                self.assertEqual(result.tz, expected.tz)

        # empty same freq GH2129
        rng = date_range('6/1/2000', '6/15/2000', freq='T')
        result = rng[0:0].intersection(rng)
        self.assertEqual(len(result), 0)

        result = rng.intersection(rng[0:0])
        self.assertEqual(len(result), 0)
Beispiel #7
0
    def test_intersection(self):
        # GH 4690 (with tz)
        for tz in [None, 'Asia/Tokyo', 'US/Eastern', 'dateutil/US/Pacific']:
            base = date_range('6/1/2000', '6/30/2000', freq='D', name='idx')

            # if target has the same name, it is preserved
            rng2 = date_range('5/15/2000', '6/20/2000', freq='D', name='idx')
            expected2 = date_range('6/1/2000', '6/20/2000', freq='D',
                                   name='idx')

            # if target name is different, it will be reset
            rng3 = date_range('5/15/2000', '6/20/2000', freq='D', name='other')
            expected3 = date_range('6/1/2000', '6/20/2000', freq='D',
                                   name=None)

            rng4 = date_range('7/1/2000', '7/31/2000', freq='D', name='idx')
            expected4 = DatetimeIndex([], name='idx')

            for (rng, expected) in [(rng2, expected2), (rng3, expected3),
                                    (rng4, expected4)]:
                result = base.intersection(rng)
                tm.assert_index_equal(result, expected)
                self.assertEqual(result.name, expected.name)
                self.assertEqual(result.freq, expected.freq)
                self.assertEqual(result.tz, expected.tz)

            # non-monotonic
            base = DatetimeIndex(['2011-01-05', '2011-01-04',
                                  '2011-01-02', '2011-01-03'],
                                 tz=tz, name='idx')

            rng2 = DatetimeIndex(['2011-01-04', '2011-01-02',
                                  '2011-02-02', '2011-02-03'],
                                 tz=tz, name='idx')
            expected2 = DatetimeIndex(
                ['2011-01-04', '2011-01-02'], tz=tz, name='idx')

            rng3 = DatetimeIndex(['2011-01-04', '2011-01-02',
                                  '2011-02-02', '2011-02-03'],
                                 tz=tz, name='other')
            expected3 = DatetimeIndex(
                ['2011-01-04', '2011-01-02'], tz=tz, name=None)

            # GH 7880
            rng4 = date_range('7/1/2000', '7/31/2000', freq='D', tz=tz,
                              name='idx')
            expected4 = DatetimeIndex([], tz=tz, name='idx')

            for (rng, expected) in [(rng2, expected2), (rng3, expected3),
                                    (rng4, expected4)]:
                result = base.intersection(rng)
                tm.assert_index_equal(result, expected)
                self.assertEqual(result.name, expected.name)
                self.assertIsNone(result.freq)
                self.assertEqual(result.tz, expected.tz)

        # empty same freq GH2129
        rng = date_range('6/1/2000', '6/15/2000', freq='T')
        result = rng[0:0].intersection(rng)
        self.assertEqual(len(result), 0)

        result = rng.intersection(rng[0:0])
        self.assertEqual(len(result), 0)
Beispiel #8
0
 def test_intersection_list(self):
     # GH#35876
     values = [pd.Timestamp("2020-01-01"), pd.Timestamp("2020-02-01")]
     idx = DatetimeIndex(values, name="a")
     res = idx.intersection(values)
     tm.assert_index_equal(res, idx.rename(None))