我有一个基于pandas的工作python脚本。将类似的脚本转换成。exe在我的电脑上工作。不幸的是,我家里的电脑不是这样的。我尝试了pyinstaller和py2exe,都出现了这个错误。在我看来,转换提出了一些错误(我已经修复了其中的一些),所以它最终不是关于日期时间问题,我认为。
import pandas as pd
import os
import glob
from datetime import datetime
import shutil
import os.path
try:
parentfolder = os.path.dirname(__file__)
parentfolder = os.path.abspath(os.path.join(parentfolder, '..'))#parentfolder der skriptdatei
except NameError: # We are the main py2exe script, not a module
import sys
parentfolder = os.path.dirname(sys.argv[0])
parentfolder = os.path.abspath(os.path.join(parentfolder, '..'))#parentfolder der skriptdatei
today = datetime.now()
day1 = today.strftime("%d-%m-%Y")
time1= today.strftime("%d-%m-%Y_%H-%M-%S")
day1=day1+'_cleaned'
logname="logfile_" + time1 + ".txt"
resultfolder=os.path.join(parentfolder, day1)
logfile = os.path.join(resultfolder, logname)
if os.path.exists(resultfolder):
shutil.rmtree(resultfolder) #deletes folder and all subfolders
os.makedirs(resultfolder)
pd.set_option('display.max_columns', 5)
pd.set_option('display.max_colwidth', 99)
f = open(logfile, "w")
f.close()
all_files = glob.glob(parentfolder + "/*.xls")
filecounter=0
first_run_counter=0
first_day_counter=0
for filename in all_files:
file_name=(os.path.splitext(os.path.basename(filename))[0])
writepath = os.path.join(resultfolder, '{}.xlsx'.format(str(file_name)+"_py"))
writer = pd.ExcelWriter(writepath, engine = 'xlsxwriter')
with open(logfile, "a") as file:
file.write("{} n".format(str(file_name)))
filecounter += 1
if filecounter > 1:
print("WARNING, JUST CONVERT 1 FILE")
break
list1 = []
dfs_by_day= []
df = pd.read_excel(filename,header=None,parse_dates=False)#ohne header einlesen ,decimal=","
#df=df.convert_dtypes(convert_string=True)
df_help=df.copy()
df_help[1] = df_help[1].astype(str)
df_help[0] = df_help[0].astype(str)
#####datei ordnen,filtern etc
df.dropna(axis=0,how='any',thresh=None,subset=None,inplace=True)#löscht zeilen mit leeren zellen
df.drop_duplicates(inplace=True) #dropt auch doppelte header?!
df.reset_index(drop=True, inplace=True)
new_header = df.iloc[0] #grab the first row for the header
df = df[1:] #take the data less the header row
df.columns = new_header#nimmt 2, reihe als header
df = df.sort_values(['Date (MM/DD/YYYY)','Time (HH:mm:ss)'], ascending=[True,True])
df.reset_index(drop=True, inplace=True)
df.rename(columns={'Date (MM/DD/YYYY)':'Date (DD/MM/YYYY)'}, inplace=True)
#df['Date (DD/MM/YYYY)']=df['Date (DD/MM/YYYY)'].astype(str)#WICHTIG! datumsangabe unabhängig von / oder . machen
#df['Date (DD/MM/YYYY)'] = df['Date (DD/MM/YYYY)'].str.replace('/','.')#/ mit . ersetzen
df_help2=df.copy() #deepcopy vom noch nicht datetime, aber getrimmten dataframe
#################################################################### datei in tage aufspalten
##df_help2['Date (DD/MM/YYYY)'] = pd.to_datetime(df_help2['Date (DD/MM/YYYY)'],format='%d.%m.%Y')#EVTL FORMAT EINFÜGEN ,format='%d.%m.%Y'
df_help2['next day'] = (df_help2['Date (DD/MM/YYYY)'].diff()).dt.days > 0 #ob neue zeile=neuer tag
###############datumsangabe unabhängig von / oder . machen
for i in range(df_help2.shape[0]):
if df_help2.at[i,'next day'] == True:
list1.append(i)
#spaltalgorithmus gesamtfile in tage
l_mod = [0] + list1 + [df.shape[0]]
dfs_by_day = [df.iloc[l_mod[n]:l_mod[n+1]] for n in range(len(l_mod)-1)]
################################################################# tage in runs aufspalten
for j in dfs_by_day:
memo=0
run_counter=1
df1 = j
df1=df1.reset_index(drop=True)
df_help4 = df1.iloc[0:1,0:2].reset_index(drop=True).copy()
df1['Date (DD/MM/YYYY)'] = df1['Date (DD/MM/YYYY)'].dt.strftime('%d.%m.%Y')
list3=[]
dfdate= str(df1.at[0,'Date (DD/MM/YYYY)'])
print(dfdate)
df_help3=df1.copy() #deepcopy für tageszeitanalyse/runs
df_help3['Time (HH:mm:ss)'] = pd.to_datetime(df_help3['Time (HH:mm:ss)'],format='%H:%M:%S')
df_help3['next run'] = (df_help3['Time (HH:mm:ss)'].diff()).dt.seconds > 2000
df_help3.reset_index(drop=True, inplace=True)
for i in range(df_help3.shape[0]):
if df_help3.at[i,'next run'] == True:
list3.append(i)
###algorithmus spaltet tag in runs auf
l_mod2 = [0] + list3 + [df1.shape[0]]
dfs_by_run = [df1.iloc[l_mod2[n]:l_mod2[n+1]] for n in range(len(l_mod2)-1)]
for k in dfs_by_run:
df_run = k
df_run['Depth m'] = pd.to_numeric(df_run['Depth m'])
df_run['depth rounded'] = df_run['Depth m'].astype(int) #rundet
df_run=df_run.reset_index(drop=True)
df_run = df_run.drop_duplicates(subset=['depth rounded'], keep='last')#letzter wert
del df_run['depth rounded']
df_run=df_run.dropna(axis=0,how='any',thresh=2)
df_run=df_run.reset_index(drop=True)
run_name = str(dfdate) +'_run' + str(run_counter)
#####sensortoresultfile
if first_run_counter==0:
last_df=df_run.copy()
last_df=last_df[0:0]
last_df=last_df.append(df_run)
first_run_counter+=1
with open(logfile, "a") as file:
file.write("{0} has {1} last measurement(s) n".format(run_name,df_run.shape[0]))
run_counter+=1
#alle daten raw aber mit sensor und header pro tag
df_help4['Time (HH:mm:ss)'] = df_help4['Time (HH:mm:ss)'].astype(str)
df_help4['Date (DD/MM/YYYY)'] = df_help4['Date (DD/MM/YYYY)'].astype(str)
for i in range(df_help.shape[0]):
if df_help4.at[0,'Date (DD/MM/YYYY)'] == df_help.at[i,0]:
if df_help4.at[0,'Time (HH:mm:ss)'] == df_help.at[i,1]:
memo=i
break
for n in reversed(list(range(memo))):
if df_help.at[n,3] == 'SENSOR SERIAL NUMBER:':
sensor_info=df_help.iloc[n:n+1,:]
sensor_info.reset_index(drop=True,inplace=True)
break
sensor_info.at[0,0:2]='-'
df1 = df1.columns.to_frame().T.append(df1, ignore_index=True)#fügt header als zeile ganz oben hinzu
df1.columns = range(len(df1.columns))#header neu 0 bis n
if first_day_counter==0:
raw_df=df1.copy()
raw_df=raw_df[0:0]
sensor_info.columns= range(len(df1.columns))
df1 = pd.concat([df1.iloc[:(0)], sensor_info, df1.iloc[0:]]).reset_index(drop=True)
raw_df=raw_df.append(df1)
first_day_counter += 1
last_df.to_excel(writer, sheet_name='{}'.format("last"),header=False, index = False)
#raw_df['Date (DD/MM/YYYY)'] = raw_df['Date (DD/MM/YYYY)'].dt.strftime('%d.%m.%Y')
raw_df.to_excel(writer, sheet_name='{}'.format("raw"),header=False, index = False)
writer.save()
with open(logfile, "a") as file:
file.write("total number of last measurements: {} n".format(last_df.shape[0]))
file.write("total number of raw measurements: {} n".format(raw_df.shape[0]))
f.close()
错误:
Traceback (most recent call last):
File "tsk-py-convert.py", line 95, in <module>
File "pandascoregeneric.pyc", line 5458, in __getattr__
File "pandascoreaccessor.pyc", line 180, in __get__
File "pandascoreindexesaccessors.pyc", line 494, in __new__
AttributeError: Can only use .dt accessor with datetimelike values
在spyder中,代码使用的是旧的pandas版本(0.23.4)。我的代码似乎不能在新版本上工作。我在windows上安装了最新的pandas版本pip,现在手动安装了anaconda版本(0.23.4)。我现在可以通过cmd, IDLE和pyinstaller works创建的.exe运行代码!