Python pandas.util.testing.SubclassedDataFrame() Examples

The following are 30 code examples of pandas.util.testing.SubclassedDataFrame(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pandas.util.testing , or try the search function .
Example #1
Source File: test_subclass.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_subclassed_wide_to_long(self):
        # GH 9762

        np.random.seed(123)
        x = np.random.randn(3)
        df = tm.SubclassedDataFrame({
            "A1970": {0: "a", 1: "b", 2: "c"},
            "A1980": {0: "d", 1: "e", 2: "f"},
            "B1970": {0: 2.5, 1: 1.2, 2: .7},
            "B1980": {0: 3.2, 1: 1.3, 2: .1},
            "X": dict(zip(range(3), x))})

        df["id"] = df.index
        exp_data = {"X": x.tolist() + x.tolist(),
                    "A": ['a', 'b', 'c', 'd', 'e', 'f'],
                    "B": [2.5, 1.2, 0.7, 3.2, 1.3, 0.1],
                    "year": [1970, 1970, 1970, 1980, 1980, 1980],
                    "id": [0, 1, 2, 0, 1, 2]}
        expected = tm.SubclassedDataFrame(exp_data)
        expected = expected.set_index(['id', 'year'])[["X", "A", "B"]]
        long_frame = pd.wide_to_long(df, ["A", "B"], i="id", j="year")

        tm.assert_frame_equal(long_frame, expected) 
Example #2
Source File: test_subclass.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_subclassed_wide_to_long(self):
        # GH 9762

        np.random.seed(123)
        x = np.random.randn(3)
        df = tm.SubclassedDataFrame({
            "A1970": {0: "a", 1: "b", 2: "c"},
            "A1980": {0: "d", 1: "e", 2: "f"},
            "B1970": {0: 2.5, 1: 1.2, 2: .7},
            "B1980": {0: 3.2, 1: 1.3, 2: .1},
            "X": dict(zip(range(3), x))})

        df["id"] = df.index
        exp_data = {"X": x.tolist() + x.tolist(),
                    "A": ['a', 'b', 'c', 'd', 'e', 'f'],
                    "B": [2.5, 1.2, 0.7, 3.2, 1.3, 0.1],
                    "year": [1970, 1970, 1970, 1980, 1980, 1980],
                    "id": [0, 1, 2, 0, 1, 2]}
        expected = tm.SubclassedDataFrame(exp_data)
        expected = expected.set_index(['id', 'year'])[["X", "A", "B"]]
        long_frame = pd.wide_to_long(df, ["A", "B"], i="id", j="year")

        tm.assert_frame_equal(long_frame, expected) 
Example #3
Source File: test_subclass.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_subclass_pivot(self):
        # GH 15564
        df = tm.SubclassedDataFrame({
            'index': ['A', 'B', 'C', 'C', 'B', 'A'],
            'columns': ['One', 'One', 'One', 'Two', 'Two', 'Two'],
            'values': [1., 2., 3., 3., 2., 1.]})

        pivoted = df.pivot(
            index='index', columns='columns', values='values')

        expected = tm.SubclassedDataFrame({
            'One': {'A': 1., 'B': 2., 'C': 3.},
            'Two': {'A': 1., 'B': 2., 'C': 3.}})

        expected.index.name, expected.columns.name = 'index', 'columns'

        tm.assert_frame_equal(pivoted, expected) 
Example #4
Source File: test_subclass.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_dataframe_metadata(self):
        df = tm.SubclassedDataFrame({'X': [1, 2, 3], 'Y': [1, 2, 3]},
                                    index=['a', 'b', 'c'])
        df.testattr = 'XXX'

        assert df.testattr == 'XXX'
        assert df[['X']].testattr == 'XXX'
        assert df.loc[['a', 'b'], :].testattr == 'XXX'
        assert df.iloc[[0, 1], :].testattr == 'XXX'

        # see gh-9776
        assert df.iloc[0:1, :].testattr == 'XXX'

        # see gh-10553
        unpickled = tm.round_trip_pickle(df)
        tm.assert_frame_equal(df, unpickled)
        assert df._metadata == unpickled._metadata
        assert df.testattr == unpickled.testattr 
Example #5
Source File: test_subclass.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_subclassed_melt(self):
        # GH 15564
        cheese = tm.SubclassedDataFrame({
            'first': ['John', 'Mary'],
            'last': ['Doe', 'Bo'],
            'height': [5.5, 6.0],
            'weight': [130, 150]})

        melted = pd.melt(cheese, id_vars=['first', 'last'])

        expected = tm.SubclassedDataFrame([
            ['John', 'Doe', 'height', 5.5],
            ['Mary', 'Bo', 'height', 6.0],
            ['John', 'Doe', 'weight', 130],
            ['Mary', 'Bo', 'weight', 150]],
            columns=['first', 'last', 'variable', 'value'])

        tm.assert_frame_equal(melted, expected) 
Example #6
Source File: test_subclass.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_subclass_align(self):
        # GH 12983
        df1 = tm.SubclassedDataFrame({'a': [1, 3, 5],
                                      'b': [1, 3, 5]}, index=list('ACE'))
        df2 = tm.SubclassedDataFrame({'c': [1, 2, 4],
                                      'd': [1, 2, 4]}, index=list('ABD'))

        res1, res2 = df1.align(df2, axis=0)
        exp1 = tm.SubclassedDataFrame({'a': [1, np.nan, 3, np.nan, 5],
                                       'b': [1, np.nan, 3, np.nan, 5]},
                                      index=list('ABCDE'))
        exp2 = tm.SubclassedDataFrame({'c': [1, 2, np.nan, 4, np.nan],
                                       'd': [1, 2, np.nan, 4, np.nan]},
                                      index=list('ABCDE'))
        assert isinstance(res1, tm.SubclassedDataFrame)
        tm.assert_frame_equal(res1, exp1)
        assert isinstance(res2, tm.SubclassedDataFrame)
        tm.assert_frame_equal(res2, exp2)

        res1, res2 = df1.a.align(df2.c)
        assert isinstance(res1, tm.SubclassedSeries)
        tm.assert_series_equal(res1, exp1.a)
        assert isinstance(res2, tm.SubclassedSeries)
        tm.assert_series_equal(res2, exp2.c) 
Example #7
Source File: test_subclass.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_subclassed_wide_to_long(self):
        # GH 9762

        np.random.seed(123)
        x = np.random.randn(3)
        df = tm.SubclassedDataFrame({
            "A1970": {0: "a", 1: "b", 2: "c"},
            "A1980": {0: "d", 1: "e", 2: "f"},
            "B1970": {0: 2.5, 1: 1.2, 2: .7},
            "B1980": {0: 3.2, 1: 1.3, 2: .1},
            "X": dict(zip(range(3), x))})

        df["id"] = df.index
        exp_data = {"X": x.tolist() + x.tolist(),
                    "A": ['a', 'b', 'c', 'd', 'e', 'f'],
                    "B": [2.5, 1.2, 0.7, 3.2, 1.3, 0.1],
                    "year": [1970, 1970, 1970, 1980, 1980, 1980],
                    "id": [0, 1, 2, 0, 1, 2]}
        expected = tm.SubclassedDataFrame(exp_data)
        expected = expected.set_index(['id', 'year'])[["X", "A", "B"]]
        long_frame = pd.wide_to_long(df, ["A", "B"], i="id", j="year")

        tm.assert_frame_equal(long_frame, expected) 
Example #8
Source File: test_subclass.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_subclass_align(self):
        # GH 12983
        df1 = tm.SubclassedDataFrame({'a': [1, 3, 5],
                                      'b': [1, 3, 5]}, index=list('ACE'))
        df2 = tm.SubclassedDataFrame({'c': [1, 2, 4],
                                      'd': [1, 2, 4]}, index=list('ABD'))

        res1, res2 = df1.align(df2, axis=0)
        exp1 = tm.SubclassedDataFrame({'a': [1, np.nan, 3, np.nan, 5],
                                       'b': [1, np.nan, 3, np.nan, 5]},
                                      index=list('ABCDE'))
        exp2 = tm.SubclassedDataFrame({'c': [1, 2, np.nan, 4, np.nan],
                                       'd': [1, 2, np.nan, 4, np.nan]},
                                      index=list('ABCDE'))
        assert isinstance(res1, tm.SubclassedDataFrame)
        tm.assert_frame_equal(res1, exp1)
        assert isinstance(res2, tm.SubclassedDataFrame)
        tm.assert_frame_equal(res2, exp2)

        res1, res2 = df1.a.align(df2.c)
        assert isinstance(res1, tm.SubclassedSeries)
        tm.assert_series_equal(res1, exp1.a)
        assert isinstance(res2, tm.SubclassedSeries)
        tm.assert_series_equal(res2, exp2.c) 
Example #9
Source File: test_subclass.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_subclassed_melt(self):
        # GH 15564
        cheese = tm.SubclassedDataFrame({
            'first': ['John', 'Mary'],
            'last': ['Doe', 'Bo'],
            'height': [5.5, 6.0],
            'weight': [130, 150]})

        melted = pd.melt(cheese, id_vars=['first', 'last'])

        expected = tm.SubclassedDataFrame([
            ['John', 'Doe', 'height', 5.5],
            ['Mary', 'Bo', 'height', 6.0],
            ['John', 'Doe', 'weight', 130],
            ['Mary', 'Bo', 'weight', 150]],
            columns=['first', 'last', 'variable', 'value'])

        tm.assert_frame_equal(melted, expected) 
Example #10
Source File: test_subclass.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_dataframe_metadata(self):
        df = tm.SubclassedDataFrame({'X': [1, 2, 3], 'Y': [1, 2, 3]},
                                    index=['a', 'b', 'c'])
        df.testattr = 'XXX'

        assert df.testattr == 'XXX'
        assert df[['X']].testattr == 'XXX'
        assert df.loc[['a', 'b'], :].testattr == 'XXX'
        assert df.iloc[[0, 1], :].testattr == 'XXX'

        # see gh-9776
        assert df.iloc[0:1, :].testattr == 'XXX'

        # see gh-10553
        unpickled = tm.round_trip_pickle(df)
        tm.assert_frame_equal(df, unpickled)
        assert df._metadata == unpickled._metadata
        assert df.testattr == unpickled.testattr 
Example #11
Source File: test_subclass.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_subclass_pivot(self):
        # GH 15564
        df = tm.SubclassedDataFrame({
            'index': ['A', 'B', 'C', 'C', 'B', 'A'],
            'columns': ['One', 'One', 'One', 'Two', 'Two', 'Two'],
            'values': [1., 2., 3., 3., 2., 1.]})

        pivoted = df.pivot(
            index='index', columns='columns', values='values')

        expected = tm.SubclassedDataFrame({
            'One': {'A': 1., 'B': 2., 'C': 3.},
            'Two': {'A': 1., 'B': 2., 'C': 3.}})

        expected.index.name, expected.columns.name = 'index', 'columns'

        tm.assert_frame_equal(pivoted, expected) 
Example #12
Source File: test_subclass.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_dataframe_metadata(self):
        df = tm.SubclassedDataFrame({'X': [1, 2, 3], 'Y': [1, 2, 3]},
                                    index=['a', 'b', 'c'])
        df.testattr = 'XXX'

        assert df.testattr == 'XXX'
        assert df[['X']].testattr == 'XXX'
        assert df.loc[['a', 'b'], :].testattr == 'XXX'
        assert df.iloc[[0, 1], :].testattr == 'XXX'

        # see gh-9776
        assert df.iloc[0:1, :].testattr == 'XXX'

        # see gh-10553
        unpickled = tm.round_trip_pickle(df)
        tm.assert_frame_equal(df, unpickled)
        assert df._metadata == unpickled._metadata
        assert df.testattr == unpickled.testattr 
Example #13
Source File: test_subclass.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_subclassed_wide_to_long(self):
        # GH 9762

        np.random.seed(123)
        x = np.random.randn(3)
        df = tm.SubclassedDataFrame({
            "A1970": {0: "a", 1: "b", 2: "c"},
            "A1980": {0: "d", 1: "e", 2: "f"},
            "B1970": {0: 2.5, 1: 1.2, 2: .7},
            "B1980": {0: 3.2, 1: 1.3, 2: .1},
            "X": dict(zip(range(3), x))})

        df["id"] = df.index
        exp_data = {"X": x.tolist() + x.tolist(),
                    "A": ['a', 'b', 'c', 'd', 'e', 'f'],
                    "B": [2.5, 1.2, 0.7, 3.2, 1.3, 0.1],
                    "year": [1970, 1970, 1970, 1980, 1980, 1980],
                    "id": [0, 1, 2, 0, 1, 2]}
        expected = tm.SubclassedDataFrame(exp_data)
        expected = expected.set_index(['id', 'year'])[["X", "A", "B"]]
        long_frame = pd.wide_to_long(df, ["A", "B"], i="id", j="year")

        tm.assert_frame_equal(long_frame, expected) 
Example #14
Source File: test_subclass.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_subclassed_melt(self):
        # GH 15564
        cheese = tm.SubclassedDataFrame({
            'first': ['John', 'Mary'],
            'last': ['Doe', 'Bo'],
            'height': [5.5, 6.0],
            'weight': [130, 150]})

        melted = pd.melt(cheese, id_vars=['first', 'last'])

        expected = tm.SubclassedDataFrame([
            ['John', 'Doe', 'height', 5.5],
            ['Mary', 'Bo', 'height', 6.0],
            ['John', 'Doe', 'weight', 130],
            ['Mary', 'Bo', 'weight', 150]],
            columns=['first', 'last', 'variable', 'value'])

        tm.assert_frame_equal(melted, expected) 
Example #15
Source File: test_subclass.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_subclass_pivot(self):
        # GH 15564
        df = tm.SubclassedDataFrame({
            'index': ['A', 'B', 'C', 'C', 'B', 'A'],
            'columns': ['One', 'One', 'One', 'Two', 'Two', 'Two'],
            'values': [1., 2., 3., 3., 2., 1.]})

        pivoted = df.pivot(
            index='index', columns='columns', values='values')

        expected = tm.SubclassedDataFrame({
            'One': {'A': 1., 'B': 2., 'C': 3.},
            'Two': {'A': 1., 'B': 2., 'C': 3.}})

        expected.index.name, expected.columns.name = 'index', 'columns'

        tm.assert_frame_equal(pivoted, expected) 
Example #16
Source File: test_subclass.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_subclass_align(self):
        # GH 12983
        df1 = tm.SubclassedDataFrame({'a': [1, 3, 5],
                                      'b': [1, 3, 5]}, index=list('ACE'))
        df2 = tm.SubclassedDataFrame({'c': [1, 2, 4],
                                      'd': [1, 2, 4]}, index=list('ABD'))

        res1, res2 = df1.align(df2, axis=0)
        exp1 = tm.SubclassedDataFrame({'a': [1, np.nan, 3, np.nan, 5],
                                       'b': [1, np.nan, 3, np.nan, 5]},
                                      index=list('ABCDE'))
        exp2 = tm.SubclassedDataFrame({'c': [1, 2, np.nan, 4, np.nan],
                                       'd': [1, 2, np.nan, 4, np.nan]},
                                      index=list('ABCDE'))
        assert isinstance(res1, tm.SubclassedDataFrame)
        tm.assert_frame_equal(res1, exp1)
        assert isinstance(res2, tm.SubclassedDataFrame)
        tm.assert_frame_equal(res2, exp2)

        res1, res2 = df1.a.align(df2.c)
        assert isinstance(res1, tm.SubclassedSeries)
        tm.assert_series_equal(res1, exp1.a)
        assert isinstance(res2, tm.SubclassedSeries)
        tm.assert_series_equal(res2, exp2.c) 
Example #17
Source File: test_subclass.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_dataframe_metadata(self):
        df = tm.SubclassedDataFrame({'X': [1, 2, 3], 'Y': [1, 2, 3]},
                                    index=['a', 'b', 'c'])
        df.testattr = 'XXX'

        assert df.testattr == 'XXX'
        assert df[['X']].testattr == 'XXX'
        assert df.loc[['a', 'b'], :].testattr == 'XXX'
        assert df.iloc[[0, 1], :].testattr == 'XXX'

        # see gh-9776
        assert df.iloc[0:1, :].testattr == 'XXX'

        # see gh-10553
        unpickled = tm.round_trip_pickle(df)
        tm.assert_frame_equal(df, unpickled)
        assert df._metadata == unpickled._metadata
        assert df.testattr == unpickled.testattr 
Example #18
Source File: test_subclass.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_subclass_align(self):
        # GH 12983
        df1 = tm.SubclassedDataFrame({'a': [1, 3, 5],
                                      'b': [1, 3, 5]}, index=list('ACE'))
        df2 = tm.SubclassedDataFrame({'c': [1, 2, 4],
                                      'd': [1, 2, 4]}, index=list('ABD'))

        res1, res2 = df1.align(df2, axis=0)
        exp1 = tm.SubclassedDataFrame({'a': [1, np.nan, 3, np.nan, 5],
                                       'b': [1, np.nan, 3, np.nan, 5]},
                                      index=list('ABCDE'))
        exp2 = tm.SubclassedDataFrame({'c': [1, 2, np.nan, 4, np.nan],
                                       'd': [1, 2, np.nan, 4, np.nan]},
                                      index=list('ABCDE'))
        assert isinstance(res1, tm.SubclassedDataFrame)
        tm.assert_frame_equal(res1, exp1)
        assert isinstance(res2, tm.SubclassedDataFrame)
        tm.assert_frame_equal(res2, exp2)

        res1, res2 = df1.a.align(df2.c)
        assert isinstance(res1, tm.SubclassedSeries)
        tm.assert_series_equal(res1, exp1.a)
        assert isinstance(res2, tm.SubclassedSeries)
        tm.assert_series_equal(res2, exp2.c) 
Example #19
Source File: test_subclass.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_subclass_pivot(self):
        # GH 15564
        df = tm.SubclassedDataFrame({
            'index': ['A', 'B', 'C', 'C', 'B', 'A'],
            'columns': ['One', 'One', 'One', 'Two', 'Two', 'Two'],
            'values': [1., 2., 3., 3., 2., 1.]})

        pivoted = df.pivot(
            index='index', columns='columns', values='values')

        expected = tm.SubclassedDataFrame({
            'One': {'A': 1., 'B': 2., 'C': 3.},
            'Two': {'A': 1., 'B': 2., 'C': 3.}})

        expected.index.name, expected.columns.name = 'index', 'columns'

        tm.assert_frame_equal(pivoted, expected) 
Example #20
Source File: test_subclass.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_subclass_align(self):
        # GH 12983
        df1 = tm.SubclassedDataFrame({'a': [1, 3, 5],
                                      'b': [1, 3, 5]}, index=list('ACE'))
        df2 = tm.SubclassedDataFrame({'c': [1, 2, 4],
                                      'd': [1, 2, 4]}, index=list('ABD'))

        res1, res2 = df1.align(df2, axis=0)
        exp1 = tm.SubclassedDataFrame({'a': [1, np.nan, 3, np.nan, 5],
                                       'b': [1, np.nan, 3, np.nan, 5]},
                                      index=list('ABCDE'))
        exp2 = tm.SubclassedDataFrame({'c': [1, 2, np.nan, 4, np.nan],
                                       'd': [1, 2, np.nan, 4, np.nan]},
                                      index=list('ABCDE'))
        assert isinstance(res1, tm.SubclassedDataFrame)
        tm.assert_frame_equal(res1, exp1)
        assert isinstance(res2, tm.SubclassedDataFrame)
        tm.assert_frame_equal(res2, exp2)

        res1, res2 = df1.a.align(df2.c)
        assert isinstance(res1, tm.SubclassedSeries)
        tm.assert_series_equal(res1, exp1.a)
        assert isinstance(res2, tm.SubclassedSeries)
        tm.assert_series_equal(res2, exp2.c) 
Example #21
Source File: test_subclass.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_subclassed_melt(self):
        # GH 15564
        cheese = tm.SubclassedDataFrame({
            'first': ['John', 'Mary'],
            'last': ['Doe', 'Bo'],
            'height': [5.5, 6.0],
            'weight': [130, 150]})

        melted = pd.melt(cheese, id_vars=['first', 'last'])

        expected = tm.SubclassedDataFrame([
            ['John', 'Doe', 'height', 5.5],
            ['Mary', 'Bo', 'height', 6.0],
            ['John', 'Doe', 'weight', 130],
            ['Mary', 'Bo', 'weight', 150]],
            columns=['first', 'last', 'variable', 'value'])

        tm.assert_frame_equal(melted, expected) 
Example #22
Source File: test_subclass.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_dataframe_metadata(self):
        df = tm.SubclassedDataFrame({'X': [1, 2, 3], 'Y': [1, 2, 3]},
                                    index=['a', 'b', 'c'])
        df.testattr = 'XXX'

        assert df.testattr == 'XXX'
        assert df[['X']].testattr == 'XXX'
        assert df.loc[['a', 'b'], :].testattr == 'XXX'
        assert df.iloc[[0, 1], :].testattr == 'XXX'

        # see gh-9776
        assert df.iloc[0:1, :].testattr == 'XXX'

        # see gh-10553
        unpickled = tm.round_trip_pickle(df)
        tm.assert_frame_equal(df, unpickled)
        assert df._metadata == unpickled._metadata
        assert df.testattr == unpickled.testattr 
Example #23
Source File: test_subclass.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_indexing_sliced(self):
        # GH 11559
        df = tm.SubclassedDataFrame({'X': [1, 2, 3],
                                     'Y': [4, 5, 6],
                                     'Z': [7, 8, 9]},
                                    index=['a', 'b', 'c'])
        res = df.loc[:, 'X']
        exp = tm.SubclassedSeries([1, 2, 3], index=list('abc'), name='X')
        tm.assert_series_equal(res, exp)
        assert isinstance(res, tm.SubclassedSeries)

        res = df.iloc[:, 1]
        exp = tm.SubclassedSeries([4, 5, 6], index=list('abc'), name='Y')
        tm.assert_series_equal(res, exp)
        assert isinstance(res, tm.SubclassedSeries)

        res = df.loc[:, 'Z']
        exp = tm.SubclassedSeries([7, 8, 9], index=list('abc'), name='Z')
        tm.assert_series_equal(res, exp)
        assert isinstance(res, tm.SubclassedSeries)

        res = df.loc['a', :]
        exp = tm.SubclassedSeries([1, 4, 7], index=list('XYZ'), name='a')
        tm.assert_series_equal(res, exp)
        assert isinstance(res, tm.SubclassedSeries)

        res = df.iloc[1, :]
        exp = tm.SubclassedSeries([2, 5, 8], index=list('XYZ'), name='b')
        tm.assert_series_equal(res, exp)
        assert isinstance(res, tm.SubclassedSeries)

        res = df.loc['c', :]
        exp = tm.SubclassedSeries([3, 6, 9], index=list('XYZ'), name='c')
        tm.assert_series_equal(res, exp)
        assert isinstance(res, tm.SubclassedSeries) 
Example #24
Source File: test_subclass.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_subclass_stack(self):
        # GH 15564
        df = tm.SubclassedDataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                                    index=['a', 'b', 'c'],
                                    columns=['X', 'Y', 'Z'])

        res = df.stack()
        exp = tm.SubclassedSeries(
            [1, 2, 3, 4, 5, 6, 7, 8, 9],
            index=[list('aaabbbccc'), list('XYZXYZXYZ')])

        tm.assert_series_equal(res, exp) 
Example #25
Source File: test_subclass.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_subclass_iterrows(self):
        # GH 13977
        df = tm.SubclassedDataFrame({'a': [1]})
        for i, row in df.iterrows():
            assert isinstance(row, tm.SubclassedSeries)
            tm.assert_series_equal(row, df.loc[i]) 
Example #26
Source File: test_subclass.py    From twitter-stock-recommendation with MIT License 5 votes vote down vote up
def test_subclass_unstack(self):
        # GH 15564
        df = tm.SubclassedDataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                                    index=['a', 'b', 'c'],
                                    columns=['X', 'Y', 'Z'])

        res = df.unstack()
        exp = tm.SubclassedSeries(
            [1, 4, 7, 2, 5, 8, 3, 6, 9],
            index=[list('XXXYYYZZZ'), list('abcabcabc')])

        tm.assert_series_equal(res, exp) 
Example #27
Source File: test_subclass.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_subclass_stack(self):
        # GH 15564
        df = tm.SubclassedDataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                                    index=['a', 'b', 'c'],
                                    columns=['X', 'Y', 'Z'])

        res = df.stack()
        exp = tm.SubclassedSeries(
            [1, 2, 3, 4, 5, 6, 7, 8, 9],
            index=[list('aaabbbccc'), list('XYZXYZXYZ')])

        tm.assert_series_equal(res, exp) 
Example #28
Source File: test_subclass.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_subclass_unstack(self):
        # GH 15564
        s = tm.SubclassedSeries(
            [1, 2, 3, 4], index=[list('aabb'), list('xyxy')])

        res = s.unstack()
        exp = tm.SubclassedDataFrame(
            {'x': [1, 3], 'y': [2, 4]}, index=['a', 'b'])

        tm.assert_frame_equal(res, exp) 
Example #29
Source File: test_subclass.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_subclass_unstack(self):
        # GH 15564
        df = tm.SubclassedDataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                                    index=['a', 'b', 'c'],
                                    columns=['X', 'Y', 'Z'])

        res = df.unstack()
        exp = tm.SubclassedSeries(
            [1, 4, 7, 2, 5, 8, 3, 6, 9],
            index=[list('XXXYYYZZZ'), list('abcabcabc')])

        tm.assert_series_equal(res, exp) 
Example #30
Source File: test_subclass.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_subclass_unstack_multi_mixed(self):
        # GH 15564
        df = tm.SubclassedDataFrame([
            [10, 11, 12.0, 13.0],
            [20, 21, 22.0, 23.0],
            [30, 31, 32.0, 33.0],
            [40, 41, 42.0, 43.0]],
            index=MultiIndex.from_tuples(
                list(zip(list('AABB'), list('cdcd'))),
                names=['aaa', 'ccc']),
            columns=MultiIndex.from_tuples(
                list(zip(list('WWXX'), list('yzyz'))),
                names=['www', 'yyy']))

        exp = tm.SubclassedDataFrame([
            [10, 20, 11, 21, 12.0, 22.0, 13.0, 23.0],
            [30, 40, 31, 41, 32.0, 42.0, 33.0, 43.0]],
            index=Index(['A', 'B'], name='aaa'),
            columns=MultiIndex.from_tuples(list(zip(
                list('WWWWXXXX'), list('yyzzyyzz'), list('cdcdcdcd'))),
            names=['www', 'yyy', 'ccc']))

        res = df.unstack()
        tm.assert_frame_equal(res, exp)

        res = df.unstack('ccc')
        tm.assert_frame_equal(res, exp)

        exp = tm.SubclassedDataFrame([
            [10, 30, 11, 31, 12.0, 32.0, 13.0, 33.0],
            [20, 40, 21, 41, 22.0, 42.0, 23.0, 43.0]],
            index=Index(['c', 'd'], name='ccc'),
            columns=MultiIndex.from_tuples(list(zip(
                list('WWWWXXXX'), list('yyzzyyzz'), list('ABABABAB'))),
                names=['www', 'yyy', 'aaa']))

        res = df.unstack('aaa')
        tm.assert_frame_equal(res, exp)