-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataframe.py
More file actions
142 lines (118 loc) · 4.27 KB
/
dataframe.py
File metadata and controls
142 lines (118 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""
This script is used to create the database (basically a pandas.DataFrame)
and define functions for
* handling the database
* analyse data
* create plots
Author: Dominik Imgrüth
"""
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from itertools import repeat
# define the columns of interest
cols = ['Date', 'Time', 'Duration(min)', 'Observer', 'Bird', 'Count']
# create empty dataframe
bird_count_df = pd.DataFrame(columns=cols)
def create_db_entry(date, time, dur: float, observer: str, data_dict: dict):
"""
creates a database entry for observations made during one bird count period
:param date: date on which the counting was conducted
:param time: starting time of the counting period
:param dur: float, duration of the counting period
:param observer: str, name of the observer
:param data_dict: dict, this is the bird counting data
keys: str, bird species
values: int, amount of birds
:return: pandas.DataFrame
"""
keys = data_dict.keys()
values = data_dict.values()
n = len(keys)
# create lists of length n with the same entry
date_list = list(repeat(date, n))
time_list = list(repeat(time, n))
dur_list = list(repeat(dur, n))
obs_list = list(repeat(observer, n))
# using zip, prepare data for creating a pandas.DataFrame with it
data = list(zip(date_list, time_list, dur_list, obs_list, keys, values))
return pd.DataFrame(data, columns=cols)
def open_db(path):
"""
a function to open an existing database; if it not exists, it will be created
:param path: must be in pickle format (.pkl)
:return: pandas.DataFrame
"""
if type(path) != str:
print("path must be string")
exit()
try:
df = pd.read_pickle(path)
except:
# create new dataframe
df = bird_count_df
df.to_pickle(path)
return df
def add_to_db(db, df):
"""
function for adding new observations to an existing database
:param db: database (pandas.Dataframe) to which the observations should be added
:param df: observations (pandas.Dataframe) to be added to db
:return: None
"""
active_db = open_db(db)
new_db = active_db.append(df, ignore_index=True)
new_db.to_pickle(db)
def save_report(df, name):
"""
saves the report as .csv file
:param df: data (pandas.Dataframe) to be saved
:param name: file name
:return: None
"""
path = 'reports/' + name + '.csv'
df.to_csv(path)
def plot_df(df):
"""
wrapper function for matplotlib pyplot.bar plotting function
:param df: birdy dataframe (pandas.Dataframe) holding data to be plotted
:return: None, opens a matplotlib window
"""
# define a color for each observer
# first get all observers as unique list
observers = df['Observer'].unique()
# get some colors from mcolors (repeat it 5 times to be sure there are enough colors)
col = list(mcolors.TABLEAU_COLORS.values())*5
# write colors in a dictionary (keys = observers, values = colors)
colors = {}
for i,obs in enumerate(observers):
colors[obs] = col[i]
# now produce handles for the legend
handles = [plt.Rectangle((0, 0), 1, 1, color=colors[l]) for l in observers]
# create bar plot with bird species and amount
plt.bar(df['Bird'], df['Count'], color=[colors[i] for i in df['Observer']])
# include title and axes labels
plt.title('Total bird counts', fontsize=14)
plt.xlabel('Species', fontsize=14)
plt.ylabel('Amount of sightings', fontsize=14)
# include a grid
plt.grid(True)
# rotate the names of the species by 45°
plt.xticks(rotation = 70)
# add legend
plt.legend(handles, observers, title="Observer")
# use a tight layout to see full x-Lables
plt.tight_layout()
# show the plot
plt.show()
def plot_df_year(df, year: int = 2023):
"""
wrapper function for plot_df: plots only a specified year
:param df: birdy dataframe (pandas.Dataframe) holding data to be plotted
:param year: int, year to be plotted
:return: None, opens a matplotlib window
"""
# translate 'Date' (str) to datetime and the extract the year
df_year = df[pd.to_datetime(df['Date']).dt.year == year]
# call plot_df on new selected year
plot_df(df_year)