import pandas as pd
from pandas.core.dtypes.missing import construct_1d_array_from_inferred_fill_value

data = {
    'Product': ['Fancy Chair', 'Fancy Chair', 'Luxury Sofa', 'Designer Table', 'Luxury Sofa'],
    'Color': ['Blue', 'Green', 'Blue', 'Green', 'Red'],
    'Customer Price': [2345.89, 2390.50, 1820.00, 3100.00, 2750.00],
    'Non-Customer Price': [2445.89, 2495.50, 1980.00, 3400.00, 2850.00]
}

df = pd.DataFrame(data)
print(df)

# pivot erlaubt keine Duplikate!
pivoted_df = df.pivot(index="Product",
                      columns="Color",
                      values="Non-Customer Price")

print(pivoted_df)

pivoted_df = df.pivot(index="Product",
                      columns="Color",
                      values=["Non-Customer Price", "Customer Price"])

print(pivoted_df)

beverages = pd.read_csv("../../data/beverages.csv")
beverages["Day"] = (["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"] * 35)[:103]
print(beverages)

# eine aggfunc für duplikate mitgeben
#   pivot_table ~= gorupby().aggfunc().unstack()
coffe_pivot = beverages.pivot_table(
    index="Name",
    columns="Day",
    values="Coffee",
    aggfunc="mean",
    fill_value=0
).round(1)
print(coffe_pivot)
# als groupby
coffees = beverages[["Name", "Day", "Coffee"]].groupby(["Name", "Day"]).mean().unstack().round(1)
print(coffees)
coffees[coffees.isna()] = 0.0
print(coffees)
# 1. Energiedaten
# ["Year_Quarter"] = ["Year"].astype(str) + ["Quarter"]  # 2019 + "Q1"
# Pro Type für jedes Quartal die Produktion gruppieren
# Pivot -> Zeilen: (Jahr, Q1) Spalten: Biomass, WInd

# df.index <- name der index-spalte
print("\n"*3)
energy_df = pd.read_csv("../../data/germany_energy_mix_2019_2024.csv")
# 1) Neue spalte
energy_df["Year Quarter"] = energy_df["Year"].astype(str) + " " + energy_df['Quarter']
# drop: axis=0 die zeilen  namens [Year, quarter] gelöscht
#       axis=1 die spalten namens [Year, quarter] gelöscht
energy_df.drop(["Year", "Quarter"], axis=1, inplace=True)
print(energy_df)

# 1) Mit 'Year Quarter' gruppieren
quarterly_data = energy_df[["Year Quarter", "Energy_Source", "Generation_TWh"]].groupby(
    ["Year Quarter", "Energy_Source"]).sum().unstack()
print(quarterly_data)

# 2) Pivot_table
quarterly_data = energy_df.pivot_table(
    index="Year Quarter",
    columns="Energy_Source",
    values="Generation_TWh",
    aggfunc="sum",
    fill_value=0, # für jeden energietyp in jedem quartal ein wert existiert
)
print(quarterly_data)


