added monthly_values.py
This commit is contained in:
parent
2ad4b856fd
commit
07443138d8
47
helpers/monthly_values.py
Normal file
47
helpers/monthly_values.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
"""
|
||||||
|
Monthly values
|
||||||
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
||||||
|
Copyright © 2020 Project Author Pilar Monsalvete pilar_monsalvete@yahoo.es
|
||||||
|
"""
|
||||||
|
|
||||||
|
import pandas as pd
|
||||||
|
import numpy as np
|
||||||
|
import calendar as cal
|
||||||
|
|
||||||
|
|
||||||
|
class MonthlyValues:
|
||||||
|
def __init__(self):
|
||||||
|
self._month_hour = None
|
||||||
|
|
||||||
|
def get_mean_values(self, values):
|
||||||
|
out = None
|
||||||
|
if values is not None:
|
||||||
|
if 'month' not in values.columns:
|
||||||
|
values = pd.concat([self.month_hour, pd.DataFrame(values)], axis=1)
|
||||||
|
out = values.groupby('month', as_index=False).mean()
|
||||||
|
del out['month']
|
||||||
|
return out
|
||||||
|
|
||||||
|
def get_total_month(self, values):
|
||||||
|
out = None
|
||||||
|
if values is not None:
|
||||||
|
if 'month' not in values.columns:
|
||||||
|
values = pd.concat([self.month_hour, pd.DataFrame(values)], axis=1)
|
||||||
|
out = pd.DataFrame(values).groupby('month', as_index=False).sum()
|
||||||
|
del out['month']
|
||||||
|
return out
|
||||||
|
|
||||||
|
@property
|
||||||
|
def month_hour(self):
|
||||||
|
"""
|
||||||
|
returns a DataFrame that has x values of the month number (January = 1, February = 2...),
|
||||||
|
being x the number of hours of the corresponding month
|
||||||
|
:return: DataFrame(int)
|
||||||
|
"""
|
||||||
|
array = []
|
||||||
|
for i in range(0, 12):
|
||||||
|
days_of_month = cal.monthrange(2015, i+1)[1]
|
||||||
|
total_hours = days_of_month * 24
|
||||||
|
array = np.concatenate((array, np.full(total_hours, i + 1)))
|
||||||
|
self._month_hour = pd.DataFrame(array, columns=['month'])
|
||||||
|
return self._month_hour
|
4
tmp/.gitignore
vendored
Normal file
4
tmp/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
# Ignore everything in this directory
|
||||||
|
*
|
||||||
|
# Except this file
|
||||||
|
!.gitignore
|
4
unittests/tests_data/.gitignore
vendored
Normal file
4
unittests/tests_data/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
# Ignore everything in this directory
|
||||||
|
*
|
||||||
|
# Except this file
|
||||||
|
!.gitignore
|
Loading…
Reference in New Issue
Block a user