Skip to content

Instantly share code, notes, and snippets.

@fanannan
Last active April 2, 2020 10:49
Show Gist options
  • Save fanannan/d894ef2d7add67e88c57f1aa25f03b9d to your computer and use it in GitHub Desktop.
Save fanannan/d894ef2d7add67e88c57f1aa25f03b9d to your computer and use it in GitHub Desktop.
iloc and loc functions for pandas DataFrame in Clojure
; Clojupyter Snippet
(require '[clojupyter.misc.helper :as helper])
(helper/add-dependencies '[cnuernber/libpython-clj "1.36"])
(require '[libpython-clj.require :refer [require-python]]
'[libpython-clj.python :refer [py. py.. py.- $. $.. $a $c
as-python as-jvm
->python ->jvm
get-attr call-attr call-attr-kw
get-item att-type-map
call call-kw initialize!
as-numpy as-tensor ->numpy
run-simple-string
add-module module-dict
import-module
python-type] :as py])
(py/initialize! :python-executable "/..../anaconda3/envs/py37/bin/python"
:library-path "/..../anaconda3/envs/py37/lib/libpython3.7m.so")
(defonce builtins (py/import-module "builtins"))
(defn slice
([]
(py. builtins slice nil))
([start]
(py. builtins slice start))
([start stop]
(py. builtins slice start stop))
([start stop incr]
(py. builtins slice start stop incr)))
(defn iloc [df & {:keys [index column] :or {index (slice) column (slice)}}]
(py. (py. df iloc) __getitem__ [index column]))
(defn loc [df & {:keys [index column] :or {index (slice) column (slice)}}]
(py. (py. df loc) __getitem__ [index column]))
(require-python '[datetime :as dt])
(require-python '[yfinance :as yf])
(require-python '[pandas :as pd])
(defn get-meta [sym] (eval (list 'meta (list 'var sym)))) ; to be rewritten
(defn get-meta-name [sym] (clojure.string/upper-case (str (:name (get-meta sym)))))
(defn rename-close [quoted-symbol]
(let [name (get-meta-name quoted-symbol)]
(-> (py. (eval quoted-symbol) rename :columns {"Adj Close" name})
(py. "__getitem__" name) ; in a case of (py/$.. name) name not to be evaled
)))
(def spy ^{:name "SPY"} (yf/download "^GSPC"))
(def vix ^{:name "VIX"} (yf/download "^VIX"))
(def gld ^{:name "GLD"} (yf/download "GLD"))
(def gdx ^{:name "GDX"} (yf/download "GDX"))
(def df (pd/concat (map #(rename-close %)['spy 'gdx 'gld 'vix]) :axis 1))
;; iloc samples
(println (iloc df :index 2))
(println (iloc df :column 2))
; notice the diffs below
(println (iloc df :index '(2 12)))
(println (iloc df :index (slice 2 12))) ; 2, 3, 4,... 11
(println (iloc df :column '(0 3)))
(println (iloc df :column (slice 0 3))) ; 0, 1, 2
(println (iloc df :index '(2 12) :column '(1 3)))
;; loc samples
(println (loc df :index (dt/datetime 2020 3 26)))
(println (loc df :column "GLD"))
; notice the diffs below
(println (loc df :index [(dt/datetime 2020 3 2) (dt/datetime 2020 3 26) (dt/datetime 2020 3 30)]))
(println (loc df :index (slice (dt/datetime 2020 3 2) (dt/datetime 2020 3 30))))
(println (loc df :column ["GLD" "GDX"]))
(println (loc df :index (slice (dt/datetime 2020 3 2) (dt/datetime 2020 3 6)) :column '("GLD" "GDX")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment