14.12.2 Loading a PgxFrame from Client-Side Data
You can also load PgxFrame
(s) directly from client-side data.
The following describes the steps to load a PgxFrame
from
client-side data:
- Create a Session and an Analyst.
See step-1 in Loading a PgxFrame from a Database for the code examples.
- Define a frame schema to load a
PgxFrame
from client side data. For example, the following shows a frame schema defined with various data types:Defining a Frame Schema Using JShellopg4j> var exampleFrameSchema = List.of( columnDescriptor("name", DataTypes.STRING_TYPE), columnDescriptor("age", DataTypes.INTEGER_TYPE), columnDescriptor("salary", DataTypes.DOUBLE_TYPE), columnDescriptor("married", DataTypes.BOOLEAN_TYPE), columnDescriptor("tax_rate", DataTypes.FLOAT_TYPE), columnDescriptor("random", DataTypes.LONG_TYPE), columnDescriptor("date_of_birth", DataTypes.LOCAL_DATE_TYPE) )
Defining a Frame Schema Using JavaList<ColumnDescriptor> exampleFrameSchema = Arrays.asList( columnDescriptor("name", DataTypes.STRING_TYPE), columnDescriptor("age", DataTypes.INTEGER_TYPE), columnDescriptor("salary", DataTypes.DOUBLE_TYPE), columnDescriptor("married", DataTypes.BOOLEAN_TYPE), columnDescriptor("tax_rate", DataTypes.FLOAT_TYPE), columnDescriptor("random", DataTypes.LONG_TYPE), columnDescriptor("date_of_birth", DataTypes.LOCAL_DATE_TYPE) );
Defining a Frame Schema Using Pythonexample_frame_schema = [ ("name", "STRING_TYPE"), ("age", "INTEGER_TYPE"), ("salary", "DOUBLE_TYPE"), ("married", "BOOLEAN_TYPE"), ("tax_rate", "FLOAT_TYPE"), ("random", "LONG_TYPE"), ("date_of_birth", "LOCAL_DATE_TYPE") ]
- Define data as per the schema.
Defining Data for the Schema Using JShell
opg4j> Map<String, Iterable<?>> exampleFrameData = Map.of( "name", Arrays.asList("Alice", "Bob", "Charlie"), "age", Arrays.asList(25, 27, 29), "salary", Arrays.asList(10000.0, 15000.0, 20000.0), "married", Arrays.asList(false, false, true), "tax_rate", Arrays.asList(0.21, 0.26, 0.32), "random", Arrays.asList(2394293898324L, 45640604960495L, 12312323409087654L), "date_of_birth", Arrays.asList( LocalDate.of(1990, 9, 15), LocalDate.of(1991, 11, 4), LocalDate.of(1993, 10, 4) ) );
Defining Data for the Schema Using JavaMap<String, Iterable<?>> exampleFrameData = new HashMap<>(); exampleFrameData.put("name", Arrays.asList("Alice", "Bob", "Charlie")); exampleFrameData.put("age", Arrays.asList(25, 27, 29)); exampleFrameData.put("salary", Arrays.asList(10000.0, 15000.0, 20000.0)); exampleFrameData.put("married", Arrays.asList(false, false, true)); exampleFrameData.put("tax_rate", Arrays.asList(0.21, 0.26, 0.32)); exampleFrameData.put("random", Arrays.asList(2394293898324L, 45640604960495L, 12312323409087654L)); exampleFrameData.put("date_of_birth", Arrays.asList(LocalDate.of(1990, 9, 15), LocalDate.of(1991, 11, 4), LocalDate.of(1993, 10, 4) ) );
Defining Data for the Schema Using Pythonfrom datetime import date example_frame_data = { "name": ["Alice", "Bob", "Charlie"], "age": [25, 27, 29], "salary": [10000.0, 15000.0, 20000.0], "married": [False, False, True], "tax_rate": [0.21, 0.26, 0.32], "random": [2394293898324, 45640604960495, 12312323409087654], "date_of_birth": [date(1990, 9, 15), date(1991, 11, 4), date(1993, 10, 4)] }
- Load the frame as shown:
Loading the Frame Using JShell
opg4j> var exampleFrame = session.createFrame(exampleFrameSchema, exampleFrameData, "example frame");
Loading the Frame Using JavaPgxFrame exampleFrame = session.createFrame(exampleFrameSchema, exampleFrameData, "example frame");
Loading the Frame Using Pythonexample_frame=session.create_frame(example_frame_schema,example_frame_data,'example frame')
- You can also load the frame incrementally as you receive more data:
Incrementally Loading the Frame Using JShell
opg4j> var exampleFrameBuilder = session.createFrameBuilder(exampleFrameSchema); opg4j> exampleFrameBuilder.addRows(exampleFrameData) opg4j> Map<String, Iterable<?>> exampleFrameDataPart2 = Map.of( "name", Arrays.asList("Dave"), "age", Arrays.asList(26), "salary", Arrays.asList(18000.0), "married", Arrays.asList(true), "tax_rate", Arrays.asList(0.30), "random", Arrays.asList(456783423423L), "date_of_birth", Arrays.asList(LocalDate.of(1989, 9, 15)) ); opg4j> exampleFrameBuilder.addRows(exampleFrameDataPart2); opg4j> var exampleFrame = exampleFrameBuilder.build("example frame");
Incrementally Loading the Frame Using JavaPgxFrameBuilder exampleFrameBuilder = session.createFrameBuilder(exampleFrameSchema); exampleFrameBuilder.addRows(exampleFrameData); Map<String, Iterable<?>> exampleFrameDataPart2 = new HashMap<>(); exampleFrameDataPart2.put("name", Arrays.asList("Dave")); exampleFrameDataPart2.put("age", Arrays.asList(26)); exampleFrameDataPart2.put("salary", Arrays.asList(18000.0)); exampleFrameDataPart2.put("married", Arrays.asList(true)); exampleFrameDataPart2.put("tax_rate", Arrays.asList(0.30)); exampleFrameDataPart2.put("random", Arrays.asList(456783423423L)); exampleFrameDataPart2.put("date_of_birth", Arrays.asList(LocalDate.of(1989, 9, 15)) ); exampleFrameBuilder.addRows(exampleFrameDataPart2); PgxFrame exampleFrame = exampleFrameBuilder.build("example frame");
Incrementally Loading the Frame Using Pythonexample_frame_builder = session.create_frame_builder(example_frame_schema) example_frame_builder.add_rows(example_frame_data) example_frame_data_part_2 = { "name": ["Dave"], "age": [26], "salary": [18000.0], "married": [True], "tax_rate": [0.30], "random": [456783423423], "date_of_birth": [date(1989, 9, 15)] } example_frame_builder.add_rows(example_frame_data_part_2) example_frame = example_frame_builder.build("example frame")
- Finally, you can also load a frame from a Pandas dataframe in Python as
shown:
Loading the Frame from
Pandas
dataframeimport pandas as pd example_pandas_dataframe = pd.DataFrame(data=example_frame_data) example_frame = session.pandas_to_pgx_frame(example_pandas_dataframe, "example frame")
You can also create a graph from the PgxFrame
(s) . See Creating a Graph from Multiple PgxFrame Objects for more information.
Parent topic: PgxFrames Tabular Data-Structure