Skip to content

Instantly share code, notes, and snippets.

View himat's full-sized avatar
💭
Making

Hima himat

💭
Making
View GitHub Profile
@himat
himat / val_in_df_col.py
Last active July 2, 2019 00:40
[Check if value in df col] You have to check if `val in col.values`, not just `val in col` #pandas #dangerous
# Doing `x in df["col"]` will always look for x in the *index* of the df, not the actual column values
# Thus, you have to use `x in df["col"].values` instead
# This can be especially insidious when you're checking if a number is in a col, but that number is also in the index.
# If you don't use `.values` in this case, you will think the value is in the col when it actually isn't!
>>> df = pd.DataFrame({"a": [20, 40, 1], "b": [32, 42, 52]})
>>> df
a b
0 20 32
1 40 42
@himat
himat / filter_pd_series.py
Created July 2, 2019 18:03
[Filter a Pandas series] Use .where(.) chaining #pandas
test = {
383: 3.000000,
663: 1.000000,
726: 1.000000,
737: 9.000000,
833: 8.166667
}
pd.Series(test).where(lambda x : x!=1).dropna()
@himat
himat / alfred_tips.md
Last active July 3, 2019 05:00
[Alfred useful tips] Things to use #alfred #mac

Google I'm Feeling Lucky search to select the first link on the search results page automatically

A lot of times for searches that have one main result, it will usually be the first result on the search results page, and you end up just clicking that first link. You can save time and mental capacity by using this more efficient method.
So use this if you pretty much know what you're looking for.

  • You don't even need to have your browser open to do this!
  • And this way, you don't need to remember URLs either, just need to know the description of the site!

With the I'm Feeling Lucky keyword set to l, just type in Alfred something like:

  • l wiki ontology
  • l github
  • l pytorch docs
@himat
himat / mock_func.py
Last active July 8, 2019 19:07
[Mock any python object/function] Create a fake object/function to disable an object/function that is called later #python
# Assume our code has some function that is called later (possibly called multiple times).
# In the code below, we have a logging function, and we don't want to always use the logger.
# Instead of adding an `if disable_logging` line every time we log something with the logging function,
# we can instead just replace the logging function with a fake function that does nothing.
def mock_logger(*args, **kwargs):
pass
if disable_logging:
logger = mock_logger
@himat
himat / cell.js
Last active April 3, 2020 15:16
[ipynb useful first cell lines] Useful commands to put in the top cells of an ipython notebook #python #notebook
%reload_ext autoreload
%autoreload 2
import os, sys
import pathlib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
@himat
himat / conda.md
Created September 30, 2019 01:45
[conda commands sheet] Useful conda commands #packages

Create an env from scratch

conda create --name envname

Create from an env file

conda env create -f environment.yml

List all packages installed in this conda env

conda list

List all conda envs on this system

@himat
himat / adder_closure.go
Created December 7, 2019 04:34
[Using closures to maintain state] Nice abstraction to add closures instead of having to maintain separate state variables within the main body #golang
import "fmt"
func adder() func(int) int {
sum := 0
return func(x int) int {
sum += x
return sum
}
}
@himat
himat / nan_tripup.py
Created March 9, 2020 18:10
[Don't use `is` to check for NaN in Python] Always use `np.isnan(.)` #python
>>> import pandas as pd
>>> import numpy as np
>>>
>>> s = pd.Series([10, 11, 12])
>>>
>>> s[2] = np.nan
>>> s[2] is np.nan
False
# A NaN type can get coerced which is what happened above, and when this happens, a separate instance is created that thus has a different id(.) which maks the `is` check fail
@himat
himat / links.md
Created March 13, 2020 18:38
[Rasterio useful things] Links to useful ways of doing things #rasterio #GIS #python
@himat
himat / things.md
Created April 18, 2020 23:51
[Useful git tips] Things for git #git

Aborting Git Commits And Rebases

When you are amending a commit or doing an interactive rebase of a series of commits, Vim will open and when you close the file, git will continue with the procedure. But if you want to cancel the commit or rebase?

Vim allows you to quit with an error code.

:cq

This means that irrespective of the content of the buffer, Vim will signal to Git with an error code to not process the commit or rebase, effectively aborting the action.