gpt4 book ai didi

python - Pandas 数据框错误 'StringArray requires a sequence of strings or pandas.NA'

转载 作者:行者123 更新时间:2023-12-03 16:41:36 26 4
gpt4 key购买 nike

在过去的一个月里,我一直在阅读Excel工作表,而使用Pandas则没有任何问题。

尽管最近我更改了日期格式(现在已改回其原始格式mm/dd/yyyy)。

现在,当我尝试读取.xlsm时,所有突然的Pandas都会引发错误

下面是完整的错误

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-5-655581a0eaab> in <module>
----> 1 inv = p.read(r'C:\Users\Jake\OneDrive\Documents\Eastern\Inventory\Inventory_Log.xlsm')

~\Anaconda3\lib\site-packages\pyinv\__init__.py in read(filepath)
36 'Cert_Expire':'string',
37 'Rating':'string',
---> 38 'Notes':'string'
39 })
40 amount = inv.Item.count()

~\Anaconda3\lib\site-packages\pandas\io\excel\_base.py in read_excel(io, sheet_name, header, names, index_col, usecols, squeeze, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, verbose, parse_dates, date_parser, thousands, comment, skipfooter, convert_float, mangle_dupe_cols, **kwds)
332 convert_float=convert_float,
333 mangle_dupe_cols=mangle_dupe_cols,
--> 334 **kwds,
335 )
336

~\Anaconda3\lib\site-packages\pandas\io\excel\_base.py in parse(self, sheet_name, header, names, index_col, usecols, squeeze, converters, true_values, false_values, skiprows, nrows, na_values, parse_dates, date_parser, thousands, comment, skipfooter, convert_float, mangle_dupe_cols, **kwds)
883 convert_float=convert_float,
884 mangle_dupe_cols=mangle_dupe_cols,
--> 885 **kwds,
886 )
887

~\Anaconda3\lib\site-packages\pandas\io\excel\_base.py in parse(self, sheet_name, header, names, index_col, usecols, squeeze, dtype, true_values, false_values, skiprows, nrows, na_values, verbose, parse_dates, date_parser, thousands, comment, skipfooter, convert_float, mangle_dupe_cols, **kwds)
510 )
511
--> 512 output[asheetname] = parser.read(nrows=nrows)
513
514 if not squeeze or isinstance(output[asheetname], DataFrame):

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in read(self, nrows)
1131 def read(self, nrows=None):
1132 nrows = _validate_integer("nrows", nrows)
-> 1133 ret = self._engine.read(nrows)
1134
1135 # May alter columns / col_dict

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in read(self, rows)
2462 columns, data = self._do_date_conversions(columns, data)
2463
-> 2464 data = self._convert_data(data)
2465 index, columns = self._make_index(data, alldata, columns, indexnamerow)
2466

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in _convert_data(self, data)
2532 self.verbose,
2533 clean_conv,
-> 2534 clean_dtypes,
2535 )
2536

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in _convert_to_ndarrays(self, dct, na_values, na_fvalues, verbose, converters, dtypes)
1725 # invalid input to is_bool_dtype
1726 pass
-> 1727 cvals = self._cast_types(cvals, cast_type, c)
1728
1729 result[c] = cvals

~\Anaconda3\lib\site-packages\pandas\io\parsers.py in _cast_types(self, values, cast_type, column)
1823 array_type = cast_type.construct_array_type()
1824 try:
-> 1825 return array_type._from_sequence_of_strings(values, dtype=cast_type)
1826 except NotImplementedError:
1827 raise NotImplementedError(

~\Anaconda3\lib\site-packages\pandas\core\arrays\string_.py in _from_sequence_of_strings(cls, strings, dtype, copy)
199 @classmethod
200 def _from_sequence_of_strings(cls, strings, dtype=None, copy=False):
--> 201 return cls._from_sequence(strings, dtype=dtype, copy=copy)
202
203 def __arrow_array__(self, type=None):

~\Anaconda3\lib\site-packages\pandas\core\arrays\string_.py in _from_sequence(cls, scalars, dtype, copy)
195 result[na_values] = StringDtype.na_value
196
--> 197 return cls(result)
198
199 @classmethod

~\Anaconda3\lib\site-packages\pandas\core\arrays\string_.py in __init__(self, values, copy)
164 self._dtype = StringDtype()
165 if not skip_validation:
--> 166 self._validate()
167
168 def _validate(self):

~\Anaconda3\lib\site-packages\pandas\core\arrays\string_.py in _validate(self)
169 """Validate that we only store NA or strings."""
170 if len(self._ndarray) and not lib.is_string_array(self._ndarray, skipna=True):
--> 171 raise ValueError("StringArray requires a sequence of strings or pandas.NA")
172 if self._ndarray.dtype != "object":
173 raise ValueError(

ValueError: StringArray requires a sequence of strings or pandas.NA

发生这种情况的原因是,当 Pandas 正在读取它时,我将其将列转换为字符串dtypes。不知道为什么日期格式打破了这一点。

当前正在使用Pandas 1.0.1

任何信息都将不胜感激。

如果需要,我将提供更多详细信息。

最佳答案

根据文档Pandas doc:

新的df.astype()方法可以与str"string"一起使用

区别在于:使用"string"它将尝试转换为不能包含任何数字的StringArray。

使用str,它将作为对象类型进行处理,该对象类型像以前一样工作。

关于python - Pandas 数据框错误 'StringArray requires a sequence of strings or pandas.NA',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60328035/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com