<s> from __future__ import print_function <EOL> from pandas import DataFrame , Series , MultiIndex , Panel <EOL> import pandas as pd <EOL> import pandas . util . testing as tm <EOL> from pandas . tests . frame . common import TestData <EOL> class TestDataFrameSubclassing ( tm . TestCase , TestData ) : <EOL> _multiprocess_can_split_ = True <EOL> def test_frame_subclassing_and_slicing ( self ) : <EOL> class CustomSeries ( Series ) : <EOL> @ property <EOL> def _constructor ( self ) : <EOL> return CustomSeries <EOL> def custom_series_function ( self ) : <EOL> return '<STR_LIT:OK>' <EOL> class CustomDataFrame ( DataFrame ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kw ) : <EOL> super ( CustomDataFrame , self ) . __init__ ( * args , ** kw ) <EOL> @ property <EOL> def _constructor ( self ) : <EOL> return CustomDataFrame <EOL> _constructor_sliced = CustomSeries <EOL> def custom_frame_function ( self ) : <EOL> return '<STR_LIT:OK>' <EOL> data = { '<STR_LIT>' : range ( <NUM_LIT:10> ) , <EOL> '<STR_LIT>' : range ( <NUM_LIT:10> ) } <EOL> cdf = CustomDataFrame ( data ) <EOL> self . assertTrue ( isinstance ( cdf , CustomDataFrame ) ) <EOL> cdf_series = cdf . col1 <EOL> self . assertTrue ( isinstance ( cdf_series , CustomSeries ) ) <EOL> self . assertEqual ( cdf_series . custom_series_function ( ) , '<STR_LIT:OK>' ) <EOL> cdf_rows = cdf [ <NUM_LIT:1> : <NUM_LIT:5> ] <EOL> self . assertTrue ( isinstance ( cdf_rows , CustomDataFrame ) ) <EOL> self . assertEqual ( cdf_rows . custom_frame_function ( ) , '<STR_LIT:OK>' ) <EOL> mcol = pd . MultiIndex . from_tuples ( [ ( '<STR_LIT:A>' , '<STR_LIT:A>' ) , ( '<STR_LIT:A>' , '<STR_LIT:B>' ) ] ) <EOL> cdf_multi = CustomDataFrame ( [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:2> , <NUM_LIT:3> ] ] , columns = mcol ) <EOL> self . assertTrue ( isinstance ( cdf_multi [ '<STR_LIT:A>' ] , CustomDataFrame ) ) <EOL> mcol = pd . MultiIndex . from_tuples ( [ ( '<STR_LIT:A>' , '<STR_LIT>' ) , ( '<STR_LIT:B>' , '<STR_LIT>' ) ] ) <EOL> cdf_multi2 = CustomDataFrame ( [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:2> , <NUM_LIT:3> ] ] , columns = mcol ) <EOL> self . assertTrue ( isinstance ( cdf_multi2 [ '<STR_LIT:A>' ] , CustomSeries ) ) <EOL> def test_dataframe_metadata ( self ) : <EOL> df = tm . SubclassedDataFrame ( { '<STR_LIT:X>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , '<STR_LIT:Y>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } , <EOL> index = [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) <EOL> df . testattr = '<STR_LIT>' <EOL> self . assertEqual ( df . testattr , '<STR_LIT>' ) <EOL> self . assertEqual ( df [ [ '<STR_LIT:X>' ] ] . testattr , '<STR_LIT>' ) <EOL> self . assertEqual ( df . loc [ [ '<STR_LIT:a>' , '<STR_LIT:b>' ] , : ] . testattr , '<STR_LIT>' ) <EOL> self . assertEqual ( df . iloc [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , : ] . testattr , '<STR_LIT>' ) <EOL> self . assertEqual ( df . iloc [ <NUM_LIT:0> : <NUM_LIT:1> , : ] . testattr , '<STR_LIT>' ) <EOL> unpickled = self . round_trip_pickle ( df ) <EOL> tm . assert_frame_equal ( df , unpickled ) <EOL> self . assertEqual ( df . _metadata , unpickled . _metadata ) <EOL> self . assertEqual ( df . testattr , unpickled . testattr ) <EOL> def test_indexing_sliced ( self ) : <EOL> df = tm . SubclassedDataFrame ( { '<STR_LIT:X>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <EOL> '<STR_LIT:Y>' : [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:9> ] } , <EOL> index = [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) <EOL> res = df . loc [ : , '<STR_LIT:X>' ] <EOL> exp = tm . SubclassedSeries ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , index = list ( '<STR_LIT:abc>' ) , name = '<STR_LIT:X>' ) <EOL> tm . assert_series_equal ( res , exp ) <EOL> tm . assertIsInstance ( res , tm . SubclassedSeries ) <EOL> res = df . iloc [ : , <NUM_LIT:1> ] <EOL> exp = tm . SubclassedSeries ( [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] , index = list ( '<STR_LIT:abc>' ) , name = '<STR_LIT:Y>' ) <EOL> tm . assert_series_equal ( res , exp ) <EOL> tm . assertIsInstance ( res , tm . SubclassedSeries ) <EOL> res = df . ix [ : , '<STR_LIT>' ] <EOL> exp = tm . SubclassedSeries ( [ <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:9> ] , index = list ( '<STR_LIT:abc>' ) , name = '<STR_LIT>' ) <EOL> tm . assert_series_equal ( res , exp ) <EOL> tm . assertIsInstance ( res , tm . SubclassedSeries ) <EOL> res = df . loc [ '<STR_LIT:a>' , : ] <EOL> exp = tm . SubclassedSeries ( [ <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:7> ] , index = list ( '<STR_LIT>' ) , name = '<STR_LIT:a>' ) <EOL> tm . assert_series_equal ( res , exp ) <EOL> tm . assertIsInstance ( res , tm . SubclassedSeries ) <EOL> res = df . iloc [ <NUM_LIT:1> , : ] <EOL> exp = tm . SubclassedSeries ( [ <NUM_LIT:2> , <NUM_LIT:5> , <NUM_LIT:8> ] , index = list ( '<STR_LIT>' ) , name = '<STR_LIT:b>' ) <EOL> tm . assert_series_equal ( res , exp ) <EOL> tm . assertIsInstance ( res , tm . SubclassedSeries ) <EOL> res = df . ix [ '<STR_LIT:c>' , : ] <EOL> exp = tm . SubclassedSeries ( [ <NUM_LIT:3> , <NUM_LIT:6> , <NUM_LIT:9> ] , index = list ( '<STR_LIT>' ) , name = '<STR_LIT:c>' ) <EOL> tm . assert_series_equal ( res , exp ) <EOL> tm . assertIsInstance ( res , tm . SubclassedSeries ) <EOL> def test_to_panel_expanddim ( self ) : <EOL> class SubclassedFrame ( DataFrame ) : <EOL> @ property <EOL> def _constructor_expanddim ( self ) : <EOL> return SubclassedPanel <EOL> class SubclassedPanel ( Panel ) : <EOL> pass <EOL> index = MultiIndex . from_tuples ( [ ( <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:0> , <NUM_LIT:2> ) ] ) <EOL> df = SubclassedFrame ( { '<STR_LIT:X>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , '<STR_LIT:Y>' : [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] } , index = index ) <EOL> result = df . to_panel ( ) <EOL> self . assertTrue ( isinstance ( result , SubclassedPanel ) ) <EOL> expected = SubclassedPanel ( [ [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] , [ [ <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] ] ] , <EOL> items = [ '<STR_LIT:X>' , '<STR_LIT:Y>' ] , major_axis = [ <NUM_LIT:0> ] , <EOL> minor_axis = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] , <EOL> dtype = '<STR_LIT>' ) <EOL> tm . assert_panel_equal ( result , expected ) <EOL> def test_subclass_attr_err_propagation ( self ) : <EOL> class A ( DataFrame ) : <EOL> @ property <EOL> def bar ( self ) : <EOL> return self . i_dont_exist <EOL> with tm . assertRaisesRegexp ( AttributeError , '<STR_LIT>' ) : <EOL> A ( ) . bar </s>
