Skip to content

Instantly share code, notes, and snippets.

@sbob909
sbob909 / useful_pandas_snippets.md
Last active July 20, 2020 15:50 — forked from bsweger/useful_pandas_snippets.md
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)

<!-- Ignore the standard Argus form -->
<ag-dashboard style="display:none">
</ag-dashboard>
<!-- Google Charts loader for Argus, depends on jQuery -->
<div>
<h4>Debug output</h4>
<div id="debugDiv">
</div>
</div>
// class for any valid Argus metric expression
function ArgusData(urlBase, validMetricExploreExpression){
this.base = urlBase + "/argusws/metrics?expression=",
this.expression = validMetricExploreExpression,
this.url = this.base + this.expression,
this.queryArgus.bind(this), // IIFE to initalize this.dataTable
this.pivotView = function(precision, groupColNum, filterValue, valueColNum){
// helper
function round(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
@sbob909
sbob909 / gist:3ae65105194dbe808cabe88c72bacf31
Created January 29, 2017 14:31
JavaScript rounding helper
// helper to round values without rounding errors
function round(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
@sbob909
sbob909 / openInvoices.page
Created October 24, 2013 16:52
Force.com Visualforce page code sample: Streaming API page for openInvoices to support Building Efficient Visualforce Pages workshop
<apex:page showHeader="true" controller="invoiceController" tabstyle="Invoice__c" sidebar="false">
<apex:includeScript value="{!$Resource.cometd}"/>
<apex:includeScript value="{!$Resource.jquery}"/>
<apex:includeScript value="{!$Resource.json2}"/>
<apex:includeScript value="{!$Resource.jquery_cometd}"/>
<script type="text/javascript">
(function($){
$(document).ready(function() {
$.cometd.init({
url: window.location.protocol+'//'+window.location.hostname+'/cometd/24.0/',
@sbob909
sbob909 / gist:1886593
Created February 22, 2012 18:44
Sample Data for Apex Workbook Spring 2012
Merchandise__c[] ml = new List<Merchandise__c>();
Merchandise__c m = new Merchandise__c(
Name='Pencils',
Description__c='Cool pencils',
Price__c=1.5,
Total_Inventory__c=1000);
ml.add(m);
Merchandise__c m2 = new Merchandise__c(
Name='Notebooks',
Description__c='Blue notebooks',
@sbob909
sbob909 / gist:1855055
Created February 17, 2012 19:36
Apex Plug-In for Force.com Cloud Flow Designer Workbook
global class MortgageCalculator implements Process.Plugin
{
global Process.PluginResult invoke(Process.PluginRequest request)
{
Double amount = (Double)request.inputParameters.get('Amount');
Double term = (Double)request.inputParameters.get('Term');
// Magic here
Double cMonthlyPayment;
cMonthlyPayment=2750;
Map<String, Object> result = new Map<String, Object>();
@sbob909
sbob909 / TestDeleteRestrictAlbums
Created August 8, 2011 18:58
Delete Restrict Trigger Unit Test Class (Apex)
@isTest
private class TestDeleteRestrictAlbums {
// Random number generator
static double getRandomNumber() {
return Math.random();
}
// Album generator, with or without a track
static Album__c createNewAlbum(Boolean withTrack) {
// Create test album and insert it into the database
@sbob909
sbob909 / Apex
Created August 8, 2011 18:57
Delete Restrict Trigger Example (Apex)
trigger DeleteRestrictAlbums on Album__c (before delete) {
// With each of the albums targeted by the trigger that have tracks,
// add an error to prevent them from being deleted.
for (Album__c targetAlbum : [SELECT Id
FROM Album__c
WHERE Id IN (SELECT Album__c FROM Track__c) AND
Id IN :Trigger.old]){
Trigger.oldMap.get(targetAlbum.id).addError(
'Cannot delete album with tracks');