【发布时间】:2020-10-28 17:45:35
【问题描述】:
这个问题的另一个版本没有得到回答,原始发布者没有给出他们代码的完整示例......
我有一个用于导入电子表格以进行格式化的函数。现在,电子表格可以有两种形式:
- 作为要作为 DataFrame 导入的文件名字符串(excel、.csv 等)
- 直接作为 DataFrame(还有另一个函数可能会或可能不会被调用来进行一些预处理)
代码看起来像
def func1(spreadsheet):
if type(spreadsheet) == pd.DataFrame:
df = spreadsheet
else:
df_ext = os.path.splitext(spreadsheet)[1]
etc. etc.
如果我使用 DataFrame 运行此函数,我会收到以下错误:
---> 67 if type(spreadsheet) == pd.DataFrame: df = spreadsheet
68 else:
/opt/anaconda3/lib/python3.7/posixpath.py in splitext(p)
120
121 def splitext(p):
--> 122 p = os.fspath(p)
123 if isinstance(p, bytes):
124 sep = b'/'
TypeError: expected str, bytes or os.PathLike object, not DataFrame
为什么要这样做?
【问题讨论】:
-
-
我试过了。它抛出了同样的错误......