I know that chained-assignment in pandas is definitely a hot topic and there are a huge amount of questions on it but I am still unable to find a solution that works in my case.
I am working with irradiance and pv time series data (pandas dataframe with DateTimeIndex). There are holes in my series, some during night-time others during day-time. I would like to replace all the NaNs during the night-time with zeros because it would make sense (irradiance and pv production during night are null).
What I came up with so far is something like:
hour_range = [*range(17, 24)] + [*range(0, 9)]
mask = df['irradiance'].isna() & df['irradiance'].index.hour.isin(hour_range)
df.loc[mask, 'irradiance'] = 0
I tried also other solutions, like combining between_time
with fill_na
or using directly df.mask
with the in_place
option but I keep getting the dreaded SettingWithCopyWarning
. I decided not to use between_time
because it does not return a boolean series and does not allow combinining easily multiple conditions. Maybe I am wrong on this.
I would like to modify the df in_place for memory efficiency.
Is there a cleaner and safer solution to my problem?
Thanks.