CEA-Liten / HeatPro

HeatPro Python Package: Generate Heat Demand Load Profile for District Heating
https://cea-liten.github.io/HeatPro/
Other
8 stars 0 forks source link

Error with external factors data file with named index #1

Closed mathieu-vallee closed 2 hours ago

mathieu-vallee commented 2 hours ago

Description

In a notebook similar to "notebooks/simple_usage.ipynb", I got a strange error when trying to use another source for "external factors.csv" (in my case, only external temperature).

The error is "'RangeIndex' object has no attribute 'date'", see stack trace below,

After investigating, it seems that removing the name of the index of the temperature dataframe solves the problem (it was the only difference I could find compared to the base example).

What I Did

I reproduced the behavior in simple_usage.ipynb by adding a name to the index, as follows :

df = pd.read_csv('external_factors.csv',index_col=0,parse_dates=True).iloc[:8760]

-> succeeds

df = pd.read_csv('my_file.csv',index_col=0,parse_dates=True) 
df.index.name = 'something'

-> fails

{
    "name": "AttributeError",
    "message": "'RangeIndex' object has no attribute 'date'",
    "stack": "---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[15], line 13
     10         hourly_mapping[day,hour] = 0.8
     11 hourly_mapping = {key:value/sum(hourly_mapping.values()) for key, value in hourly_mapping.items()}
---> 13 hourly_residential_profile = basic_building_heating_profile(
     14     felt_temperature=pd.DataFrame(external_factors.data[EXTERNAL_TEMPERATURE_NAME].ewm(24).mean().rename(BUILDING_FELT_TEMPERATURE_NAME)),
     15     non_heating_temperature=18,
     16     hourly_weight=apply_weekly_hourly_pattern(
     17         hourly_index=external_factors.data.index,
     18         hourly_mapping=hourly_mapping
     19                 )
     20 )
     22 hourly_residential_load = weekly_weighted_disaggregate(
     23                                 monthly_demand=monthly_building_load,
     24                                 weights=hourly_residential_profile,
     25                             )

File [...]\\heatpro\\demand_profile\\building_heating_profile.py:43, in basic_building_heating_profile(felt_temperature, non_heating_temperature, hourly_weight)
      8 def basic_building_heating_profile(felt_temperature: pd.DataFrame, non_heating_temperature: float,
      9                                    hourly_weight: pd.DataFrame) -> pd.DataFrame:
     10     r\"\"\"Create an hourly heating building consumption hourly profile adjusted to felt temperature. With sum over a month equals to 1.
     11 
     12     Args:
   (...)
     41     
     42     \"\"\"
---> 43     if not find_xor_hour(felt_temperature,hourly_weight).empty:
     44         raise ValueError(f\"felt_temperature and hourly_weight hours are not match\
 Difference (head(10)\
 {find_xor_hour(felt_temperature,hourly_weight).head(10)}\")
     46     felt_temperature.set_index(felt_temperature.index.to_period('h').start_time, inplace = True)

File [...]\\heatpro\\check\\check_data_format.py:135, in find_xor_hour(df_left, df_right)
    123 def find_xor_hour(df_left: pd.DataFrame, df_right: pd.DataFrame) -> pd.DataFrame:
    124     \"\"\"Find hours that are not in both index
    125 
    126     Args:
   (...)
    131         pd.DataFrame: Dataframe showing of hours that are not in both index
    132     \"\"\"
    133     df = pd.merge(
    134     pd.DataFrame({'Date':df_left.index.date,'Hour':df_left.index.hour}), 
--> 135     pd.DataFrame({'Date':df_right.index.date,'Hour':df_right.index.hour}), 
    136     on=['Date'], 
    137     how='outer', 
    138     indicator=True)
    139     df.index = df.reset_index(drop=True).index
    140     return df[df['_merge']!='both']

AttributeError: 'RangeIndex' object has no attribute 'date'"
}
RobinsonBeaucour commented 2 hours ago

Solved in 612d25e7066de62247abe8b8f46535c5e9d48b3b

mathieu-vallee commented 1 hour ago

Thanks for the quick fix @RobinsonBeaucour 👍