worldbank_as_dataframe 0.1.1 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +63 -0
- data/lib/worldbank_as_dataframe/client.rb +11 -11
- data/lib/worldbank_as_dataframe/commodities.rb +39 -0
- data/lib/worldbank_as_dataframe/version.rb +1 -1
- data/lib/worldbank_as_dataframe.rb +13 -14
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1141c280f36ed7062973307d4020dce4dc9800a942461b9f9333d68599fd1a4
|
4
|
+
data.tar.gz: f506f93a82cc9d85e00ec518e6a1cf88f6ce5dba88fd9c409e28db7ddfbe5c79
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de67152e06b713eff2488cead10307c8b19fb19da27912bae2236849a7c6a43ab8fb1ed729d57a45b578589c9b4f7cb7f7d5aff07bf5acebebed6a245df98073
|
7
|
+
data.tar.gz: b5fc9be7e5419a206b9cfd99202eb0f96b8ea6661285a35ca47aec7c552e98e0080dd586fdb76ba2d45668ca9c22b86bebf3025383ee7815c8b61e5cce720080
|
data/README.md
CHANGED
@@ -96,6 +96,69 @@ include WorldbankAsDataframe
|
|
96
96
|
# modifiers (like #dates above) called as class methods or chained in a query.
|
97
97
|
#
|
98
98
|
|
99
|
+
WorldbankAsDataframe::Commodities.new.fetch
|
100
|
+
# =>
|
101
|
+
# shape: (777, 72)
|
102
|
+
# ┌────────────┬────────────────────────────┬──────────────────────────┬──────────────────────────┬───┬─────────────┬──────────────────┬──────────────────────┬────────────────────┐
|
103
|
+
# │ Timestamps ┆ Crude oil, average ($/bbl) ┆ Crude oil, Brent ($/bbl) ┆ Crude oil, Dubai ($/bbl) ┆ … ┆ Zinc ($/mt) ┆ Gold ($/troy oz) ┆ Platinum ($/troy oz) ┆ Silver ($/troy oz) │
|
104
|
+
# │ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │
|
105
|
+
# │ date ┆ f64 ┆ f64 ┆ f64 ┆ ┆ f64 ┆ f64 ┆ f64 ┆ f64 │
|
106
|
+
# ╞════════════╪════════════════════════════╪══════════════════════════╪══════════════════════════╪═══╪═════════════╪══════════════════╪══════════════════════╪════════════════════╡
|
107
|
+
# │ 1960-01-01 ┆ 1.63 ┆ 1.63 ┆ 1.63 ┆ … ┆ 260.8 ┆ 35.27 ┆ 83.5 ┆ 0.9137 │
|
108
|
+
# │ 1960-02-01 ┆ 1.63 ┆ 1.63 ┆ 1.63 ┆ … ┆ 244.9 ┆ 35.27 ┆ 83.5 ┆ 0.9137 │
|
109
|
+
# │ 1960-03-01 ┆ 1.63 ┆ 1.63 ┆ 1.63 ┆ … ┆ 248.7 ┆ 35.27 ┆ 83.5 ┆ 0.9137 │
|
110
|
+
# │ 1960-04-01 ┆ 1.63 ┆ 1.63 ┆ 1.63 ┆ … ┆ 254.6 ┆ 35.27 ┆ 83.5 ┆ 0.9137 │
|
111
|
+
# │ 1960-05-01 ┆ 1.63 ┆ 1.63 ┆ 1.63 ┆ … ┆ 253.8 ┆ 35.27 ┆ 83.5 ┆ 0.9137 │
|
112
|
+
# │ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … │
|
113
|
+
# │ 2024-05-01 ┆ 81.445 ┆ 81.995 ┆ 83.53 ┆ … ┆ 2959.13 ┆ 2351.13 ┆ 1014.68 ┆ 29.36 │
|
114
|
+
# │ 2024-06-01 ┆ 81.205 ┆ 82.555 ┆ 82.17 ┆ … ┆ 2809.19 ┆ 2326.44 ┆ 985.08 ┆ 29.577 │
|
115
|
+
# │ 2024-07-01 ┆ 83.258667 ┆ 85.296 ┆ 83.94 ┆ … ┆ 2777.27 ┆ 2398.2 ┆ 978.8 ┆ 29.773 │
|
116
|
+
# │ 2024-08-01 ┆ 78.121 ┆ 80.863 ┆ 77.95 ┆ … ┆ 2714.08 ┆ 2470.15 ┆ 945.36 ┆ 28.53 │
|
117
|
+
# │ 2024-09-01 ┆ 72.424333 ┆ 74.293 ┆ 73.43 ┆ … ┆ 2837.02 ┆ 2570.55 ┆ 966.7 ┆ 30.131 │
|
118
|
+
# └────────────┴────────────────────────────┴──────────────────────────┴──────────────────────────┴───┴─────────────┴──────────────────┴──────────────────────┴────────────────────┘
|
119
|
+
|
120
|
+
WorldbankAsDataframe::Commodities.new('crude').fetch
|
121
|
+
# =>
|
122
|
+
# shape: (777, 5)
|
123
|
+
# ┌────────────┬────────────────────────────┬──────────────────────────┬──────────────────────────┬────────────────────────┐
|
124
|
+
# │ Timestamps ┆ Crude oil, average ($/bbl) ┆ Crude oil, Brent ($/bbl) ┆ Crude oil, Dubai ($/bbl) ┆ Crude oil, WTI ($/bbl) │
|
125
|
+
# │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
|
126
|
+
# │ date ┆ f64 ┆ f64 ┆ f64 ┆ f64 │
|
127
|
+
# ╞════════════╪════════════════════════════╪══════════════════════════╪══════════════════════════╪════════════════════════╡
|
128
|
+
# │ 1960-01-01 ┆ 1.63 ┆ 1.63 ┆ 1.63 ┆ null │
|
129
|
+
# │ 1960-02-01 ┆ 1.63 ┆ 1.63 ┆ 1.63 ┆ null │
|
130
|
+
# │ 1960-03-01 ┆ 1.63 ┆ 1.63 ┆ 1.63 ┆ null │
|
131
|
+
# │ 1960-04-01 ┆ 1.63 ┆ 1.63 ┆ 1.63 ┆ null │
|
132
|
+
# │ 1960-05-01 ┆ 1.63 ┆ 1.63 ┆ 1.63 ┆ null │
|
133
|
+
# │ … ┆ … ┆ … ┆ … ┆ … │
|
134
|
+
# │ 2024-05-01 ┆ 81.445 ┆ 81.995 ┆ 83.53 ┆ 78.81 │
|
135
|
+
# │ 2024-06-01 ┆ 81.205 ┆ 82.555 ┆ 82.17 ┆ 78.89 │
|
136
|
+
# │ 2024-07-01 ┆ 83.258667 ┆ 85.296 ┆ 83.94 ┆ 80.54 │
|
137
|
+
# │ 2024-08-01 ┆ 78.121 ┆ 80.863 ┆ 77.95 ┆ 75.55 │
|
138
|
+
# │ 2024-09-01 ┆ 72.424333 ┆ 74.293 ┆ 73.43 ┆ 69.55 │
|
139
|
+
# └────────────┴────────────────────────────┴──────────────────────────┴──────────────────────────┴────────────────────────┘
|
140
|
+
|
141
|
+
WorldbankAsDataframe::Commodities.new('gold').fetch(start: '1980-01-01', fin: '2000-12-31')
|
142
|
+
# =>
|
143
|
+
# shape: (252, 2)
|
144
|
+
# ┌────────────┬──────────────────┐
|
145
|
+
# │ Timestamps ┆ Gold ($/troy oz) │
|
146
|
+
# │ --- ┆ --- │
|
147
|
+
# │ date ┆ f64 │
|
148
|
+
# ╞════════════╪══════════════════╡
|
149
|
+
# │ 1980-01-01 ┆ 675.31 │
|
150
|
+
# │ 1980-02-01 ┆ 665.32 │
|
151
|
+
# │ 1980-03-01 ┆ 553.58 │
|
152
|
+
# │ 1980-04-01 ┆ 517.41 │
|
153
|
+
# │ 1980-05-01 ┆ 513.8 │
|
154
|
+
# │ … ┆ … │
|
155
|
+
# │ 2000-08-01 ┆ 274.47 │
|
156
|
+
# │ 2000-09-01 ┆ 273.68 │
|
157
|
+
# │ 2000-10-01 ┆ 270.0 │
|
158
|
+
# │ 2000-11-01 ┆ 266.01 │
|
159
|
+
# │ 2000-12-01 ┆ 271.45 │
|
160
|
+
# └────────────┴──────────────────┘
|
161
|
+
|
99
162
|
```
|
100
163
|
|
101
164
|
|
@@ -1,15 +1,15 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '/source'))
|
2
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '/income_level'))
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '/lending_type'))
|
4
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '/country'))
|
5
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '/indicator'))
|
6
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '/topic'))
|
7
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '/region'))
|
8
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '/query'))
|
9
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '/data'))
|
10
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '/param_query'))
|
11
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '/data_query'))
|
1
12
|
require 'httparty'
|
2
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'source'))
|
3
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'income_level'))
|
4
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'lending_type'))
|
5
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'country'))
|
6
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'indicator'))
|
7
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'topic'))
|
8
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'region'))
|
9
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'query'))
|
10
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'data'))
|
11
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'param_query'))
|
12
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'data_query'))
|
13
13
|
|
14
14
|
module WorldbankAsDataframe
|
15
15
|
class Client
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'simple_xlsx_reader'
|
3
|
+
require 'polars-df'
|
4
|
+
|
5
|
+
module WorldbankAsDataframe
|
6
|
+
class Commodities
|
7
|
+
attr_reader :tag
|
8
|
+
|
9
|
+
def initialize(series = nil, options={})
|
10
|
+
@tag = series
|
11
|
+
end
|
12
|
+
|
13
|
+
def fetch(start: nil, fin: nil)
|
14
|
+
doc = SimpleXlsxReader.parse(URI.open('https://thedocs.worldbank.org/en/doc/5d903e848db1d1b83e0ec8f744e55570-0350012021/related/CMO-Historical-Data-Monthly.xlsx'))
|
15
|
+
ary = doc.sheets.detect{|sht| sht.name == 'Monthly Prices'}.rows.to_a[4..-1]
|
16
|
+
ary[0][0] = 'Timestamps'
|
17
|
+
ary.map!{|a| a.reverse.drop_while(&:nil?).reverse }
|
18
|
+
ary[1].length.times {|i| ary[1][i] = [ary[0][i], ary[1][i]].compact.join(' ') }
|
19
|
+
ary = ary[1..-1]
|
20
|
+
|
21
|
+
dat = ary[1..-1]
|
22
|
+
cols = ary[0]
|
23
|
+
|
24
|
+
dat.each{|a| dt = a[0].split('M'); a[0] = Date.new(dt[0].to_i, dt[1].to_i, 1).to_date }
|
25
|
+
dat.each{|row| row.map!{|c| c.in?(["…","..."]) ? nil : c }}
|
26
|
+
|
27
|
+
df = Polars::DataFrame.new(dat.transpose, columns: cols)
|
28
|
+
df = df.filter(Polars.col('Timestamps') >= start.to_date) unless start.nil?
|
29
|
+
df = df.filter(Polars.col('Timestamps') <= fin.to_date) unless fin.nil?
|
30
|
+
|
31
|
+
unless @tag.nil?
|
32
|
+
cols = cols.map{|c| (/#{tag.downcase}/ =~ c.downcase).nil? ? nil : c }.compact
|
33
|
+
df = df.select(['Timestamps', cols].flatten) if (cols.length>0)
|
34
|
+
end
|
35
|
+
|
36
|
+
df
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -1,21 +1,20 @@
|
|
1
1
|
require File.expand_path(File.join(File.dirname(__FILE__), '/worldbank_as_dataframe/client'))
|
2
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '/worldbank_as_dataframe/commodities'))
|
2
3
|
|
3
4
|
module WorldbankAsDataframe
|
4
|
-
|
5
5
|
def self.client(query={}, raw=false)
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
# Delegate to WorldbankAsDataframe::Client.new
|
12
|
-
def self.method_missing(method, *args, &block)
|
13
|
-
return super unless client.respond_to?(method)
|
14
|
-
client.send(method, *args, &block)
|
15
|
-
end
|
6
|
+
defaults = {:params => {:format => :json}, :dirs => []}
|
7
|
+
defaults.merge!(query)
|
8
|
+
WorldbankAsDataframe::Client.new(defaults, raw)
|
9
|
+
end
|
16
10
|
|
17
|
-
|
18
|
-
|
19
|
-
|
11
|
+
# Delegate to WorldbankAsDataframe::Client.new
|
12
|
+
def self.method_missing(method, *args, &block)
|
13
|
+
return super unless client.respond_to?(method)
|
14
|
+
client.send(method, *args, &block)
|
15
|
+
end
|
20
16
|
|
17
|
+
def self.respond_to?(method, include_private=false)
|
18
|
+
client.respond_to?(method, include_private) || super(method, include_private)
|
19
|
+
end
|
21
20
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: worldbank_as_dataframe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bill McKinnon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-10-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ZenTest
|
@@ -177,6 +177,7 @@ files:
|
|
177
177
|
- bin/setup
|
178
178
|
- lib/worldbank_as_dataframe.rb
|
179
179
|
- lib/worldbank_as_dataframe/client.rb
|
180
|
+
- lib/worldbank_as_dataframe/commodities.rb
|
180
181
|
- lib/worldbank_as_dataframe/country.rb
|
181
182
|
- lib/worldbank_as_dataframe/data.rb
|
182
183
|
- lib/worldbank_as_dataframe/data_query.rb
|
@@ -238,7 +239,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
238
239
|
- !ruby/object:Gem::Version
|
239
240
|
version: '0'
|
240
241
|
requirements: []
|
241
|
-
rubygems_version: 3.
|
242
|
+
rubygems_version: 3.3.7
|
242
243
|
signing_key:
|
243
244
|
specification_version: 4
|
244
245
|
summary: A Ruby wrapper around the World Bank's Development Indicators API
|