def hours_per_month_to_json(trainings_per_month): month_names = dict(Aika.objects.values_list('kk', 'kk_nimi').distinct()) hours = trainings_per_month.pivot( index='kk', columns='vuosi', values='kesto').sort_values(by='kk').rename(index=month_names) hours = dataframe_to_dict(hours) return json.dumps(hours)
def hours_per_sport_group_to_json(trainings_df): hours = trainings_df.groupby(['vuosi', 'laji_ryhma']).sum().reset_index()[[ 'vuosi', 'laji_ryhma', 'kesto' ]].round(1) hours_pivot = hours.pivot(index='laji_ryhma', columns='vuosi', values='kesto').sort_values(by='laji_ryhma') hours_pivot = dataframe_to_dict(hours_pivot) return json.dumps(hours_pivot)
def test_df_with_rows(self): categories = ['A', 'B'] data = {'col1': [1, 2], 'col2': [3, 4]} df = pd.DataFrame(data, index=categories) result = [{ "category": "A", "series": { "col1": 1, "col2": 3 } }, { "category": "B", "series": { "col1": 2, "col2": 4 } }] self.assertEqual(dataframe_to_dict(df), result)
def hours_per_zone_to_json(trainings_df, user_id): zones = list( Teho.objects.filter(harjoitus_id__user=user_id).values_list( 'tehoalue_id__tehoalue', flat=True).distinct().order_by('tehoalue_id__jarj_nro')) + ['Muu'] zones_objects = Teho.objects.filter( harjoitus_id__user=user_id).values_list( 'harjoitus_id', 'harjoitus_id__aika_id__vuosi', 'tehoalue_id__tehoalue', 'kesto') if not zones_objects: return json.dumps([]) zones_df = pd.DataFrame(list(zones_objects), columns=['harjoitus_id', 'vuosi', 'teho', 'kesto']).fillna(np.nan) zones_df = zones_df.merge(trainings_df[['id', 'kausi']], how='inner', left_on='harjoitus_id', right_on='id') zones_df['kesto'] = zones_df['kesto'].astype(float) # count training hours without zone defined zones_per_training = zones_df[['harjoitus_id', 'kesto']].groupby('harjoitus_id').sum() zones_per_training = trainings_df.merge(zones_per_training, how='left', left_on='id', right_index=True, suffixes=('', '_zone'))[[ 'id', 'vuosi', 'kausi', 'kesto', 'kesto_zone' ]].fillna(0) zones_per_training[ 'Muu'] = zones_per_training['kesto'] - zones_per_training['kesto_zone'] zone_not_defined_per_year = zones_per_training[[ 'vuosi', 'Muu' ]].groupby('vuosi').sum().round(1).reset_index() zone_not_defined_per_season = zones_per_training[ zones_per_training['kausi'] != 0][[ 'kausi', 'Muu' ]].groupby('kausi').sum().round(1).reset_index() # count training hours per zone per year zones_per_year = zones_df.groupby(['vuosi', 'teho']).sum().reset_index() zones_per_year = zones_per_year.pivot(index='vuosi', columns='teho', values='kesto').round(1) zones_per_year.index = zones_per_year.index.astype(str) zones_per_year = zone_not_defined_per_year.merge( zones_per_year, how='left', left_on='vuosi', right_on='vuosi').set_index('vuosi') zones_per_year = zones_per_year[zones] # count training hours per zone per season zones_per_season = zones_df.groupby(['kausi', 'teho']).sum().reset_index() if not zones_per_season.empty: zones_per_season = zones_per_season.pivot(index='kausi', columns='teho', values='kesto').round(1) zones_per_season.index = zones_per_season.index.astype(str) zones_per_season = zone_not_defined_per_season.merge( zones_per_season, how='left', left_on='kausi', right_on='kausi').set_index('kausi') zones_per_season = zones_per_season.loc[:, [ z for z in zones if z in list(zones_per_season) ]] hours_per_zone = {} hours_per_zone['year'] = dataframe_to_dict(zones_per_year) hours_per_zone['season'] = dataframe_to_dict(zones_per_season) return json.dumps(hours_per_zone)
def hours_per_week_to_json(trainings_per_week): hours = trainings_per_week.pivot(index='vko', columns='vuosi', values='kesto').sort_values(by='vko') hours = dataframe_to_dict(hours) return json.dumps(hours)
def test_df_with_no_rows(self): df = pd.DataFrame(columns=['A', 'B', 'C']) self.assertEqual(dataframe_to_dict(df), [])
def test_empty_df(self): df = pd.DataFrame() self.assertEqual(dataframe_to_dict(df), [])
def reports_sports(request): """ Trainings reports per sport """ user_id = request.user.id if not Harjoitus.objects.filter(user=user_id): sport = '' sports = [] avg_per_sport = [] amounts_per_sport = [] avg_per_sport_table = [] hours_per_sport = [] kilometers_per_sport = [] else: sports = tr.sports_to_list(user_id) sport = sports[0] trainings_df = tr.trainings_base_to_df(user_id) trainings_per_sport_per_year = tr.trainings_per_sport_to_df( trainings_df, 'vuosi') trainings_per_sport_per_season = tr.trainings_per_sport_to_df( trainings_df, 'kausi') hours_per_sport = {'year': {}, 'season': {}} kilometers_per_sport = {'year': {}, 'season': {}} avg_per_sport = {'year': {}, 'season': {}} avg_per_sport_table = {'year': {}, 'season': {}} amounts_per_sport = {'year': {}, 'season': {}} for s in sports: data_per_year = trainings_per_sport_per_year[ trainings_per_sport_per_year['laji_nimi'].astype(str) == s] data_per_season = trainings_per_sport_per_season[ trainings_per_sport_per_season['laji_nimi'].astype(str) == s] if not data_per_year.empty: amounts_per_sport['year'][s] = data_per_year[[ 'vuosi', 'lkm', 'kesto (h)', 'matka (km)' ]].fillna('').to_dict(orient='records') avg_per_sport_table['year'][s] = data_per_year[[ 'vuosi', 'kesto (h) ka.', 'matka (km) ka.', 'vauhti (km/h)', 'keskisyke' ]].rename(columns={ 'kesto (h) ka.': 'kesto (h)', 'matka (km) ka.': 'matka (km)' }).fillna('').to_dict(orient='records') data_per_year = data_per_year.set_index('vuosi') hours_per_sport['year'][s] = utils.dataframe_to_dict( data_per_year[['kesto (h)']]) kilometers_per_sport['year'][s] = utils.dataframe_to_dict( data_per_year[['matka (km)']]) avg_per_sport['year'][s] = utils.dataframe_to_dict( data_per_year[['vauhti (km/h)', 'keskisyke']]) if not data_per_season.empty: amounts_per_sport['season'][s] = data_per_season[[ 'kausi', 'lkm', 'kesto (h)', 'matka (km)' ]].fillna('').to_dict(orient='records') avg_per_sport_table['season'][s] = data_per_season[[ 'kausi', 'kesto (h) ka.', 'matka (km) ka.', 'vauhti (km/h)', 'keskisyke' ]].rename(columns={ 'kesto (h) ka.': 'kesto (h)', 'matka (km) ka.': 'matka (km)' }).fillna('').to_dict(orient='records') data_per_season = data_per_season.set_index('kausi') hours_per_sport['season'][s] = utils.dataframe_to_dict( data_per_season[['kesto (h)']]) kilometers_per_sport['season'][s] = utils.dataframe_to_dict( data_per_season[['matka (km)']]) avg_per_sport['season'][s] = utils.dataframe_to_dict( data_per_season[['vauhti (km/h)', 'keskisyke']]) return render(request, 'reports_sports.html', context={ 'sport': sport, 'sports': sports, 'avg_per_sport': json.dumps(avg_per_sport), 'amounts_per_sport': json.dumps(amounts_per_sport), 'avg_per_sport_table': json.dumps(avg_per_sport_table), 'hours_per_sport': json.dumps(hours_per_sport), 'kilometers_per_sport': json.dumps(kilometers_per_sport) })