Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Recommend.Games
Blog
Commits
d9c10241
Verified
Commit
d9c10241
authored
Oct 24, 2020
by
Markus Shepherd
🙊
Browse files
first version of adjust.py
parent
1b686521
Changes
3
Hide whitespace changes
Inline
Side-by-side
experiments/votes_by_year/.gitignore
0 → 100644
View file @
d9c10241
bgg_RatingItem.csv
experiments/votes_by_year/adjust.py
0 → 100644
View file @
d9c10241
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.6.0
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# %%
import
json
from
datetime
import
date
from
pathlib
import
Path
import
matplotlib.pyplot
as
plt
import
pandas
as
pd
from
pytility
import
parse_date
SEED
=
23
# %matplotlib inline
# %load_ext nb_black
# %load_ext lab_black
# %%
def
process_row
(
item
,
columns
=
(
"item_id"
,
"bgg_id"
,
"bgg_user_name"
,
"bgg_user_owned"
,
"bgg_user_play_count"
,
"bgg_user_rating"
,
"updated_at"
,
),
):
item
=
json
.
loads
(
item
)
if
isinstance
(
item
,
dict
)
and
all
(
item
.
get
(
column
)
is
not
None
for
column
in
columns
):
return
{
column
:
item
[
column
]
for
column
in
columns
}
return
None
def
df_from_jl
(
path
):
with
open
(
path
)
as
f
:
records
=
filter
(
None
,
map
(
process_row
,
f
))
df
=
pd
.
DataFrame
.
from_records
(
records
,
index
=
"item_id"
)
df
[
"year"
]
=
df
.
updated_at
.
apply
(
lambda
x
:
x
[:
4
]).
astype
(
"int32"
)
return
df
# %%
path_csv
=
Path
(
"bgg_RatingItem.csv"
).
resolve
()
path_jl
=
Path
(
"../../../board-game-data/scraped/bgg_RatingItem.jl"
).
resolve
()
# %%
try
:
df
=
pd
.
read_csv
(
path_csv
,
index_col
=
"item_id"
,
dtype
=
{
"item_id"
:
"object"
})
except
Exception
:
df
=
df_from_jl
(
path_jl
)
df
.
shape
# %%
df
.
sample
(
5
,
random_state
=
SEED
).
T
# %%
if
not
path_csv
.
exists
():
df
.
to_csv
(
path_csv
)
# %%
by_year
=
df
.
groupby
(
"year"
).
bgg_id
.
count
()
by_year
# %%
now
=
date
.
today
()
eoy
=
date
(
now
.
year
,
12
,
31
)
# %%
eoy_adj
=
by_year
[
now
.
year
]
*
eoy
.
timetuple
().
tm_yday
/
now
.
timetuple
().
tm_yday
eoy_adj
# %%
by_year
[
by_year
.
index
<
now
.
year
].
plot
(
style
=
"k-"
,
linewidth
=
3
)
plt
.
plot
([
now
.
year
-
1
,
now
.
year
],
[
by_year
[
now
.
year
-
1
],
eoy_adj
],
"k:"
,
linewidth
=
3
)
# %%
plt
.
savefig
(
"ratings_by_year.svg"
)
experiments/votes_by_year/ratings_by_year.svg
0 → 100644
View file @
d9c10241
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Created with matplotlib (https://matplotlib.org/) -->
<svg
height=
"288pt"
version=
"1.1"
viewBox=
"0 0 432 288"
width=
"432pt"
xmlns=
"http://www.w3.org/2000/svg"
xmlns:xlink=
"http://www.w3.org/1999/xlink"
>
<metadata>
<rdf:RDF
xmlns:cc=
"http://creativecommons.org/ns#"
xmlns:dc=
"http://purl.org/dc/elements/1.1/"
xmlns:rdf=
"http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>
<cc:Work>
<dc:type
rdf:resource=
"http://purl.org/dc/dcmitype/StillImage"
/>
<dc:date>
2020-10-24T08:36:31.994929
</dc:date>
<dc:format>
image/svg+xml
</dc:format>
<dc:creator>
<cc:Agent>
<dc:title>
Matplotlib v3.3.2, https://matplotlib.org/
</dc:title>
</cc:Agent>
</dc:creator>
</cc:Work>
</rdf:RDF>
</metadata>
<defs>
<style
type=
"text/css"
>
*{stroke-linecap:butt;stroke-linejoin:round;}
</style>
</defs>
<g
id=
"figure_1"
>
<g
id=
"patch_1"
>
<path
d=
"M 0 288
L 432 288
L 432 0
L 0 0
z
"
style=
"fill:none;"
/>
</g>
</g>
</svg>
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment