值错误:使用 pandas.read_csv() 时,'float' 类型的对象'd'未知的格式代码



我发现了产生这种奇怪行为的原因:在其中一个模块中,我引入了一个错误pd.options.display.float_format = '{:,.0d}'.format,只有在打印DataFrame时才会显示出来。我将把我最初的问题留在这里,这样它可能会帮助其他人。

我正在研究我的代码中的一个错误,现在我发现在代码的一部分(一个模块(中,我可以将csv读取到Panda DataFrame中而没有问题(例如,最后我可以在调试控制台中执行df(,而在另一部分(另一个模块,但当我尝试打印它时,我会得到以下错误:ValueError: Unknown format code 'd' for object of type 'float'(当我在调试控制台中执行df时,我得到<pandas.core.frame.DataFrame object at 0x172dcad60>(。

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/machine/miniforge3/envs/py39deps26-source/lib/python3.9/site-packages/pandas/core/frame.py", line 995, in __repr__
self.to_string(
File "/machine/miniforge3/envs/py39deps26-source/lib/python3.9/site-packages/pandas/core/frame.py", line 1131, in to_string
return fmt.DataFrameRenderer(formatter).to_string(
File "/machine/miniforge3/envs/py39deps26-source/lib/python3.9/site-packages/pandas/io/formats/format.py", line 1053, in to_string
string = string_formatter.to_string()
File "/machine/miniforge3/envs/py39deps26-source/lib/python3.9/site-packages/pandas/io/formats/string.py", line 25, in to_string
text = self._get_string_representation()
File "/machine/miniforge3/envs/py39deps26-source/lib/python3.9/site-packages/pandas/io/formats/string.py", line 40, in _get_string_representation
strcols = self._get_strcols()
File "/machine/miniforge3/envs/py39deps26-source/lib/python3.9/site-packages/pandas/io/formats/string.py", line 31, in _get_strcols
strcols = self.fmt.get_strcols()
File "/machine/miniforge3/envs/py39deps26-source/lib/python3.9/site-packages/pandas/io/formats/format.py", line 540, in get_strcols
strcols = self._get_strcols_without_index()
File "/machine/miniforge3/envs/py39deps26-source/lib/python3.9/site-packages/pandas/io/formats/format.py", line 804, in _get_strcols_without_index
fmt_values = self.format_col(i)
File "/machine/miniforge3/envs/py39deps26-source/lib/python3.9/site-packages/pandas/io/formats/format.py", line 818, in format_col
return format_array(
File "/machine/miniforge3/envs/py39deps26-source/lib/python3.9/site-packages/pandas/io/formats/format.py", line 1240, in format_array
return fmt_obj.get_result()
File "/machine/miniforge3/envs/py39deps26-source/lib/python3.9/site-packages/pandas/io/formats/format.py", line 1271, in get_result
fmt_values = self._format_strings()
File "/machine/miniforge3/envs/py39deps26-source/lib/python3.9/site-packages/pandas/io/formats/format.py", line 1518, in _format_strings
return list(self.get_result_as_array())
File "/machine/miniforge3/envs/py39deps26-source/lib/python3.9/site-packages/pandas/io/formats/format.py", line 1435, in get_result_as_array
return format_with_na_rep(self.values, self.formatter, self.na_rep)
File "/machine/miniforge3/envs/py39deps26-source/lib/python3.9/site-packages/pandas/io/formats/format.py", line 1427, in format_with_na_rep
[
File "/machine/miniforge3/envs/py39deps26-source/lib/python3.9/site-packages/pandas/io/formats/format.py", line 1428, in <listcomp>
formatter(val) if not m else na_rep
ValueError: Unknown format code 'd' for object of type 'float'

在两个模块上读取的精确指令是相等的(!(,但我得到了

df = pd.read_csv(results_path / f'{results}.csv')

我无法复制它,除非我通过现有的代码(通过现有的模块(运行它。如果我使用一个新程序,它只是用pd.read_csv()函数读取csv,那么问题就不会发生。我真的无法理解这一点,所以我把这个留在这里,希望有人能发现我做错了什么。

也许问题出在数据上:它似乎同时存在int值和float值。您应该尝试将列转换为浮点类型。(这是read_csv方法中的一个参数(

以下是一些有类似问题的人:

  • 字符串格式设置为"{0:d}";。format给出未知格式代码';d';对于类型';float';

  • 字符串格式(ValueError:类型为';str';的对象的未知格式代码';d';(

  • 未知格式代码';f';对于类型';unicode';

相关内容

  • 没有找到相关文章

最新更新