tools

psql_insert_copy(table, conn, keys, data_iter)[source]

Execute SQL statement inserting data

Parameters:
  • table (pandas.io.sql.SQLTable)
  • conn (sqlalchemy.engine.Engine or sqlalchemy.engine.Connection)
  • keys (list of str) – Column names
  • data_iter (Iterable that iterates the values to be inserted)
random_ints_until_sum(s_sum, m_max)[source]

Generate non-negative random integers < m_max summing to s_sum.

random_point_in_square(geom, tol)[source]

Generate a random point within a square

Parameters:
  • geom (gpd.Series) – Geometries of square
  • tol (float) – tolerance to square bounds
Returns:

points (gpd.Series) – Series of random points

specific_int_until_sum(s_sum, i_int)[source]

Generate list i_int summing to s_sum. Last value will be <= i_int

timeit(func)[source]

Decorator for measuring function’s running time.

write_table_to_postgis(gdf, table, engine=Engine(postgresql+psycopg2://egon:***@127.0.0.1:59734/egon-data), drop=True)[source]

Helper function to append df data to table in db. Only predefined columns are passed. Error will raise if column is missing. Dtype of columns are taken from table definition.

Parameters:
  • gdf (gpd.DataFrame) – Table of data
  • table (declarative_base) – Metadata of db table to export to
  • engine – connection to database db.engine()
  • drop (bool) – Drop table before appending
write_table_to_postgres(df, db_table, drop=False, index=False, if_exists='append')[source]

Helper function to append df data to table in db. Fast string-copy is used. Only predefined columns are passed. If column is missing in dataframe a warning is logged. Dtypes of columns are taken from table definition. The writing process happens in a scoped session.

Parameters:
  • df (pd.DataFrame) – Table of data
  • db_table (declarative_base) – Metadata of db table to export to
  • drop (boolean, default False) – Drop db-table before appending
  • index (boolean, default False) – Write DataFrame index as a column.
  • if_exists ({‘fail’, ‘replace’, ‘append’}, default ‘append’) –
    • fail: If table exists, do nothing.
    • replace: If table exists, drop it, recreate it, and insert data.
    • append: If table exists, insert data. Create if does not exist.