Ejemplo n.º 1
0
 def test_to_timestamp_pi_mult(self):
     idx = PeriodIndex(['2011-01', 'NaT', '2011-02'], freq='2M', name='idx')
     result = idx.to_timestamp()
     expected = DatetimeIndex(
         ['2011-01-01', 'NaT', '2011-02-01'], name='idx')
     tm.assert_index_equal(result, expected)
     result = idx.to_timestamp(how='E')
     expected = DatetimeIndex(
         ['2011-02-28', 'NaT', '2011-03-31'], name='idx')
     tm.assert_index_equal(result, expected)
Ejemplo n.º 2
0
    def test_to_timestamp_pi_mult(self):
        idx = PeriodIndex(["2011-01", "NaT", "2011-02"], freq="2M", name="idx")

        result = idx.to_timestamp()
        expected = DatetimeIndex(["2011-01-01", "NaT", "2011-02-01"], name="idx")
        tm.assert_index_equal(result, expected)

        result = idx.to_timestamp(how="E")
        expected = DatetimeIndex(["2011-02-28", "NaT", "2011-03-31"], name="idx")
        expected = expected + Timedelta(1, "D") - Timedelta(1, "ns")
        tm.assert_index_equal(result, expected)
Ejemplo n.º 3
0
 def test_to_timestamp_pi_combined(self):
     idx = PeriodIndex(start='2011', periods=2, freq='1D1H', name='idx')
     result = idx.to_timestamp()
     expected = DatetimeIndex(
         ['2011-01-01 00:00', '2011-01-02 01:00'], name='idx')
     tm.assert_index_equal(result, expected)
     result = idx.to_timestamp(how='E')
     expected = DatetimeIndex(
         ['2011-01-02 00:59:59', '2011-01-03 01:59:59'], name='idx')
     tm.assert_index_equal(result, expected)
     result = idx.to_timestamp(how='E', freq='H')
     expected = DatetimeIndex(
         ['2011-01-02 00:00', '2011-01-03 01:00'], name='idx')
     tm.assert_index_equal(result, expected)
Ejemplo n.º 4
0
    def test_to_timestamp_pi_nat(self):
        # GH#7228
        index = PeriodIndex(["NaT", "2011-01", "2011-02"],
                            freq="M",
                            name="idx")

        result = index.to_timestamp("D")
        expected = DatetimeIndex(
            [NaT, datetime(2011, 1, 1),
             datetime(2011, 2, 1)], name="idx")
        tm.assert_index_equal(result, expected)
        assert result.name == "idx"

        result2 = result.to_period(freq="M")
        tm.assert_index_equal(result2, index)
        assert result2.name == "idx"

        result3 = result.to_period(freq="3M")
        exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="3M", name="idx")
        tm.assert_index_equal(result3, exp)
        assert result3.freqstr == "3M"

        msg = "Frequency must be positive, because it represents span: -2A"
        with pytest.raises(ValueError, match=msg):
            result.to_period(freq="-2A")
Ejemplo n.º 5
0
    def test_where_invalid_dtypes(self):
        pi = period_range("20130101", periods=5, freq="D")

        tail = pi[2:].tolist()
        i2 = PeriodIndex([NaT, NaT] + tail, freq="D")
        mask = notna(i2)

        result = pi.where(mask, i2.asi8)
        expected = pd.Index([NaT.value, NaT.value] + tail, dtype=object)
        assert isinstance(expected[0], int)
        tm.assert_index_equal(result, expected)

        tdi = i2.asi8.view("timedelta64[ns]")
        expected = pd.Index([tdi[0], tdi[1]] + tail, dtype=object)
        assert isinstance(expected[0], np.timedelta64)
        result = pi.where(mask, tdi)
        tm.assert_index_equal(result, expected)

        dti = i2.to_timestamp("S")
        expected = pd.Index([dti[0], dti[1]] + tail, dtype=object)
        assert expected[0] is NaT
        result = pi.where(mask, dti)
        tm.assert_index_equal(result, expected)

        td = Timedelta(days=4)
        expected = pd.Index([td, td] + tail, dtype=object)
        assert expected[0] == td
        result = pi.where(mask, td)
        tm.assert_index_equal(result, expected)
Ejemplo n.º 6
0
    def test_to_timestamp_preserve_name(self):
        index = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2009',
                            name='foo')
        assert index.name == 'foo'

        conv = index.to_timestamp('D')
        assert conv.name == 'foo'
Ejemplo n.º 7
0
    def test_to_timestamp_pi_nat(self):
        # GH#7228
        index = PeriodIndex(['NaT', '2011-01', '2011-02'], freq='M',
                            name='idx')

        result = index.to_timestamp('D')
        expected = DatetimeIndex([pd.NaT, datetime(2011, 1, 1),
                                  datetime(2011, 2, 1)], name='idx')
        tm.assert_index_equal(result, expected)
        assert result.name == 'idx'

        result2 = result.to_period(freq='M')
        tm.assert_index_equal(result2, index)
        assert result2.name == 'idx'

        result3 = result.to_period(freq='3M')
        exp = PeriodIndex(['NaT', '2011-01', '2011-02'],
                          freq='3M', name='idx')
        tm.assert_index_equal(result3, exp)
        assert result3.freqstr == '3M'

        msg = ('Frequency must be positive, because it'
               ' represents span: -2A')
        with tm.assert_raises_regex(ValueError, msg):
            result.to_period(freq='-2A')
Ejemplo n.º 8
0
    def test_to_timestamp_preserve_name(self):
        index = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2009',
                            name='foo')
        assert index.name == 'foo'

        conv = index.to_timestamp('D')
        assert conv.name == 'foo'
Ejemplo n.º 9
0
    def test_to_timestamp_pi_nat(self):
        # GH#7228
        index = PeriodIndex(['NaT', '2011-01', '2011-02'], freq='M',
                            name='idx')

        result = index.to_timestamp('D')
        expected = DatetimeIndex([pd.NaT, datetime(2011, 1, 1),
                                  datetime(2011, 2, 1)], name='idx')
        tm.assert_index_equal(result, expected)
        assert result.name == 'idx'

        result2 = result.to_period(freq='M')
        tm.assert_index_equal(result2, index)
        assert result2.name == 'idx'

        result3 = result.to_period(freq='3M')
        exp = PeriodIndex(['NaT', '2011-01', '2011-02'],
                          freq='3M', name='idx')
        tm.assert_index_equal(result3, exp)
        assert result3.freqstr == '3M'

        msg = ('Frequency must be positive, because it'
               ' represents span: -2A')
        with tm.assert_raises_regex(ValueError, msg):
            result.to_period(freq='-2A')
Ejemplo n.º 10
0
    def test_to_timestamp_preserve_name(self):
        index = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2009',
                            name='foo')
        self.assertEqual(index.name, 'foo')

        conv = index.to_timestamp('D')
        self.assertEqual(conv.name, 'foo')
Ejemplo n.º 11
0
    def test_to_timestamp_preserve_name(self):
        index = PeriodIndex(freq='A', start='1/1/2001', end='12/1/2009',
                            name='foo')
        self.assertEqual(index.name, 'foo')

        conv = index.to_timestamp('D')
        self.assertEqual(conv.name, 'foo')
Ejemplo n.º 12
0
    def test_to_timestamp_quarterly_bug(self):
        years = np.arange(1960, 2000).repeat(4)
        quarters = np.tile(lrange(1, 5), 40)

        pindex = PeriodIndex(year=years, quarter=quarters)

        stamps = pindex.to_timestamp('D', 'end')
        expected = DatetimeIndex([x.to_timestamp('D', 'end') for x in pindex])
        tm.assert_index_equal(stamps, expected)
Ejemplo n.º 13
0
    def test_to_timestamp_quarterly_bug(self):
        years = np.arange(1960, 2000).repeat(4)
        quarters = np.tile(list(range(1, 5)), 40)

        pindex = PeriodIndex(year=years, quarter=quarters)

        stamps = pindex.to_timestamp("D", "end")
        expected = DatetimeIndex([x.to_timestamp("D", "end") for x in pindex])
        tm.assert_index_equal(stamps, expected)
Ejemplo n.º 14
0
    def test_where_invalid_dtypes(self):
        pi = period_range("20130101", periods=5, freq="D")

        i2 = pi.copy()
        i2 = PeriodIndex([NaT, NaT] + pi[2:].tolist(), freq="D")

        with pytest.raises(TypeError, match="Where requires matching dtype"):
            pi.where(notna(i2), i2.asi8)

        with pytest.raises(TypeError, match="Where requires matching dtype"):
            pi.where(notna(i2), i2.asi8.view("timedelta64[ns]"))

        with pytest.raises(TypeError, match="Where requires matching dtype"):
            pi.where(notna(i2), i2.to_timestamp("S"))
Ejemplo n.º 15
0
    def test_where_invalid_dtypes(self):
        pi = period_range("20130101", periods=5, freq="D")

        i2 = PeriodIndex([NaT, NaT] + pi[2:].tolist(), freq="D")

        msg = "value should be a 'Period', 'NaT', or array of those"
        with pytest.raises(TypeError, match=msg):
            pi.where(notna(i2), i2.asi8)

        with pytest.raises(TypeError, match=msg):
            pi.where(notna(i2), i2.asi8.view("timedelta64[ns]"))

        with pytest.raises(TypeError, match=msg):
            pi.where(notna(i2), i2.to_timestamp("S"))

        with pytest.raises(TypeError, match=msg):
            # non-matching scalar
            pi.where(notna(i2), Timedelta(days=4))
Ejemplo n.º 16
0
    def test_where_invalid_dtypes(self):
        pi = period_range("20130101", periods=5, freq="D")

        i2 = PeriodIndex([NaT, NaT] + pi[2:].tolist(), freq="D")

        with pytest.raises(TypeError, match="Where requires matching dtype"):
            pi.where(notna(i2), i2.asi8)

        with pytest.raises(TypeError, match="Where requires matching dtype"):
            pi.where(notna(i2), i2.asi8.view("timedelta64[ns]"))

        with pytest.raises(TypeError, match="Where requires matching dtype"):
            pi.where(notna(i2), i2.to_timestamp("S"))

        with pytest.raises(TypeError, match="Where requires matching dtype"):
            # non-matching scalar
            pi.where(notna(i2), Timedelta(days=4))

        with pytest.raises(TypeError, match="Where requires matching dtype"):
            # non-matching NA value
            pi.where(notna(i2), np.timedelta64("NaT", "ns"))