У меня есть эта функция, которая прекрасно работает
def transform(df):
#find the interval by substracting the second date from the first one
a = df.loc[0, 'dtime']
b = df.loc[1, 'dtime']
c = a - b
minutes = c.total_seconds() / 60
d=int(minutes) #d can be only 15 ,30 or 60
#This function will create new row to the time series anytime when it finds gaps and will fill it with NaN or leave it blank.
#df.drop_duplicates(keep='first') keeps the first value of duplicates
if d==15:
df= df.set_index('dtime').asfreq('-15T')
elif d==30:
df= df.set_index('dtime').asfreq('-30T')
elif d==60:
df= df.set_index('dtime').asfreq('-60T')
else:
None
return df
, и я хотел, чтобы она повторялась и перезаписывала все листы в файле Excel. Я пытаюсь это, но я получаю сообщение об ошибке: ValueError: не могу переиндексировать с дублирующейся оси.
import pandas as pd
import openpyxl
path = os.getcwd()
files = os.listdir(path)
files
wb = openpyxl.load_workbook('example1.xlsx')
for sheet in wb.worksheets:
df.columns =(['dtime','kW'])
df['dtime'] = pd.to_datetime(df['dtime'])
a = df.loc[0, 'dtime']
b = df.loc[1, 'dtime']
c = a - b
minutes = c.total_seconds() / 60
d=int(minutes)
if d==15:
df= df.set_index('dtime').asfreq('-15T')
elif d==30:
df= df.set_index('dtime').asfreq('-30T')
elif d==60:
df= df.set_index('dtime').asfreq('-60T')
else:
None
return df