File tree 2 files changed +43
-0
lines changed
spec/API_specification/examples
2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ from typing import Callable , Any , cast
2
+
3
+ from dataframe_api import EagerColumn
4
+
5
+ my_plotting_function : Callable [[Any , Any ], Any ]
6
+
7
+ def group_by_and_plot (x_any : Any , y_any : Any , color_any : Any ) -> None :
8
+ x = cast (EagerColumn [Any ], x_any .__column_consortium_standard__ ())
9
+ y = cast (EagerColumn [Any ], y_any .__column_consortium_standard__ ())
10
+ color = cast (EagerColumn [Any ], color_any .__column_consortium_standard__ ())
11
+
12
+ namespace = x .__column_namespace__ ()
13
+
14
+ df = namespace .dataframe_from_dict ({"x" : x , "y" : y , "color" : color })
15
+
16
+ agg = df .groupby ("color" ).mean ().collect ()
17
+ x = agg .get_column ("x" ).to_array_object (namespace .Float64 )
18
+ y = agg .get_column ("y" ).to_array_object (namespace .Float64 )
19
+
20
+ my_plotting_function (x , y )
Original file line number Diff line number Diff line change
1
+ from typing import Any , cast
2
+
3
+ from dataframe_api import DataFrame
4
+
5
+ df : DataFrame
6
+ namespace = df .__dataframe_namespace__ ()
7
+ col = namespace .col
8
+
9
+ # You can select columns using column names or expressions
10
+ # the following are all valid
11
+ df .select ('a' )
12
+ df .select ('a' , 'b' )
13
+ df .select (col ('a' ))
14
+ df .select ((col ('a' )+ 1 ).rename ('b' ))
15
+
16
+ # You can filter using expressions
17
+ df = df .filter (col ('width' ) > col ('height' ))
18
+
19
+ # EagerColumn can be thought of as a trivial expression.
20
+ # So, filtering using EagerColumn works too, though is less readable
21
+ df_eager = df .collect ()
22
+ df_eager = df_eager .filter (df_eager .get_column ('width' ) > df_eager .get_column ('height' ))
23
+
You can’t perform that action at this time.
0 commit comments