Skip to content

Instantly share code, notes, and snippets.

View aleksandr-kosobokov's full-sized avatar

Aleksandr Kosobokov aleksandr-kosobokov

View GitHub Profile
@lebedov
lebedov / msgpack_pandas_attrib.py
Created July 23, 2014 16:48
Serialize/unserialize a class with a pandas data structure attribute using msgpack.
#!/usr/bin/env python
"""
Serialize/unserialize a class with a pandas data structure attribute using msgpack.
"""
import msgpack
import numpy as np
import pandas as pd
@bsweger
bsweger / useful_pandas_snippets.md
Last active June 14, 2025 19:01
Useful Pandas Snippets

Useful Pandas Snippets

A personal diary of DataFrame munging over the years.

Data Types and Conversion

Convert Series datatype to numeric (will error if column has non-numeric values)
(h/t @makmanalp)

@gjreda
gjreda / gist:7433f5f70299610d9b6b
Last active April 11, 2023 16:23
pandas' read_csv parse_dates vs explicit date conversion
# When you're sure of the format, it's much quicker to explicitly convert your dates than use `parse_dates`
# Makes sense; was just surprised by the time difference.
import pandas as pd
from datetime import datetime
to_datetime = lambda d: datetime.strptime(d, '%m/%d/%Y %H:%M')
%time trips = pd.read_csv('data/divvy/Divvy_Trips_2013.csv', parse_dates=['starttime', 'stoptime'])
# CPU times: user 1min 29s, sys: 331 ms, total: 1min 29s
# Wall time: 1min 30s
@ampedandwired
ampedandwired / Vagrantfile
Created January 14, 2015 05:09
Configure a Vagrant VM to piggyback off CNTLM running on the host
def get_proxy_url
# Doesn't support different proxies for different protocols at present
host_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY'] || ENV['https_proxy'] || ENV["HTTPS_PROXY"]
if host_proxy
uri = URI(host_proxy)
if ['localhost', '127.0.0.1'].include? uri.host
# 10.0.2.2 is the default vagrant gateway and should connect to the host OS.
# Confirm this by running 'netstat -r' in the guest.
host_proxy = host_proxy.sub(uri.host, '10.0.2.2')
end
@kevin-peel
kevin-peel / SimpleMinify.py
Last active February 2, 2016 09:12
A very short and very simple Python script that removes new lines and extra spaces from text files. I created it to do a simple minify of large JavaScript files that browser-based tools were too slow to do.
file = open("minify1.txt", "w")
with open('test.txt') as f:
for line in f:
newTxt = line.rstrip('\r\n').replace(" ","")
file.write(newTxt)
if 'str' in line:
break
file.close()
@DrPaulBrewer
DrPaulBrewer / unfubarcsv.py
Last active February 2, 2016 09:09
Attempts rational removal of inner commas and inner quotes from csv file fields
# Copyright 2015 Paul Brewer Economic and Financial Technology Consulting LLC
# Released under the MIT Public License
# LICENSE: http://opensource.org/licenses/MIT
# Purpose: rationally removes inner commas and inner quotes from csv file fields
# Useful for Google BigQuery as of 2015-03 does not support quoted commas in CSV fields
# python ./unf
@ryan-hill
ryan-hill / dbf2DF.py
Last active August 23, 2024 08:27
Import DBF file to Pandas data frame in Python
import pysal as ps
import pandas as pd
'''
Arguments
---------
dbfile : DBF file - Input to be imported
upper : Condition - If true, make column heads upper case
'''
def dbf2DF(dbfile, upper=True): #Reads in DBF files and returns Pandas DF
db = ps.open(dbfile) #Pysal to open DBF
@ankurk91
ankurk91 / install-node-js.sh
Last active November 4, 2024 05:29
Install node-js, npm and yarn on Ubuntu/Mac using nvm
#!/bin/sh
# Install node and npm via nvm - https://github.com/nvm-sh/nvm
# Run this script like - bash script-name.sh
# Define versions
INSTALL_NODE_VER=22
INSTALL_NVM_VER=0.40.1
@m-ou-se
m-ou-se / replace-debian-with-arch.txt
Last active June 18, 2025 08:44
Instructions to replace a live Debian installation with Arch
# Download latest archlinux bootstrap package, see https://www.archlinux.org/download/
wget 'ftp://ftp.nluug.nl/pub/os/Linux/distr/archlinux/iso/latest/archlinux-bootstrap-*-x86_64.tar.gz'
# Make sure you'll have enough entropy for pacman-key later.
apt-get install haveged
# Install the arch bootstrap image in a tmpfs.
mount -t tmpfs none /mnt
cd /mnt
tar xvf ~/archlinux-bootstrap-*-x86_64.tar.gz --strip-components=1
@maxivak
maxivak / _readme.md
Last active June 8, 2022 06:19
Vagrant with Ubuntu 16.04 in VirtualBox

Setup Ubuntu 16.04 to be used with Vagrant and Virtualbox

Prepare Vagrant box with Ubuntu 16.04

We will use official box "ubuntu/xenial64" and modify it to work with Vagrant.

  • Vagrantfile