【发布时间】:2020-04-21 20:28:06
【问题描述】:
我正在尝试创建一个函数,该函数返回从给定年份和给定月份到今天的年份和月份的数据。为简单起见,我用外循环和内循环的打印语句替换了我希望我的函数执行的操作。我收到错误 TypeError: 'str' 对象不能被解释为整数
定义函数
def frequency_database(df,start_month,start_year):
data = pd.DataFrame([])
import datetime
start_month=int(start_month)
start_year=int(start_year)
today = datetime.date.today()
today_year=today.strftime('%Y')
for y in range(start_year,today_year):
print('Outer loop enter for year', y)
Some function here which I want to do.............
for x in range(start_month,13):
print('Inner loop enter for month number',x)
Some function here which I want to do.............
调用功能
frequency_database(df,1,2015)
错误
TypeError: 'str' 对象不能被解释为整数
根据要求进行堆栈跟踪
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-37-06567ae5d027> in <module>
12 print('Inner loop enter for month number',x)
13
---> 14 frequency_database(df,1,2015)
<ipython-input-37-06567ae5d027> in frequency_database(df, start_month, start_year)
7 today_year=today.strftime('%Y')
8
----> 9 for y in range(start_year,today_year):
10 print('Outer loop enter for year', y)
11 for x in range(start_month,13):
TypeError: 'str' object cannot be interpreted as an integer
【问题讨论】:
-
用
today_year=int(today.strftime('%Y'))替换today_year=today.strftime('%Y') -
请包含您的异常的堆栈跟踪并告诉我们它指示的是哪一行
标签: python loops datetime for-loop