python中的初学者并获得日期时间错误。不知道为什么。任何帮助将非常感激。
我在同一目录中有2个.py文件,model.py和func2.py。我正在尝试在运行model.py时导入func.py。我得到的错误是 NameError:name' datetime'未定义
model.py:
import numpy as np
import pandas as pd
import datetime
from xlsxwriter.utility import xl_rowcol_to_cell
import func2
def data_upload(xls):
demand_df=pd.read_excel(xls,'Historical Demand',encoding = "ISO-8859-1",parse_dates=['Transaction Date'],converters={'Part Number':str})
po_df=pd.read_excel(xls,'Historical PO',encoding = "ISO-8859-1",parse_dates=['Date Order Placed', 'Date Order Received'],converters={'Part Number':str, 'Unit Cost':float,'Total Spend':float})
snapshot_df=pd.read_excel(xls,'Snapshot Summary',converters={'Part Number':str},)
other_df=pd.read_excel(xls,'Other Data',converters={'Part Number':str})
return demand_df, po_df, snapshot_df, snapshot_df, other_df
xls = pd.ExcelFile('/Users/Desktop/Input Raw Data Template.xlsx')
demand_df, po_df, snapshot_df, snapshot_df, other_df = data_upload(xls)
input_scope = 365
input_leadtime_max = 140
model_input_df = func2.scoping_analysis(demand_df,po_df,input_scope)
func2.py:
import numpy as np
import pandas as pd
import datetime
def scoping_analysis(demand_df,po_df,input_scope):
demand_scope = []
demand_timeframe = demand_df['Transaction Date']> demand_df['Transaction Date'].max() - datetime.timedelta(days=input_scope)
demand_df = demand_df[demand_timeframe].groupby('Part Number').sum().astype(int)
demand_df = demand_df[demand_df['Transaction Quantity']>0].sort_values('Transaction Quantity',ascending=False)
demand_df.reset_index(inplace=True)
demand_scope = demand_df['Part Number'].values
po_scope = []
po_timeframe = po_df['Date Order Placed']> po_df['Date Order Placed'].max() - datetime.timedelta(days=input_scope)
po_df = po_df[po_timeframe].groupby('Part Number').sum().astype(int)
po_df = po_df[po_df['Total Spend']>0].sort_values('Total Spend',ascending=False)
po_df.reset_index(inplace=True)
po_scope = po_df['Part Number'].values
inscope_items = list(set(demand_scope).intersection(po_scope))
model_input_df = pd.DataFrame(data = inscope_items, columns=['Part Number'])
return model_input_df
回溯:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-38-3db20f4cbfba> in <module>()
----> 1 model_input_df = func2.scoping_analysis(demand_df,po_df,input_scope)
2
~/Desktop/Programming/Code/SPO/func2.py in scoping_analysis(demand_df, po_df, input_scope)
1 import numpy as np
2 import pandas as pd
----> 3 import datetime
4
5 def scoping_analysis(demand_df,po_df,input_scope):
NameError: name 'datetime' is not defined