Example #1
0
    def test_frame_align_aware(self):
        idx1 = date_range('2001', periods=5, freq='H', tz='US/Eastern')
        idx2 = date_range('2001', periods=5, freq='2H', tz='US/Eastern')
        df1 = DataFrame(np.random.randn(len(idx1), 3), idx1)
        df2 = DataFrame(np.random.randn(len(idx2), 3), idx2)
        new1, new2 = df1.align(df2)
        assert df1.index.tz == new1.index.tz
        assert df2.index.tz == new2.index.tz

        # different timezones convert to UTC

        # frame with frame
        df1_central = df1.tz_convert('US/Central')
        new1, new2 = df1.align(df1_central)
        assert new1.index.tz == pytz.UTC
        assert new2.index.tz == pytz.UTC

        # frame with Series
        new1, new2 = df1.align(df1_central[0], axis=0)
        assert new1.index.tz == pytz.UTC
        assert new2.index.tz == pytz.UTC

        df1[0].align(df1_central, axis=0)
        assert new1.index.tz == pytz.UTC
        assert new2.index.tz == pytz.UTC
Example #2
0
 def test_align_aware(self):
     idx1 = date_range('2001', periods=5, freq='H', tz='US/Eastern')
     idx2 = date_range('2001', periods=5, freq='2H', tz='US/Eastern')
     df1 = DataFrame(np.random.randn(len(idx1), 3), idx1)
     df2 = DataFrame(np.random.randn(len(idx2), 3), idx2)
     new1, new2 = df1.align(df2)
     self.assertEqual(df1.index.tz, new1.index.tz)
     self.assertEqual(df2.index.tz, new2.index.tz)
 def test_align_aware(self):
     idx1 = date_range("2001", periods=5, freq="H", tz="US/Eastern")
     idx2 = date_range("2001", periods=5, freq="2H", tz="US/Eastern")
     df1 = DataFrame(np.random.randn(len(idx1), 3), idx1)
     df2 = DataFrame(np.random.randn(len(idx2), 3), idx2)
     new1, new2 = df1.align(df2)
     self.assertEqual(df1.index.tz, new1.index.tz)
     self.assertEqual(df2.index.tz, new2.index.tz)
Example #4
0
class LevelAlign(object):

    def setup(self):
        self.index = MultiIndex(
            levels=[np.arange(10), np.arange(100), np.arange(100)],
            codes=[np.arange(10).repeat(10000),
                   np.tile(np.arange(100).repeat(100), 10),
                   np.tile(np.tile(np.arange(100), 100), 10)])
        self.df = DataFrame(np.random.randn(len(self.index), 4),
                            index=self.index)
        self.df_level = DataFrame(np.random.randn(100, 4),
                                  index=self.index.levels[1])

    def time_align_level(self):
        self.df.align(self.df_level, level=1, copy=False)

    def time_reindex_level(self):
        self.df_level.reindex(self.index, level=1)
    def test_align_broadcast_axis(self):
        # GH 13194
        # First four tests for DataFrame.align(Index)
        # For 'right' join
        df = DataFrame(np.array([[1., 2.], [3., 4.]]), columns=list('AB'))
        ts = Series([5., 6., 7.])

        result = df.align(ts, join='right', axis=0, broadcast_axis=1)
        expected1 = DataFrame(np.array([[1., 2.], [3., 4.],
                                        [pd.np.nan, pd.np.nan]]),
                              columns=list('AB'))
        expected2 = DataFrame(np.array([[5., 5.], [6., 6.], [7., 7.]]),
                              columns=list('AB'))
        assert_frame_equal(result[0], expected1)
        assert_frame_equal(result[1], expected2)

        # For 'right' join on different index
        result = df.align(ts, join='right', axis=1, broadcast_axis=1)
        expected1 = DataFrame(np.array([[1., 2.], [3., 4.]]),
                              columns=list('AB'))
        expected2 = DataFrame(np.array([[5., 5.], [6., 6.],
                                        [7., 7.]]),
                              columns=list('AB'))
        assert_frame_equal(result[0], expected1)
        assert_frame_equal(result[1], expected2)

        # For 'left' join
        result = df.align(ts, join='left', axis=0, broadcast_axis=1)
        expected1 = DataFrame(np.array([[1., 2.], [3., 4.]]),
                              columns=list('AB'))
        expected2 = DataFrame(np.array([[5., 5.], [6., 6.]]),
                              columns=list('AB'))
        assert_frame_equal(result[0], expected1)
        assert_frame_equal(result[1], expected2)

        # For 'left' join on different axis
        result = df.align(ts, join='left', axis=1, broadcast_axis=1)
        expected1 = DataFrame(np.array([[1., 2.], [3., 4.]]),
                              columns=list('AB'))
        expected2 = DataFrame(np.array([[5., 5.], [6., 6.], [7., 7.]]),
                              columns=list('AB'))
        assert_frame_equal(result[0], expected1)
        assert_frame_equal(result[1], expected2)

        # Series.align(DataFrame) tests, 'outer' join
        result = ts.align(df, join='outer', axis=0, broadcast_axis=1)
        expected1 = DataFrame(np.array([[5., 5.], [6., 6.], [7., 7.]]),
                              columns=list('AB'))
        expected2 = DataFrame(np.array([[1., 2.], [3., 4.],
                                        [pd.np.nan, pd.np.nan]]),
                              columns=list('AB'))
        assert_frame_equal(result[0], expected1)
        assert_frame_equal(result[1], expected2)

        # Series.align(DataFrame) tests, 'inner' join
        result = ts.align(df, join='inner', axis=0, broadcast_axis=1)
        expected1 = DataFrame(np.array([[5., 5.], [6., 6.]]),
                              columns=list('AB'))
        expected2 = DataFrame(np.array([[1., 2.], [3., 4.]]),
                              columns=list('AB'))
        assert_frame_equal(result[0], expected1)
        assert_frame_equal(result[1], expected2)