until_now = (today - self.pd).days ^^^^^^^ AttributeError: 'Medicine' object has no attribute 'pd'
时间: 2024-02-15 20:58:43 浏览: 95
根据提供的引用内容,可以看出这是两个不同的问题。第一个问题是关于Python中的AttributeError,第二个问题是关于Medicine对象的AttributeError。
针对第二个问题,根据错误提示,可以看出'Medicine'对象没有'pd'属性。这意味着在创建'Medicine'对象时,没有为其定义'pd'属性。因此,需要检查代码并确保在创建'Medicine'对象时定义了'pd'属性。
以下是一个可能的解决方案:
class Medicine:
def __init__(self, name, pd):
self.name = name
self.pd = pd # 在这里定义'pd'属性
today = date.today()
pd = date(2021, 1, 1)
medicine = Medicine('Aspirin', pd)
until_now = (today - medicine.pd).days
print(until_now)
这里我们在'Medicine'类的构造函数中定义了'pd'属性,并在创建'Medicine'对象时为其赋值。这样,我们就可以在后面的代码中使用'medicine.pd'来获取'Medicine'对象的'pd'属性。
相关问题
stations_data = pd.DataFrame({ ^^^^^^^^^^^^ AttributeError: module 'pandas' has no attribute 'DataFrame' 什么意思
这个错误提示 "AttributeError: module 'pandas' has no attribute 'DataFrame'" 意味着在Python中,你尝试使用pandas模块(通常用来处理数据的库)创建DataFrame对象,但pandas模块实际上并没有定义名为DataFrame的属性。可能是以下几个原因:
- 你可能还没有导入pandas模块。确保在代码开头添加
import pandas as pd
。 - 如果你在某个特定版本的pandas或环境中,DataFrame可能被重命名或已更改。检查是否是最新版本,并确认名称是否正确。
- 可能是拼写错误或大小写不匹配。请检查
DataFrame
的拼写,确保与pandas官方文档中的定义一致。
为了解决这个问题,请尝试以下步骤:
- 检查导入语句:
import pandas as pd
- 确保没有其他库或模块覆盖了DataFrame的定义。
- 更新pandas到最新版本,如果有必要。
如果还有问题,请提供完整的代码片段以便更好地分析。
File "C:\Users\13923\Desktop\2\主页面.py", line 81, in check Home(self.window) File "C:\Users\13923\Desktop\2\主页面.py", line 120, in __init__ self.readExcel() File "C:\Users\13923\Desktop\2\主页面.py", line 127, in readExcel self.head = pd.read_Excel("1.xlsx").columns.tolist() ^^^^^^^^^^^^^ AttributeError: module 'pandas' has no attribute 'read_Excel'
这段代码的错误是因为在read_excel()函数的调用中,Excel单词中的"e"应该是小写的,但是在代码中写成了大写。因此,Python无法找到该函数并引发了AttributeError异常。正确的代码应该是这样的:
self.head = pd.read_excel("1.xlsx").columns.tolist()
相关推荐
















