pandas-valueerror:stringarray需要字符串序列或pandas.na

pw136qt2  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(214)

我遇到了一个奇怪的错误,我不知道该怎么去改正它。发生在这条线上

df_filter_start['end_date_cutoff_delta'] = (df_filtered['end_date'] - cutoff_date).dt.days

这是整个错误日志

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-170-e06e4d71ac4d> in <module>
      3     for device_model in testing_dict['device_models']:
      4         for region in testing_dict['regions']:
----> 5             df_list.append(get_projection(cutoff_date, df_survival_merged, device_model, region))

<ipython-input-167-1622ba7ed3a8> in get_projection(cutoff_date, df, device_model, region)
     11         # Apply cutoff_date
     12         df_filter_start = df_filtered[(df_filtered['revised_start_date'] - cutoff_date).dt.days < 0]
---> 13         df_filter_start['end_date_cutoff_delta'] = (df_filtered['end_date'] - cutoff_date).dt.days
     14         df_filter_start['failure_status_backtest'] = np.where(df_filter_start['end_date_cutoff_delta'] > 0, 0, df_filter_start['failure_status'])
     15 

~/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in __setitem__(self, key, value)
   3161         else:
   3162             # set column
-> 3163             self._set_item(key, value)
   3164 
   3165     def _setitem_slice(self, key: slice, value):

~/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in _set_item(self, key, value)
   3236         ensure homogeneity.
   3237         """
-> 3238         self._ensure_valid_index(value)
   3239         value = self._sanitize_column(key, value)
   3240         NDFrame._set_item(self, key, value)

~/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in _ensure_valid_index(self, value)
   3299                 index_copy.name = self.index.name
   3300 
-> 3301             self._mgr = self._mgr.reindex_axis(index_copy, axis=1, fill_value=np.nan)
   3302 
   3303     def _box_col_values(self, values, loc: int) -> Series:

~/anaconda3/lib/python3.8/site-packages/pandas/core/internals/managers.py in reindex_axis(self, new_index, axis, method, limit, fill_value, copy, consolidate, only_slice)
   1248         )
   1249 
-> 1250         return self.reindex_indexer(
   1251             new_index,
   1252             indexer,

~/anaconda3/lib/python3.8/site-packages/pandas/core/internals/managers.py in reindex_indexer(self, new_axis, indexer, axis, fill_value, allow_dups, copy, consolidate, only_slice)
   1309             )
   1310         else:
-> 1311             new_blocks = [
   1312                 blk.take_nd(
   1313                     indexer,

~/anaconda3/lib/python3.8/site-packages/pandas/core/internals/managers.py in <listcomp>(.0)
   1310         else:
   1311             new_blocks = [
-> 1312                 blk.take_nd(
   1313                     indexer,
   1314                     axis=axis,

~/anaconda3/lib/python3.8/site-packages/pandas/core/internals/blocks.py in take_nd(self, indexer, axis, new_mgr_locs, fill_value)
   1861         # but are passed the axis depending on the calling routing
   1862         # if its REALLY axis 0, then this will be a reindex and not a take
-> 1863         new_values = self.values.take(indexer, fill_value=fill_value, allow_fill=True)
   1864 
   1865         # Called from three places in managers, all of which satisfy

~/anaconda3/lib/python3.8/site-packages/pandas/core/arrays/_mixins.py in take(self, indices, allow_fill, fill_value, axis)
     76             axis=axis,
     77         )
---> 78         return self._from_backing_data(new_data)
     79 
     80     def _validate_fill_value(self, fill_value):

~/anaconda3/lib/python3.8/site-packages/pandas/core/arrays/numpy_.py in _from_backing_data(self, arr)
    188 
    189     def _from_backing_data(self, arr: np.ndarray) -> "PandasArray":
--> 190         return type(self)(arr)
    191 
    192     # ------------------------------------------------------------------------

~/anaconda3/lib/python3.8/site-packages/pandas/core/arrays/string_.py in __init__(self, values, copy)
    193         self._dtype = StringDtype()  # type: ignore[assignment]
    194         if not isinstance(values, type(self)):
--> 195             self._validate()
    196 
    197     def _validate(self):

~/anaconda3/lib/python3.8/site-packages/pandas/core/arrays/string_.py in _validate(self)
    198         """Validate that we only store NA or strings."""
    199         if len(self._ndarray) and not lib.is_string_array(self._ndarray, skipna=True):
--> 200             raise ValueError("StringArray requires a sequence of strings or pandas.NA")
    201         if self._ndarray.dtype != "object":
    202             raise ValueError(

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

任何关于解决这个问题的建议都将不胜感激。我知道这是一个已知的错误与新的Pandas版本,但我不知道如何着手修复它为我的案件。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题