1
1
"""
2
2
Test metadata propagation in groupby
3
3
4
- The PandasTable class below is implemented according to the [guidelines], and as such would
5
- expect `__finalize__` to always be called so that the `_pandastable_metadata` is always populated.
4
+ The PandasTable class below is implemented according to the [guidelines],
5
+ and as such would expect `__finalize__` to always be called so that the
6
+ `_pandastable_metadata` is always populated.
6
7
7
8
[guidelines]: https://pandas.pydata.org/pandas-docs/stable/development/extending.html#override-constructor-properties
8
9
"""
@@ -38,19 +39,21 @@ def __finalize__(self, other, method=None, **kwargs):
38
39
"""
39
40
This method is responsible for populating metadata when creating new Table-object.
40
41
41
- The method argument is subject to change, and a robust handling of this is implemented
42
+ The method argument is subject to change, and a robust handling of this
43
+ is implemented.
42
44
"""
43
- src = [other ] #more logic here in actual implementation
44
- metadata = _combine_metadata ([d .get_metadata () for d in src if isinstance (d , PandasTable )])
45
+ src = [other ] # more logic here in actual implementation
46
+ metadata = _combine_metadata (
47
+ [d .get_metadata () for d in src if isinstance (d , PandasTable )])
45
48
46
49
if not metadata :
47
- warn ('__finalize__ unable to combine metadata for method "{method}", falling back to DataFrame' )
50
+ warn ('__finalize__ unable to combine metadata for method "{method}", '
51
+ 'falling back to DataFrame' )
48
52
return pd .DataFrame (self )
49
53
object .__setattr__ (self , _TABLE_METADATA_FIELD_NAME , metadata )
50
54
return self
51
55
52
56
def get_metadata (self ):
53
- #return object.__getattribute__(self, _TABLE_METADATA_FIELD_NAME)
54
57
metadata = getattr (self , _TABLE_METADATA_FIELD_NAME , None )
55
58
if metadata is None :
56
59
warn ('PandasTable object not correctly initialized: no metadata' )
@@ -65,16 +68,16 @@ def from_table_data(df: pd.DataFrame, metadata) -> 'PandasTable':
65
68
66
69
@pytest .fixture
67
70
def dft ():
68
- df = pd .DataFrame ([[11 , 12 , 0 ], [21 , 22 , 0 ], [31 , 32 , 1 ]], columns = {'a' ,'b' ,'g' })
69
- return PandasTable .from_table_data (df , 'My metadata' )
71
+ df = pd .DataFrame ([[11 , 12 , 0 ], [21 , 22 , 0 ], [31 , 32 , 1 ]], columns = {'a' , 'b' , 'g' })
72
+ return PandasTable .from_table_data (df , 'My metadata' )
70
73
71
74
72
75
def test_initial_metadata (dft ):
73
76
assert dft .get_metadata () == 'My metadata'
74
77
75
78
76
79
def test_basic_propagation (dft ):
77
- gg = dft .loc [dft .g == 0 , :]
80
+ gg = dft .loc [dft .g == 0 , :]
78
81
assert gg .get_metadata () == 'My metadata'
79
82
80
83
0 commit comments