Skip to content

Instantly share code, notes, and snippets.

@monocongo
monocongo / mssql_df_upsert.py
Created September 26, 2023 15:04 — forked from gordthompson/mssql_df_upsert.py
Build a T-SQL MERGE statement and upsert a DataFrame
# Copyright 2023 Gordon D. Thompson, gord@gordthompson.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
@monocongo
monocongo / xarray_stack_groupby_apply_unstack_spi.ipynb
Last active January 3, 2023 23:26
Use xarray for parallel computation with stack-groupby-apply-unstack
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@monocongo
monocongo / start_jupyter_pyspark.sh
Created July 29, 2022 01:06 — forked from BryanCutler/start_jupyter_pyspark.sh
How to start a Jupyter Notebook with PySpark Kernel
#!/usr/bin/env bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
@monocongo
monocongo / gist:853f49fec2705ab12cd94bfd20b38cf7
Created June 22, 2022 11:12
Below we demonstrate how to utilize [sbpy](https://sbpy.readthedocs.io/en/latest/index.html) to transform an unpacked designation to packed format
import pandas as pd
from sbpy.data import Names
# function to create the new packed format column
def pack_designation(
row: pd.Series,
) -> pd.Series:
return pd.Series(
data=[Names.to_packed(row.pdes)],
index=['Principal_desig'],
@monocongo
monocongo / compute_correlation_matrix.py
Created March 10, 2022 14:25 — forked from cameres/compute_correlation_matrix.py
Compute Pandas Correlation Matrix of a Spark Data Frame
from pyspark.mllib.stat import Statistics
import pandas as pd
# result can be used w/ seaborn's heatmap
def compute_correlation_matrix(df, method='pearson'):
# wrapper around
# https://forums.databricks.com/questions/3092/how-to-calculate-correlation-matrix-with-all-colum.html
df_rdd = df.rdd.map(lambda row: row[0:])
corr_mat = Statistics.corr(df_rdd, method=method)
corr_mat_df = pd.DataFrame(corr_mat,
@monocongo
monocongo / prefix_all_files_in_directory.py
Created January 13, 2020 16:49
Rename all files in a directory with a prefix
import os
directory = "/home/james/images"
extension = ".jpg"
prefix = "prefix_"
for filename in os.listdir(directory):
if not filename.endswith(extension):
continue
new_filename = prefix + filename
old_file_path = os.path.join(directory, filename)
new_file_path = os.path.join(directory, new_filename)
@monocongo
monocongo / ray_test.ipynb
Created December 18, 2019 15:37
Example of using ray for parallization, not working as expected (10x slower than comparable loop implementation)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@monocongo
monocongo / ray_example_numpy.ipynb
Last active November 30, 2019 18:18
Inability to assign values into shared memory objects using ray, i.e. it results in ValueError: assignment destination is read-only
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@monocongo
monocongo / venv_basics.md
Created July 3, 2019 14:11
Python virtual environment basics

Configure Python development environment

Install pip, virtualenv, and virtualenvwrapper
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python3 get-pip.py
$ rm get-pip.py
$ sudo pip install virtualenv virtualenvwrapper
Update .bashrc
@monocongo
monocongo / conda_environment_management.md
Created July 2, 2019 19:48
Build *conda environment, save requirements.txt and environment.yml

Create and activate a new conda environment:

$ conda create -n myenv python=3 --yes
$ conda activate myenv

Add the conda-forge channel into the conda configuration:

$ conda config --add channels conda-forge

Add packages into the environment using both conda and pip: