POC Data Management & Integration 3 — Questions and Answers
Question 1: Which method in SQLAlchemy's ORM is used to add a new object to the session for insertion into the database?
- session.insert(obj)
- session.add(obj) (Correct answer)
- session.push(obj)
- session.commit(obj)
Correct answer: session.add(obj)
session.add() stages a new ORM object for insertion; session.commit() then persists it to the database.
Question 2: What does the pandas 'groupby()' method return before an aggregation function is applied?
- A new DataFrame
- A GroupBy object (Correct answer)
- A Series
- A dict of DataFrames
Correct answer: A GroupBy object
groupby() returns a DataFrameGroupBy or SeriesGroupBy object that holds groups and waits for an aggregation call like .sum() or .mean().
Question 3: Which Python standard library module is used to read and write JSON data?
- csv
- pickle
- json (Correct answer)
- yaml
Correct answer: json
The built-in json module provides json.loads(), json.dumps(), json.load(), and json.dump() for JSON serialization.
Question 4: In pandas, what is the result of performing an 'outer' merge when a key exists in only one DataFrame?
- The row is dropped from the result
- NaN is inserted for missing values from the other DataFrame (Correct answer)
- An error is raised
- The key is duplicated in both DataFrames
Correct answer: NaN is inserted for missing values from the other DataFrame
An outer merge keeps all rows from both DataFrames and fills missing counterpart values with NaN.
Question 5: Which method serializes a Python object to a JSON-formatted string?
- json.load()
- json.dump()
- json.dumps() (Correct answer)
- json.encode()
Correct answer: json.dumps()
json.dumps() converts a Python object to a JSON string (the 's' suffix stands for 'string').
Question 6: What does the 'pivot_table()' function in pandas do?
- Rotates column names to row index
- Creates a spreadsheet-style pivot table as a DataFrame (Correct answer)
- Transposes the DataFrame's rows and columns
- Reindexes the DataFrame by a new column
Correct answer: Creates a spreadsheet-style pivot table as a DataFrame
pivot_table() aggregates data by one or more keys and produces a reshaped summary DataFrame, similar to Excel pivot tables.
Question 7: When using psycopg2 to connect to PostgreSQL, which parameter is NOT part of the standard connection string?
- host
- dbname
- charset (Correct answer)
- port
Correct answer: charset
psycopg2 uses host, dbname, user, password, and port; 'charset' is a MySQL/MariaDB connection concept, not PostgreSQL.
Which method in SQLAlchemy's ORM is used to add a new object to the session for insertion into the database?